Codeforces Round #370 (Div. 2)
A. Memory and Crow
There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:
- The crow sets ai initially 0.
- The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus,ai = bi - bi + 1 + bi + 2 - bi + 3....
Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.
The next line contains n, the i'th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i'th number.
Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
5
6 -4 8 -2 3
2 4 6 1 3
5
3 -2 -1 5 6
1 -3 4 11 6
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.
In the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.
题目大意:已知数列a,ai = b(i) - b(i + 1) + b(i + 2) - b(i + 3)....求数列b。
sb题
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<vector> 7 #include<cmath> 8 #include<ctime> 9 #include<cstring> 10 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout); 11 #define llg long long 12 #define maxn 1000100 13 using namespace std; 14 llg i,j,k,n,m,a[maxn],x,c[maxn]; 15 int main() 16 { 17 yyj("1"); 18 cin>>n; 19 for (i=1;i<=n;i++) scanf("%lld",&a[i]); 20 x+=a[n]; c[n]=a[n]; 21 for (i=n-1;i>=1;i--) 22 { 23 //x*=-1; 24 c[i]=x+a[i]; 25 x*=-1; 26 x+=c[i]; 27 //x*=-1; 28 } 29 for (i=1;i<=n;i++) printf("%lld ",c[i]); 30 return 0; 31 }
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:
- An 'L' indicates he should move one unit left.
- An 'R' indicates he should move one unit right.
- A 'U' indicates he should move one unit up.
- A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
RRU
-1
UDUR
1
RUUR
2
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
题目大意:U,D,L,R分别表示向上下左右移动一格,问最少修改其中的多少个字符,才可以满足最后回到原点。
显然,如果移动步数为奇数无解,不然另a1,a2,a3,a4分别为U,D,L,R出现的次数,那么 ans=(abs(a1-a2)+abs(a3-a4))/2.
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<vector> 7 #include<cmath> 8 #include<ctime> 9 #include<cstring> 10 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout); 11 #define llg long long 12 #define maxn 100100 13 using namespace std; 14 llg a4,k,x,n,m,a1,a2,a3,i,l; 15 char s[maxn]; 16 int main() 17 { 18 //yyj("2"); 19 scanf("%s",s+1); 20 l=strlen(s+1); 21 if (l%2) 22 { 23 cout<<-1; 24 return 0; 25 } 26 else 27 { 28 for (i=1;i<=l;i++) 29 { 30 if (s[i]=='U') a1++; 31 else if (s[i]=='D') a2++; 32 else if (s[i]=='R') a3++; 33 else if (s[i]=='L') a4++; 34 } 35 cout<<(abs(a1-a2)+abs(a3-a4))/2; 36 } 37 return 0; 38 }
Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
6 3
4
8 5
3
22 4
6
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .
In the second sample test, Memory can do .
In the third sample test, Memory can do:
.
There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:
- The crow sets ai initially 0.
- The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus,ai = bi - bi + 1 + bi + 2 - bi + 3....
Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.
The next line contains n, the i'th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i'th number.
Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
5
6 -4 8 -2 3
2 4 6 1 3
5
3 -2 -1 5 6
1 -3 4 11 6
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.
In the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.
题目大意:给出一个大等边三角形的边长和一个小等边三角形的边长,要求从大三角形变化为小三角形,每次可以改变一个边的长度,同时要满足三角形的形态(就是说两边之和大于第三边,两边之差小于第三边),求最少需要几步。
观察样例,发现倒过来看(从小三角形变到大三角形)比较简单,每一次都是最小的那一个边变得尽可能的大,求出最少要多少步才可以有一条边变到目标边,答案就是当前步数+2。
贪心
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> #include<ctime> #include<cstring> #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout); #define llg long long using namespace std; llg a,b,c,ans,f,t; void sor() { if (b>c) swap(b,c); if (a>b) swap(a,b); if (b>c) swap(b,c); if (a>b) swap(a,b); if (b>c) swap(b,c); if (a>b) swap(a,b); } int main() { // yyj("3"); cin>>f>>t; if (f>t) swap(f,t); if (f==t) {cout<<3; return 0;} a=b=c=f; while (1) { ans++; a=min(b+c-1,t); if (a==t) break; sor(); } cout<<ans+2; return 0; }