codeforces Codeforces Round #407 Div2题解

A. Anastasia and pebbles

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers n and k (1 ≤ n ≤ 1051 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.

Output

The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.

Examples
input
3 2
2 3 4
output
3
input
5 4
3 1 8 9 7
output
Note

In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.

Optimal sequence of actions in the second sample case:

  • In the first day Anastasia collects 8 pebbles of the third type.
  • In the second day she collects 8 pebbles of the fourth type.
  • In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
  • In the fourth day she collects 7 pebbles of the fifth type.

  • In the fifth day she collects 1 pebble of the second type.

题解:水题。

#include<bits/stdc++.h>
using namespace std;
int n,k;
int exm;
int main()
{
    scanf("%d %d",&n,&k);
    int ans=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&exm);
        ans=ans+exm/k;
        if(exm%k)
            ans++;
    }
    int re=0;
    re=ans/2;
    if(ans%2)re++;
    printf("%d\n",re);
    return 0;
}



B. Masha and geometric depression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integersb1, 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" integersa1, 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.

Input

The first line of input contains four integers b1qlm (-109 ≤ b1, q ≤ 1091 ≤ l ≤ 1091 ≤ m ≤ 105) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively.

The second line contains m distinct integers a1, a2, ..., am (-109 ≤ ai ≤ 109) — numbers that will never be written on the board.

Output

Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise.

Examples
input
3 2 30 4
6 14 25 48
output
3
input
123 1 2143435 4
123 11 -5453 141245
output
0
input
123 1 2143435 4
54343 -13 6 124
output
inf
Note

In the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed l by absolute value.

In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer.

In the third case, Masha will write infinitely integers 123.


题解:还是很水呀!!!!


#include<bits/stdc++.h>
#define ll __int64
using namespace std;
ll b1,q,l,m;
ll tmp;
map<ll,int>mp;
ll ans=0;
int main()
{
    scanf("%I64d%I64d%I64d%I64d",&b1,&q,&l,&m);
    for(ll i=1;i<=m;i++){
        scanf("%I64d",&tmp);
        mp[tmp]=1;
    }
    for(int i=1;i<=1000;i++)ans=0;
    for(int i=0;i<501;i++){
        if(abs(b1)>l)break;
        if(mp[b1]!=1)ans++;
        b1*=q;
    }
    if(ans>100)printf("inf\n");
    else printf("%I64d\n",ans);
    return 0;
}


C. Functions again
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements.

Output

Print the only integer — the maximum value of f.

Examples
input
5
1 4 2 3 1
output
3
input
4
1 5 4 7
output
6
Note

In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.


题解:这题有点意思,尽管还是很水。差分后dp即可

#include<stdio.h>
#include<algorithm>
using namespace std;
#define MAXN 500000
long long a[MAXN],f[MAXN],dp[MAXN][2],n,ans;
int main()
{
	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
	for(int i=1;i<n;i++)f[i]=abs(a[i+1]-a[i]);
	ans=dp[1][1]=f[1];
	for(int i=2;i<n;i++){
		dp[i][1]=max(f[i],dp[i-1][0]+f[i]);
		dp[i][0]=dp[i-1][1]-f[i];
		ans=max(ans,max(dp[i][1],dp[i][0]));
	}	
	printf("%I64d",ans);
	return 0;
}





D. Weird journey
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 106) — the number of cities and roads in Uzhlyandia, respectively.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) that mean that there is road between cities u and v.

It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.

Output

Print out the only integer — the number of good paths in Uzhlyandia.

Examples
input
5 4
1 2
1 3
1 4
1 5
output
6
input



题解:题目很好。把图看成一个欧拉路径,再分两种情况讨论,即有自环和无自环。注意判联通即可。

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 5000000
struct ed{int v,next;}edge[MAXN];
int n,m,de[MAXN],head[MAXN],vis[MAXN],ex[MAXN];
long long ans=0,loop=0;
void add(int u,int v){
	static int tot=0;
	edge[++tot].v=v;
	edge[tot].next=head[u];
	head[u]=tot;
}
bool bfs(){
	queue<int>q;
	for(int i=1;i<=n;i++)
		if(ex[i]){q.push(i);vis[i]=1;break;}	
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v;
			if(!vis[v]){
				q.push(v);
				vis[v]=1;
			}	
		}
	}
	for(int i=1;i<=n;i++)
		if(ex[i]&&!vis[i])return false;
	return true;		
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		ex[u]=ex[v]=1;
		if(u!=v){
			add(u,v);add(v,u);
			de[u]++;de[v]++;	
		}
		else{
			add(u,v);loop++;
		}
	}	
	if(n==1||!bfs()){
		printf("0");
		return 0;	
	}
	ans=(long long)loop*(m-loop)+(long long)loop*(loop-1)/2;
	for(int i=1;i<=n;i++)
		ans+=(long long)de[i]*(de[i]-1)/2;
	printf("%I64d",ans);
	return 0;
}




E. The Great Mixing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers nk (0 ≤ n ≤ 10001 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

Examples
input
400 4
100 300 450 500
output
2
input
50 2
100 25
output
3
Note

In the first sample case, we can achieve concentration  using one liter of Coke of types  and .

In the second case, we can achieve concentration  using two liters of  type and one liter of  type: .


题解:

先用如下方式将式子变型:


接着建图,每个节点表示一种浓度,最后从0开始bfs找最小环即可


#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 10000000+10
struct ed{int v,next;}edge[MAXN];
struct si{int v,st;si(int _v,int _st):v(_v),st(_st){}};
int head[MAXN],vis[MAXN],n,k,map[2001][2001],ex[2001];
void add(int u,int v){
	static int tot=0;
	edge[++tot].v=v;
	edge[tot].next=head[u];
	head[u]=tot;	
}
int bfs(){
	queue<si>q;
	q.push(si(0,0));
	while(!q.empty()){
		int st=q.front().st,u=q.front().v;
		q.pop();
		for(int i=head[u];~i;i=edge[i].next){
			int v=edge[i].v;
			if(v==0)return st+1;
			if(!vis[v]){
				q.push(si(v,st+1));
				vis[v]=1;
			}
		}
	}
	return -1;
}
int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&k);
	for(int i=1;i<=k;i++){
		int x;
		scanf("%d",&x);
		ex[x]=1;
	}
	for(int i=0;i<=1000;i++)
		for(int j=0;j<=1000;j++)
			if(ex[j]&&i+j-n>=0&&i+j-n<=1000&&!map[i][i+j-n]){
				add(i,i+j-n);
				map[i][i+j-n]=1;		
			}
	printf("%d",bfs());		
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值