Codeforces Round #624 (Div. 3)

A.

A. Add Odd or Subtract Even
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two positive integers a and b.

In one move, you can change a in the following way:

Choose any positive odd integer x (x>0) and replace a with a+x;
choose any positive even integer y (y>0) and replace a with a−y.
You can perform as many such operations as you want. You can choose the same numbers x and y in different moves.

Your task is to find the minimum number of moves required to obtain b from a. It is guaranteed that you can always obtain b from a.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

Then t test cases follow. Each test case is given as two space-separated integers a and b (1≤a,b≤109).

Output
For each test case, print the answer — the minimum number of moves required to obtain b from a if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain b from a.

Example
inputCopy
5
2 3
10 10
2 4
7 4
9 3
outputCopy
1
0
2
2
1
Note
In the first test case, you can just add 1.

In the second test case, you don't need to do anything.

In the third test case, you can add 1 two times.

In the fourth test case, you can subtract 4 and add 1.

In the fifth test case, you can just subtract 6.

思路

  分情况讨论,a > b 时, 差值是奇数要操作两次, 反之一次。

  a = b 不用操作

  a < b 时, 差值是奇数操作一次, 反之两次

CODE

 1 #include <bits/stdc++.h>
 2 #define dbg(x) cout << #x << "=" << x << endl
 3 #define eps 1e-8
 4 #define pi acos(-1.0)
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 template<class T>inline void read(T &res)
10 {
11     char c;T flag=1;
12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
14 }
15 
16 namespace _buff {
17     const size_t BUFF = 1 << 19;
18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
19     char getc() {
20         if (ib == ie) {
21             ib = ibuf;
22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
23         }
24         return ib == ie ? -1 : *ib++;
25     }
26 }
27 
28 int qread() {
29     using namespace _buff;
30     int ret = 0;
31     bool pos = true;
32     char c = getc();
33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
34         assert(~c);
35     }
36     if (c == '-') {
37         pos = false;
38         c = getc();
39     }
40     for (; c >= '0' && c <= '9'; c = getc()) {
41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
42     }
43     return pos ? ret : -ret;
44 }
45 
46 int main()
47 {
48     int t;
49     scanf("%d",&t);
50     for ( int i = 1; i <= t; ++i ) {
51         int a, b;
52         scanf("%d %d",&a, &b);
53         int ans = 0;
54         if(a > b) {
55             int temp = a - b;
56             if(temp % 2) {
57                 ans += 2;
58             }
59             else {
60                 ans++;
61             }
62         }
63         else if(a == b) {
64             ans = 0;
65         }
66         else {
67             int temp = b - a;
68             if(temp % 2) {
69                 ans++;
70             }
71             else {
72                 ans += 2;
73             }
74         }
75         cout << ans << endl;
76     }
77     return 0;
78 }
View Code

B.

B. WeirdSort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a of length n.

You are also given a set of distinct positions p1,p2,…,pm, where 1≤pi<n. The position pi means that you can swap elements a[pi] and a[pi+1]. You can apply this operation any number of times for each of the given positions.

Your task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps.

For example, if a=[3,2,1] and p=[1,2], then we can first swap elements a[2] and a[3] (because position 2 is contained in the given set p). We get the array a=[3,1,2]. Then we swap a[1] and a[2] (position 1 is also contained in p). We get the array a=[1,3,2]. Finally, we swap a[2] and a[3] again and get the array a=[1,2,3], sorted in non-decreasing order.

You can see that if a=[4,1,2,3] and p=[3,2] then you cannot sort the array.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

Then t test cases follow. The first line of each test case contains two integers n and m (1≤m<n≤100) — the number of elements in a and the number of elements in p. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100). The third line of the test case contains m integers p1,p2,…,pm (1≤pi<n, all pi are distinct) — the set of positions described in the problem statement.

Output
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print "NO".

Example
inputCopy
6
3 2
3 2 1
1 2
4 2
4 1 2 3
3 2
5 1
1 2 3 4 5
1
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
5 2
2 1 2 3 3
1 4
outputCopy
YES
NO
YES
YES
NO
YES

思路

  因为要保证非递减, 所以只要反着成一个非递增即可, 可以考虑反着dfs一遍, 如果一个 a[i] < a[i-1] 且 i-1 ∈ p 就可以换位置.

