codeforces #197 div.2解题报告
Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.
The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.
You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.
The first line contains a non-empty string s — the sum Xenia needs to count. Strings contains no spaces. It only contains digits and characters "+". Besides, strings is a correct sum of numbers 1, 2 and 3. Strings is at most 100 characters long.
Print the new sum that Xenia can count.
3+2+1
1+2+3
1+1+3+1+3
1+1+1+3+3
2
2
水题1道,签到题。
#include<stdio.h> #include<string.h> int main() { int a[4],l,i,c; char s[100]; while(scanf("%s",s)!=EOF) { a[1]=0,a[2]=0,a[3]=0; l=strlen(s);c=0; for(i=0;i<l;i++) { if(s[i]!='+') { a[s[i]-'0']++;c++; } } while(a[1]--) { if(c!=1) printf("1+"); else printf("1\n"); c--; } while(a[2]--) { if(c!=1) printf("2+"); else printf("2\n"); c--; } while(a[3]--) { if(c!=1) printf("3+"); else printf("3\n"); c--; } } return 0; }
Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 throughn in the clockwise order. The ringroad traffic is one way and also is clockwise.
Xenia has recently moved into the ringroad house number 1. As a result, she's gotm things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less thani. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.
The first line contains two integers n andm (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line containsm integers a1, a2, ..., am(1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.
Print a single integer — the time Xenia needs to complete all tasks.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin, cout streams or the%I64d specifier.
4 3 3 2 3
6
4 3 2 3 3
2
题目大意:Xenia住在一个城市中的一个环形街道上,街道里有n个房子。Xenia家是1号房子。Xenia有m个有序列的任务就是要去其他房子办事。每过个房子就要消耗单位1的时间。要求她办完事要多久。直接模拟即可。
#include<stdio.h> #include<string.h> int main() { int n,m; int s; __int64 t,k; while(scanf("%d %d",&n,&m)!=EOF) { t=0; s=1; while(m--) { scanf("%I64d",&k); if(k>=s) t+=k-s; else t+=n-s+k; s=k; } printf("%I64d\n",t); } return 0; }
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total ofm weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goesi-th should be different from the (i + 1)-th weight for any i(1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia laym weights on the scales or to say that it can't be done.
The first line contains a string consisting of exactly ten zeroes and ones: thei-th (i ≥ 1) character in the line equals "1" if Xenia hasi kilo weights, otherwise the character equals "0". The second line contains integerm (1 ≤ m ≤ 1000).
In the first line print "YES", if there is a way to putm weights on the scales by all rules. Otherwise, print in the first line "NO". If you can putm weights on the scales, then print in the next linem integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
0000000101 3
YES 8 10 8
1000000000 2
NO
题目大意:这个妹子有1g到10g的砝码和一个天平。这个妹子要求1开始把某个砝码放左盘上让左盘的重量大于又盘。然后又放某个砝码到右盘让右盘质量大于左盘。但是相邻的砝码不能放得一样。
输入第一串0000000101表示这个妹子只有8g和10g的两种砝码。3表示只有3部。
输出YES表示存在符合规则的。并输出。例 8 10 8;而不是8 10 10;因为连续的砝码质量不能一样。
思路直接做暴力搜索。从小的砝码搜到大的即可。
#include<stdio.h> int p[1024]; int m; char s[10]; int dfs(int n,int b) { int i; if(n>m) return 1; for(i=0;i<10;i++) { p[n]=i+1; if(s[i]-'0') { if(n%2) { if(b+p[n]>0&&p[n]!=p[n-1]) { if(dfs(n+1,b+p[n])) return 1; } } else { if(b-p[n]<0&&p[n]!=p[n-1]) if(dfs(n+1,b-p[n])) return 1; } } } return 0; } int main() { int b,i; while(scanf("%s %d",s,&m)!=EOF) { if(dfs(1,0)) { printf("YES\n"); for(i=1;i<m;i++) { printf("%d ",p[i]); } printf("%d\n",p[i]); } else printf("NO\n"); } return 0; }
Xenia the beginner programmer has a sequence a, consisting of2n non-negative integers:a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequencea. At the second iteration, Xenia writes the bitwiseexclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element isv.
Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations(1, 2, 3, 4) → (1 or 2 = 3, 3 or 4 = 7) → (3 xor 7 = 4). The result is v = 4.
You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additionalm queries. Each query is a pair of integersp, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new valuev for the new sequence a.
The first line contains two integers n andm (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains2n integersa1, a2, ..., a2n(0 ≤ ai < 230). Each of the nextm lines contains queries. The i-th line contains integers pi, bi(1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — thei-th query.
Print m integers — the i-th integer denotes value v for sequencea after the i-th query.
2 4 1 6 3 5 1 4 3 4 1 2 1 2
1 3 3 3
这题也很简单,但是要用到一个经典的数据结构,树。当然也有人说线段树。不过怎样都可以。
先把所有子叶节点初始化,向上建立一棵树。每个节点都是当前的解。这也有点像dp。
每次改变某个子叶节点,只需向上分析。重新修改树中的部分节点即可。无需对整棵树都修改。
#include<stdio.h> #include<string.h> #include<math.h> int a[262144]; int flag,l; int main() { int m,n,i,p,d,j; while (scanf("%d %d",&n,&m)!=EOF) { memset(a,0,sizeof(a)); l=(int)pow(2,n); for(i=l;i<2*l;i++) { scanf("%d",&a[i]); } flag=1; for(i=l/2;i>0;i=i/2) { for(j=i;j<2*i;j++) { if(flag) { a[j]=a[j*2]|a[j*2+1]; } else { a[j]=a[j*2]^a[j*2+1]; } } flag=!flag; } for(i=0;i<m;i++) { flag=1; scanf("%d %d",&p,&d); a[l+p-1]=d; for(j=(l+p-1)/2;j>0;j=j/2) { if(flag) { a[j]=a[j*2]|a[j*2+1]; } else { a[j]=a[j*2]^a[j*2+1]; } flag=!flag; } printf("%d\n",a[1]); } } }