zoukankan      html  css  js  c++  java
  • Codeforces Round#407 Div.2

    A. Anastasia and pebbles

    题面

    Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.

    She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.

    Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.

    题意

    Anastasia有两个包,问几天装完所有东西

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    priority_queue<int> q;
    int n,k;
    int x;
    int cnt;
    
    int main()
    {
    	cin>>n>>k;
    	for (int i=1;i<=n;i++) 
    	{
    		cin>>x;
    		cnt+=(x%k==0)?(x/k):(x/k+1);
    	}
    	cout<<(cnt+1)/2;
    } 
    

    B. Masha and geometric depression

    题面

    Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.

    You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integers b1, b2, b3, ..., where for each i > 1 the respective term satisfies the condition bi = bi - 1·q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can equal 0. Also, Dvastan gave Masha m "bad" integers a1, a2, ..., am, and an integer l.

    Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.

    But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf" in case she needs to write infinitely many integers.

    题意

    等差数列bi,与等差数列ai,bi<l,从b1开始写的第一个ai不重复的是多少。

    不存在输出0,死循环输出inf

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    long long n,m,l,q;
    long long x;
    set<long long> s;
    long long ans;
    
    int main()
    {
    	cin>>n>>q>>l>>m;
    	for (int i=1;i<=m;i++) 
    	{
    		cin>>x;
    		if (!s.count(x)) s.insert(x);
    	}
    	
    	if (n==0)
    	{
    		if (s.count(0)) cout<<0;else cout<<"inf";
    		return 0;
    	}
    	
    	if (abs(n)>l)
    	{
    		cout<<0;
    		return 0;
    	}
    	
    	if (q==1)
    	{
    		if (s.count(n))
    		{
    			cout<<0<<endl;
    			return 0;
    		}
    		cout<<"inf"<<endl;
    		return 0;
    	}
    	
    	if (q==-1)
    	{
    		if (s.count(n) && s.count(-n))
    		{
    			cout<<0<<endl;
    			return 0;
    		}
    		cout<<"inf"<<endl;
    		return 0;
    	}
    	
    	if (q==0) 
    	{
    		if (!s.count(0))
    		{
    			cout<<"inf";
    			return 0;
    		}
    		if (!s.count(n)) 
    		{
    			cout<<1;
    			return 0;
    		}
    		cout<<0;return 0;
    	}
    	
    	x=n;
    	while (abs(x)<=l)
    	{
    		if (!s.count(x)) 
    			ans++;
    		x*=q;
    		continue;
    	}
    	cout<<ans;
    } 
    

    C. Functions again

    题面

    Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

    In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

    题意

    连续区间的最大和。分从奇数开始和从偶数开始。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    long long n;
    long long a[100010];
    long long ans;
    long long maxi;
    
    int main()
    {
    	cin>>n;
    	for (int i=1;i<=n;i++) cin>>a[i];
    	for (int i=1;i<n;i++) a[i]=abs(a[i+1]-a[i]);
    	for (int i=1;i<n;i++) if (i%2==0) a[i]*=(-1); 
    	
    	for (int i=1;i<n;i++)
    	{
    		if (maxi<0) maxi=a[i];else maxi+=a[i];
    		ans=max(ans,maxi);
    	}
    	
    	maxi=0;
    	for (int i=1;i<n;i++) a[i]*=(-1); 
    	for (int i=1;i<n;i++)
    	{
    		if (maxi<0) maxi=a[i];else maxi+=a[i];
    		ans=max(ans,maxi);
    	}
    	
    	cout<<ans;
    } 
    

    D. Weird journey

    题面

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia.

    It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over m - 2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.

    Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor — calculate the number of good paths.

    题意

    选两条路只走一次,其他路走两次,回到最初的起点。

    官方题解

    先dfs或者bfs判断是否能走通。

    枚举走一次的边。

    • 选两次普通边。显然需要同时经过同一个点。
    • 选择一个自环,另一条边可以任意
    • 选两个自环。显然可以

    所以我们先加上普通边组成的,然后加上自环的,减去多算了一次的自环。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    using ll = long long;
    int n;
    ll m;
    vector<int> g[1000100];
    int cnt[1000010]; 
    ll dgree[1000010];
    int vis[1000010];
    ll loop;
    ll ans;
    int flag;
    int in[1000010];
    queue<int> q;
    
    int dfs(int k)
    {
    	vis[k]=1;
    	for (int i=0;i<g[k].size();i++)
    	{
    		int u=g[k][i];
    		if (!vis[u]) dfs(u);
    	}
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    	cin>>n>>m;
    	for (int i=1;i<=m;i++)
    	{
    		int x,y;
    		cin>>x>>y;
    		if (x==y) 
    		{
    			if (!in[x])
    			{
    				q.push(x);
    				in[x]=1;
    			}
    			loop++;
    			continue;
    		}
    		dgree[x]++;
    		dgree[y]++;
    		flag=1;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		if (!in[x])
    		{
    			q.push(x);
    			in[x]=1;
    		}
    		if (!in[y])
    		{
    			q.push(y);
    			in[y]=1;
    		}
    	}
    	
    	for (int i=1;i<=n;i++) if (dgree[i] && !vis[i]) 
    	{
    		dfs(i);
    		break;
    	}
    	while (!q.empty())
    	{
    		int x=q.front();q.pop();
    		if (!vis[x]) return 0*puts("0");
    	}
    	
    	for (int i=1;i<=n;i++)
    		ans+=dgree[i]*(dgree[i]-1)/2;
    	if (loop) ans+=loop*(m-1);
    	if (loop) ans-=loop*(loop-1)/2;
    	
    	cout<<ans;
    }
    

    E. The Great Mixing

    题面

    Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

    Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

    Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

    题意

    问拿最少的数字,使他们平均数为给出的n。

    官方题解

    因为0<n<1000,ai也是,所以最多有1001个数字。且访问的距离在0前后1000以内。(因为不可能同时存在501和-501,稍微想一下=w=)

    所以用bfs最坏情况 O(2000*min(k,1001))

    代码

    #include<bits/stdc++.h>
    using namespace std;
    int k,n; 
    int cnt;
    int b[1010];
    int a[1010];
    int f[1010]; //我把这里改小了,应该可以过评测,如果过不了这里改成10001000
    queue<int> q;
    bool flag1,flag2;
    
    int main()
    {
        ios::sync_with_stdio(false);
    	cin>>k>>n;
    	for (int i=1;i<=n;i++) 
    	{
    		cin>>a[++cnt];
    		if (b[a[cnt]]) 
    		{
    			--cnt;
    			continue;
    		}
    		b[a[cnt]]=1;
    	}
    	for (int i=1;i<=cnt;i++) a[i]-=k; 
    	for (int i=1;i<=cnt;i++) 
    	{
    		if (a[i]>0) flag2=1;
    		if (a[i]<0) flag1=1;
    		if (a[i]==0) return 0*puts("1");
    	}
    	if (!flag1 || !flag2) return 0*puts("-1");
    	
    	for (int i=1;i<=cnt;i++)  if (a[i]>0)
    	{
    		q.push(a[i]);
    		f[a[i]]=1;
    	}
    		
    	while (!q.empty())
    	{
    		int x=q.front();q.pop();
    		
    		if (x==0) break;
    			
    		for (int i=1;i<=cnt;i++)
    			if (x+a[i]>=0 && !f[x+a[i]] && x+a[i]<=1000)
    			{
    				f[x+a[i]] = f[x]+1;
    				q.push(x+a[i]);
    			} 
    	}
    	if (!f[0]) return 0*puts("0");else cout<<f[0];
    }
    

    比赛链接

    http://codeforces.com/contest/789

  • 相关阅读:
    memcache启动程序/etc/sysconfig/memcached
    shell中的点号
    mysql监控
    secureCRT 中文乱码
    memcache key
    杀死所以数据库进程
    导出表记录
    重建二叉树
    从尾到头打印链表
    二维数组中的查找
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/6877865.html
Copyright © 2011-2022 走看看