CODE

  1 #include <bits/stdc++.h>
  2 #define dbg(x) cout << #x << "=" << x << endl
  3 #define eps 1e-8
  4 #define pi acos(-1.0)
  5 
  6 using namespace std;
  7 typedef long long LL;
  8 
  9 template<class T>inline void read(T &res)
 10 {
 11     char c;T flag=1;
 12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
 13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
 14 }
 15 
 16 namespace _buff {
 17     const size_t BUFF = 1 << 19;
 18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
 19     char getc() {
 20         if (ib == ie) {
 21             ib = ibuf;
 22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
 23         }
 24         return ib == ie ? -1 : *ib++;
 25     }
 26 }
 27 
 28 int qread() {
 29     using namespace _buff;
 30     int ret = 0;
 31     bool pos = true;
 32     char c = getc();
 33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
 34         assert(~c);
 35     }
 36     if (c == '-') {
 37         pos = false;
 38         c = getc();
 39     }
 40     for (; c >= '0' && c <= '9'; c = getc()) {
 41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
 42     }
 43     return pos ? ret : -ret;
 44 }
 45 
 46 int n,m;
 47 int a[107], p[107];
 48 
 49 bool check() {
 50     for ( int i = 1; i < n; ++i ) {
 51         if(a[i] > a[i+1]) {
 52             return 0;
 53         }
 54     }
 55     return 1;
 56 }
 57 
 58 bool belong(int x) {
 59     for ( int i = 1; i <= m; ++i ) {
 60         if(x == p[i]) {
 61             return 1;
 62         }
 63     }
 64     return 0;
 65 }
 66 
 67 void dfs(int x) {
 68     if(x < 0) {
 69         return;
 70     }
 71     for ( int i = x; i <= n; ++i ) {
 72         if(a[i] < a[i-1]) {
 73             if(belong(i-1)) {
 74                 swap(a[i], a[i - 1]);
 75                 dfs(i - 1);
 76             }
 77             else {
 78                 return;
 79             }
 80         }
 81     }
 82 }
 83 
 84 int main()
 85 {
 86     int t;
 87     scanf("%d",&t);
 88     while(t--) {
 89         memset(a, 0, sizeof(a));
 90         memset(p, 0, sizeof(p));
 91         scanf("%d %d",&n, &m);
 92         for ( int i = 1; i <= n; ++i ) {
 93             scanf("%d",&a[i]);
 94         }
 95         for ( int i = 1; i <= m; ++i ) {
 96             scanf("%d",&p[i]);
 97         }
 98         for ( int i = n; i >= 1; --i ) {
 99             if(a[i] < a[i-1]) {
100                 dfs(i);
101             }
102         }
103         if(check()) puts("YES");
104         else puts("NO");
105     }
106     return 0;
107 }
View Code

C.

C. Perform the Combo
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in s. I.e. if s="abca" then you have to press 'a', then 'b', 'c' and 'a' again.

You know that you will spend m wrong tries to perform the combo and during the i-th try you will make a mistake right after pi-th button (1≤pi<n) (i.e. you will press first pi buttons right and start performing the combo from the beginning). It is guaranteed that during the m+1-th try you press all buttons right and finally perform the combo.

I.e. if s="abca", m=2 and p=[1,3] then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.

Your task is to calculate for each button (letter) the number of times you'll press it.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

Then t test cases follow.

The first line of each test case contains two integers n and m (2≤n≤2⋅105, 1≤m≤2⋅105) — the length of s and the number of tries correspondingly.

The second line of each test case contains the string s consisting of n lowercase Latin letters.

The third line of each test case contains m integers p1,p2,…,pm (1≤pi<n) — the number of characters pressed right during the i-th try.

It is guaranteed that the sum of n and the sum of m both does not exceed 2⋅105 (∑n≤2⋅105, ∑m≤2⋅105).

It is guaranteed that the answer for each letter does not exceed 2⋅109.

Output
For each test case, print the answer — 26 integers: the number of times you press the button 'a', the number of times you press the button 'b', …, the number of times you press the button 'z'.

Example
inputCopy
3
4 2
abca
1 3
10 5
codeforces
2 8 3 2 9
26 10
qwertyuioplkjhgfdsazxcvbnm
20 10 1 2 3 5 10 5 9 4
outputCopy
4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0
2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2
Note
The first test case is described in the problem statement. Wrong tries are "a", "abc" and the final try is "abca". The number of times you press 'a' is 4, 'b' is 2 and 'c' is 2.

