[CF]Codeforces Round #529 (Div. 3)
[CF]Codeforces Round #529 (Div. 3)
C. Powers Of Two
Description
A positive integer xx is called a power of two if it can be represented as x=2yx=2y, where yy is a non-negative integer. So, the powers of two are 1,2,4,8,16,…1,2,4,8,16,….
You are given two positive integers nn and kk. Your task is to represent nn as the sum of exactlykk powers of two.
Input
The only line of the input contains two integers nn and kk (1≤n≤1091≤n≤109, 1≤k≤2⋅1051≤k≤2⋅105).
output
If it is impossible to represent nn as the sum of kk powers of two, print NO.
Otherwise, print YES, and then print kk positive integers b1,b2,…,bkb1,b2,…,bk such that each of bibi is a power of two, and ∑i=1kbi=n∑i=1kbi=n. If there are multiple answers, you may print any of them.
Examples
Input
9 4
Output
YES
1 2 2 4
Input
5 1
Output
NO
正确解法:
题目的意思是说,从2的次幂中找出k个,使他们相加等于n。
我刚开始想的暴搜,从1 1 1 2 2 4 8 等等一个一个找。
我们只要贪心,从大到小就好,找到一个方案就可以。
于是只要满足 n- 2的次幂 >=k-1
如果找到有k个,就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<algorithm> 8 #include<cmath> 9 #include<cstdlib> 10 using namespace std; 11 int a[40],f[200100],cnt=0; 12 int n, k; 13 int main() 14 { 15 a[0] = 1; 16 for (int i = 1; i <= 30; i++) 17 a[i] = 2 * a[i - 1]; 18 scanf("%d %d",&n,&k); 19 for (int i = 30; i >= 0; i--) 20 { 21 while (n - a[i] >= k - 1 &&n&&k) 22 { 23 n = n - a[i]; 24 f[++cnt] = a[i]; 25 k--; 26 } 27 } 28 if (n==0) 29 { 30 cout << "YES" << endl; 31 for (int i = cnt; i>1; i--) 32 printf("%d ",f[i]); 33 printf("%d\n",f[1]); 34 } 35 else cout << "NO" << endl; 36 return 0; 37 } 38
D. Circular Dance
Description
There are nn kids, numbered from 11 to nn, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as p1p1, p2p2, ..., pnpn (all these numbers are from 11 to nnand are distinct, so pp is a permutation). Let the next kid for a kid pipi be kid pi+1pi+1 if i<ni<n and p1p1 otherwise. After the dance, each kid remembered two kids: the next kid (let's call him xx) and the next kid for xx. Each kid told you which kids he/she remembered: the kid iiremembered kids ai,1ai,1 and ai,2ai,2. However, the order of ai,1ai,1 and ai,2ai,2 can differ from their order in the circle.
You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.
Input
The first line of the input contains one integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of the kids.
The next nn lines contain 22 integers each. The ii-th line contains two integers ai,1ai,1 and ai,2ai,2 (1≤ai,1,ai,2≤n,ai,1≠ai,21≤ai,1,ai,2≤n,ai,1≠ai,2) — the kids the ii-th kid remembered, given in arbitrary order.
output
Print nn integers p1p1, p2p2, ..., pnpn — permutation of integers from 11 to nn, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists.
Examples
Input
5
3 5
1 4
2 4
1 5
2 3
Output
3 2 4 1 5
Input
3
2 3
3 1
1 2
Output
3 1 2
正确解法:
题目是说,有n个小朋友转圈,每个小朋友只记得自己后面的两个小朋友,让你找出来小朋友排列的正确次序。
搜索把,我不太喜欢这种排列问题。真要我写可能会卡死。
(这个小朋友记得的第一个人,(记得的后面的两个人其中之一))如果是(这个小朋友记得的第二个人),那么这个第一个人就在这个小朋友后面。
很明显两种情况。
排列嘛,无论从哪个小朋友开始都可以,后来我发现一个问题,如果你找的最后一个人是第一个小朋友,那么这个圈就结束了。如果不是,就不符合情况。
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<algorithm> 8 #include<cmath> 9 #include<cstdlib> 10 using namespace std; 11 int n, a[200100][3],b[201000]; 12 int flag = 0; 13 void dfs(int x, int c) 14 { 15 if (x>=n) 16 { 17 if (flag==0&&c==b[0]) 18 { 19 for (int i = 0; i < n - 1; i++) 20 printf("%d ", b[i]); 21 printf("%d\n", b[n - 1]); 22 flag = 1; 23 } 24 return ; 25 } 26 if (a[a[c][0]][0] == a[c][1] || a[a[c][0]][1] == a[c][1]) 27 { 28 b[x+1] = a[c][0]; 29 dfs(x + 1, b[x+1]); 30 } 31 if (a[a[c][1]][0] == a[c][0] || a[a[c][1]][1] == a[c][0]) 32 { 33 b[x+1] = a[c][1]; 34 dfs(x + 1, b[x+1]); 35 } 36 } 37 int main() 38 { 39 scanf("%d",&n); 40 for (int i = 1; i <= n; i++) 41 scanf("%d %d",&a[i][0],&a[i][1]); 42 b[0] = 1; 43 dfs(0, 1); 44 45 return 0; 46 } 47