codeforces AIM Tech Round3

来源:互联网 发布:吴闲云水浒知乎 编辑:程序博客网 时间:2024/05/22 03:02
A. Juicer
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.

The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?

Input

The first line of the input contains three integers nb and d (1 ≤ n ≤ 100 0001 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000) — sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.

Output

Print one integer — the number of times Kolya will have to empty the waste section.

Examples
input
2 7 105 6
output
1
input
1 5 107
output
0
input
3 10 105 7 7
output
1
input
1 1 11
output
0
Note

In the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.

In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int main(){int n,b,d,tot=0,i,j,k,ans=0;cin>>n>>b>>d;for(i=1;i<=n;i++){int x;scanf("%d",&x);if(x>b)continue;tot+=x;if(tot>d){ans++;tot=0;}} cout<<ans;}




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

Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.

Input

The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000 - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.

The second line contains n integers x1, x2, ..., xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.

Output

Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.

Examples
input
3 101 7 12
output
7
input
2 011 -10
output
10
input
5 00 0 1000 0 0
output
0
Note

In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.

In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10.

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=100005;int pos[maxn];int main(){int n,a,i,j,k,ans,p;cin>>n>>a;ans=1e9;for(i=1;i<=n;i++){scanf("%d",&pos[i]);}n++;pos[n]=a;sort(pos+1,pos+1+n);for(i=1;i<=n;i++){if(pos[i]==a){p=i;break;}}for(i=max(0,p-2);i<=min(p-1,n-2);i++){int t1=p-i,t2=p+n-i-2;if(t2<p)t2=p;if(t1>p)t1=p;ans=min(ans,2*(a-pos[p-i])+pos[p+n-i-2]-a);ans=min(ans,a-pos[p-i]+2*(pos[p+n-i-2]-a));}cout<<ans;}



C. Letters Cyclic Shift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z 'y 'x 'b 'a 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.

What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?

Input

The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.

Output

Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.

Examples
input
codeforces
output
bncdenqbdr
input
abacaba
output
aaacaba
Note

String s is lexicographically smaller than some other string t of the same length if there exists some 1 ≤ i ≤ |s|, such thats1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti.

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;char change(char ch){if(ch=='a')return 'z';else return char(int(ch)-1);}int main(){string s;cin>>s;int i,j,k,len,start,end=1e9;bool flag=false;len=s.length();for(i=0;i<len;i++){if(s[i]!='a'&&(flag==false)){start=i;flag=true;}if(flag){if(s[i]=='a'){end=i-1;break;}}}if(flag==false){for(i=1;i<len;i++)printf("a");cout<<"z";return 0;}if(end==1e9)end=len-1;for(i=0;i<start;i++)printf("%c",s[i]);for(i=start;i<=end;i++)printf("%c",change(s[i]));for(i=end+1;i<len;i++)printf("%c",s[i]);}





D. Recover the String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For each string s consisting of characters '0' and '1' one can define four integers a00a01a10 and a11, where axy is the number ofsubsequences of length 2 of the string s equal to the sequence {x, y}.

In these problem you are given four integers a00a01a10a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than1 000 000.

Input

The only line of the input contains four non-negative integers a00a01a10 and a11. Each of them doesn't exceed 109.

Output

If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.

Examples
input
1 2 3 4
output
Impossible
input
1 2 2 1
output
0110

#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;char ans[1000005];long long A01,A10,remain0,remain1,len;int main(){long long a00,a01,a10,a11,i,j,k,num0,num1,dt1,dt2;double t;cin>>a00>>a01>>a10>>a11;t=sqrt(1+8*a00);if(a00==0)num0=0;else if(fabs(t-double(ceil(t)))<=0.0000001){num0=(1+ceil(t))/2;}else{cout<<"Impossible";return 0;}t=sqrt(1+8*a11);if(a11==0)num1=0;else if(fabs(t-double(ceil(t)))<=0.0000001){num1=(1+ceil(t))/2;}else{cout<<"Impossible";return 0;}if(a00==0&&a11==0){if(a10!=0||a01!=0){num0=1;num1=1;if(a10==1&&a01==0){cout<<"10";return 0;}else if(a01==1&&a10==0){cout<<"01";return 0;}cout<<"Impossible";return 0;}cout<<"0";return 0;} if(a00==0){if(a10!=0||a01!=0)num0=1;}if(a11==0){if(a10!=0||a01!=0)num1=1;}if(num1+num0>1000000){cout<<"Impossible";return 0;}A10=a10,A01=a01;remain0=num0,remain1=num1;len=num0+num1;for(i=1;i<=num0+num1;i++){if(remain1<=A01){ans[i]='0';A01-=remain1;remain0--;}else if(remain0<=A10){ans[i]='1';A10-=remain0;remain1--;}else break;}if(i==num0+num1+1&&A10==0&&A01==0){for(i=1;i<=num0+num1;i++){printf("%c",ans[i]);}return 0;}A10=a10;A01=a01;remain0=num0;remain1=num1;for(i=1;i<=num0+num1;i++){if(remain0<=A10){ans[i]='1';A10-=remain0;remain1--;}else if(remain1<=A01){ans[i]='0';A01-=remain1;remain0--;}else break;}if(i<=num0+num1||A10||A01){cout<<"Impossible";return 0;}else {for(i=1;i<=num0+num1;i++){printf("%c",ans[i]);}return 0;}}



0 0
原创粉丝点击