In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press 'c' is 9, 'd' is 4, 'e' is 5, 'f' is 3, 'o' is 9, 'r' is 3 and 's' is 1.

思路

  记录一下每个位置需要重新输入的次数和一个字母出现的次数, 遍历一遍字符串, 到当前字符 a[i] 时更新 ai 出现的次数和录入的次数即可;

  如果当前点需要重输, 更新 ai 录入的次数 += 当前点重输的次数 * ai 当前点出现的次数.

CODE

 1 #include <bits/stdc++.h>
 2 #define dbg(x) cout << #x << "=" << x << endl
 3 #define eps 1e-8
 4 #define pi acos(-1.0)
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 template<class T>inline void read(T &res)
10 {
11     char c;T flag=1;
12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
14 }
15 
16 namespace _buff {
17     const size_t BUFF = 1 << 19;
18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
19     char getc() {
20         if (ib == ie) {
21             ib = ibuf;
22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
23         }
24         return ib == ie ? -1 : *ib++;
25     }
26 }
27 
28 int qread() {
29     using namespace _buff;
30     int ret = 0;
31     bool pos = true;
32     char c = getc();
33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
34         assert(~c);
35     }
36     if (c == '-') {
37         pos = false;
38         c = getc();
39     }
40     for (; c >= '0' && c <= '9'; c = getc()) {
41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
42     }
43     return pos ? ret : -ret;
44 }
45 
46 const int maxn = 2e5 + 7;
47 
48 int t, n, m;
49 int tot[30];
50 char a[maxn];
51 int sum[maxn][30];
52 
53 int main()
54 {
55     scanf("%d",&t);
56     while(t--) {
57         scanf("%d %d", &n, &m);
58         memset(tot, 0, sizeof(tot));
59         map<int, int> mp;
60         map<int, int> cnt;
61         getchar();
62         scanf("%s",a+1);
63         for ( int i = 1; i <= n; ++i ) {
64             mp[i] = 0;
65         }
66         for ( int i = 1; i <= m; ++i ) {
67             int x;
68             scanf("%d",&x);
69             mp[x]++;
70         }
71         for ( int i = 1; i <= n; ++i ) {
72             tot[a[i] - 'a' + 1]++;
73             cnt[a[i] - 'a' + 1]++;
74             if(mp[i]) {
75                 for ( int j = 1; j <= 26; ++j ) {
76                     tot[j] = tot[j] + mp[i] * cnt[j];
77                 }
78             }
79         }
80         for ( int i = 1; i <= 26; ++i ) {
81             if(i == 1) {
82                 printf("%d",tot[i]);
83             }
84             else {
85                 printf(" %d",tot[i]);
86             }
87         }
88         printf("
");
89     }
90     return 0;
91 }
View Code

D.

D. Three Integers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three integers a≤b≤c.

In one move, you can add +1 or −1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.

You have to perform the minimum number of such operations in order to obtain three integers A≤B≤C such that B is divisible by A and C is divisible by B.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

The next t lines describe test cases. Each test case is given on a separate line as three space-separated integers a,b and c (1≤a≤b≤c≤104).

Output
For each test case, print the answer. In the first line print res — the minimum number of operations you have to perform to obtain three integers A≤B≤C such that B is divisible by A and C is divisible by B. On the second line print any suitable triple A,B and C.

Example
inputCopy
8
1 2 3
123 321 456
5 10 15
15 18 21
100 100 101
1 22 29
3 19 38
6 30 46
outputCopy
1
1 1 3
102
114 228 456
4
4 8 16
6
18 18 18
1
100 100 100
7
1 22 22
2
1 19 38
8
6 24 48

思路

  暴力预处理出2e4内所有数的因子然后枚举即可

CODE

 1 #include <bits/stdc++.h>
 2 #define dbg(x) cout << #x << "=" << x << endl
 3 #define eps 1e-8
 4 #define pi acos(-1.0)
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 template<class T>inline void read(T &res)
10 {
11     char c;T flag=1;
12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
14 }
15 
16 namespace _buff {
17     const size_t BUFF = 1 << 19;
18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
19     char getc() {
20         if (ib == ie) {
21             ib = ibuf;
22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
23         }
24         return ib == ie ? -1 : *ib++;
25     }
26 }
27 
28 int qread() {
29     using namespace _buff;
30     int ret = 0;
31     bool pos = true;
32     char c = getc();
33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
34         assert(~c);
35     }
36     if (c == '-') {
37         pos = false;
38         c = getc();
39     }
40     for (; c >= '0' && c <= '9'; c = getc()) {
41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
42     }
43     return pos ? ret : -ret;
44 }
45 
46 const int maxn = 2e4 + 7;
47 
48 int t, a, b, c;
49 
50 set<int> s[maxn];
51 
52 int main()
53 {
54     for ( int i = 1; i <= 2e4; ++i ) {
55         for ( int j = 1; j * j <= i; ++j ) {
56             if(i % j == 0) {
57                 s[i].insert(j);
58                 s[i].insert(i/j);
59             } 
60         }
61     }
62     scanf("%d",&t);
63     while(t--) {
64         scanf("%d %d %d",&a, &b, &c);
65         set<int>::iterator it1;
66         set<int>::iterator it2;
67         int ans = 0x3f3f3f3f;
68         int aa, bb, cc;
69         for ( int i = 1; i <= 2e4; ++i ) {
70             int tc = abs(c - i);
71             for( it1 = s[i].begin(); it1 != s[i].end(); ++it1 ) {
72                 int tb = abs(*it1 - b);
73                 for ( it2 = s[*it1].begin(); it2 != s[*it1].end(); ++it2) {
74                     int ta = abs(*it2 - a);
75                     // dbg(ta);
76                     // dbg(tb);
77                     // dbg(tc);
78                     if(ta + tb + tc < ans) {
79                         ans = ta + tb + tc;
80                         aa = *it2, bb = *it1, cc = i;
81                     }
82                 }
83             }
84         }
85         cout << ans << endl;
86         printf("%d %d %d
", aa, bb, cc);
87     }
88     return 0;
89 }
View Code

E.

E. Construct the Binary Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d.

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. The depth of the vertex v is the length of the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. The binary tree is such a tree that no vertex has more than 2 children.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains two integers n and d (2≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.

It is guaranteed that the sum of n and the sum of d both does not exceed 5000 (∑n≤5000,∑d≤5000).

Output
For each test case, print the answer.

If it is impossible to construct such a tree, print "NO" (without quotes) in the first line. Otherwise, print "{YES}" in the first line. Then print n−1 integers p2,p3,…,pn in the second line, where pi is the parent of the vertex i. Note that the sequence of parents you print should describe some binary tree.

Example
inputCopy
3
5 7
10 19
10 18
outputCopy
YES
1 2 1 3
YES
1 2 3 3 9 9 2 1 6
NO
Note
Pictures corresponding to the first and the second test cases of the example:

思路

  先把所有点变成一套链, 记录总值, 如果总值大了就依次上移, 更新总值.

  最后给所有非根节点找爸爸, 如果所有非根节点都找得到就YES, 找不到或者总值不等于 d 就NO

CODE

  1 #include <bits/stdc++.h>
  2 #define dbg(x) cout << #x << "=" << x << endl
  3 #define eps 1e-8
  4 #define pi acos(-1.0)
  5 
  6 using namespace std;
  7 typedef long long LL;
  8 
  9 template<class T>inline void read(T &res)
 10 {
 11     char c;T flag=1;
 12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
 13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
 14 }
 15 
 16 namespace _buff {
 17     const size_t BUFF = 1 << 19;
 18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
 19     char getc() {
 20         if (ib == ie) {
 21             ib = ibuf;
 22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
 23         }
 24         return ib == ie ? -1 : *ib++;
 25     }
 26 }
 27 
 28 int qread() {
 29     using namespace _buff;
 30     int ret = 0;
 31     bool pos = true;
 32     char c = getc();
 33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
 34         assert(~c);
 35     }
 36     if (c == '-') {
 37         pos = false;
 38         c = getc();
 39     }
 40     for (; c >= '0' && c <= '9'; c = getc()) {
 41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
 42     }
 43     return pos ? ret : -ret;
 44 }
 45 
 46 const int maxn = 5e3 + 7;
 47 
 48 int t, n, d;
 49 int fa[maxn], son[maxn];
 50 int depth[maxn], sz[maxn];
 51 
 52 int main()
 53 {
 54     scanf("%d",&t);
 55     while(t--) {
 56         scanf("%d %d",&n, &d);
 57         int sum = 0;
 58         int ok  = 1;
 59         for ( int i = 1; i <= n; ++i ) {
 60             sum += (i-1);
 61         }
 62         //dbg(sum);
 63         memset(sz, 0, sizeof(sz));
 64         memset(depth, 0, sizeof(depth));
 65         memset(son, 0, sizeof(son));
 66         sz[1] = 1;
 67         for ( int i = 1; i <= n; ++i ) {
 68             depth[i] = depth[i - 1] + 1;
 69             sz[depth[i]]--;
 70             //dbg(sum - n + i);
 71             if(d <= sum - n - 1 + i) {
 72                 if(sz[depth[i] - 1]) {
 73                     sum -= (n + 1 - i);
 74                     //dbg(sum);
 75                     sz[depth[i] - 1]--;
 76                     sz[depth[i]]++;
 77                     depth[i]--;
 78                 }
 79             }
 80             sz[depth[i] + 1] += 2;
 81         }
 82         if(sum != d) {
 83             puts("NO");
 84             continue;
 85         }
 86         for ( int i = 2; i <= n; ++i ) {
 87             fa[i] = 0;
 88             for ( int j = 1; j < i; ++j ) {
 89                 if(son[j] < 2 && depth[i] == depth[j] + 1) {
 90                     son[j]++;
 91                     fa[i] = j;
 92                     break;
 93                 }
 94             }
 95             if(!fa[i]) {
 96                 ok = 0;
 97                 break;
 98             }
 99         }
100         if(!ok) {
101             puts("NO");
102         }
103         else {
104             puts("YES");
105             for ( int i = 2; i <= n; ++i ) {
106                 if( i == 2 ) {
107                     printf("%d",fa[i]);
108                 }
109                 else {
110                     printf(" %d",fa[i]);
111                 }
112             }
113         }
114         printf("
");
115     }
116     return 0;
117 }
View Code
#include <bits/stdc++.h>
#define dbg(x) cout << #x << "=" << x << endl
#define eps 1e-8
#define pi acos(-1.0)

using namespace std;
typedef long long LL;

template<class T>inline void read(T &res)
{
    char c;T flag=1;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
    while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

namespace _buff {
    const size_t BUFF = 1 << 19;
    char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
    char getc() {
        if (ib == ie) {
            ib = ibuf;
            ie = ibuf + fread(ibuf, 1, BUFF, stdin);
        }
        return ib == ie ? -1 : *ib++;
    }
}

int qread() {
    using namespace _buff;
    int ret = 0;
    bool pos = true;
    char c = getc();
    for (; (< '0' || c > '9') && c != '-'; c = getc()) {
        assert(~c);
    }
    if (== '-') {
        pos = false;
        c = getc();
    }
    for (; c >= '0' && c <= '9'; c = getc()) {
        ret = (ret << 3) + (ret << 1) + (^ 48);
    }
    return pos ? ret : -ret;
}

const int maxn = 5e3 + 7;

int t, n, d;
int fa[maxn], son[maxn];
int depth[maxn], sz[maxn];

int main()
{
    scanf("%d",&t);
    while(t--) {
        scanf("%d %d",&n, &d);
        int sum = 0;
        int ok  = 1;
        for ( int i = 1; i <= n; ++) {
            sum += (i-1);
        }
        //dbg(sum);
        memset(sz, 0, sizeof(sz));
        memset(depth, 0, sizeof(depth));
        memset(son, 0, sizeof(son));
        sz[1] = 1;
        for ( int i = 1; i <= n; ++) {
            depth[i] = depth[- 1] + 1;
            sz[depth[i]]--;
            //dbg(sum - n + i);
            if(<= sum - n - 1 + i) {
                if(sz[depth[i] - 1]) {
                    sum -= (+ 1 - i);
                    //dbg(sum);
                    sz[depth[i] - 1]--;
                    sz[depth[i]]++;
                    depth[i]--;
                }
            }
            sz[depth[i] + 1] += 2;
        }
        if(sum != d) {
            puts("NO");
            continue;
        }
        for ( int i = 2; i <= n; ++) {
            fa[i] = 0;
            for ( int j = 1; j < i; ++) {
                if(son[j] < 2 && depth[i] == depth[j] + 1) {
                    son[j]++;
                    fa[i] = j;
                    break;
                }
            }
            if(!fa[i]) {
                ok = 0;
                break;
            }
        }
        if(!ok) {
            puts("NO");
        }
        else {
            puts("YES");
            for ( int i = 2; i <= n; ++) {
                if( i == 2 ) {
                    printf("%d",fa[i]);
                }
                else {
                    printf(" %d",fa[i]);
                }
            }
        }
        printf(" ");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/orangeko/p/12392913.html