Codeforces Round # 612 (Div. 2) The first four questions problem solution

The topic of the game quite interesting people, all the magic became blue.

As well as the title of the picture is particularly interesting.

Did not play at night, open the virtual contest to play, it will first three road, I was too dishes.

Finally, looking at the problem solution complement the Fourth Way.

Game portal

 

A. Angry Students

Subject to the effect: t have teams of students, each student has two states, angry (A) or angry (P). (Why are the words of angry wearing Christmas hats wow) will forward all those angry people throw a snowball was thrown people will become angry and will throw snowballs. Each team people ask you the last time a student becomes angry.

This question is to count the longest continuous 'P' course, provided that there are people left angry.

code show as below:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(int x = l; x <= r; x++)
 7 #define repd(x, r, l) for(int x = r; x >= l; x--)
 8 #define clr(x, y) memset(x, y, sizeof(x))
 9 #define all(x) x.begin(), x.end()
10 #define pb push_back
11 #define mp make_pair
12 #define MAXN 105
13 #define fi first
14 #define se second
15 #define SZ(x) ((int)x.size())
16 using namespace std;
17 typedef long long ll;
18 typedef vector<int> vi;
19 typedef pair<int, int> pii;
20 const int INF = 1 << 30;
21 const int p = 1000000009;
22 int lowbit(int x){ return x & (-x);}
23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}
24 
25 char st[MAXN];
26 
27 int main(){
28     int t;
29     scanf("%d", &t);
30     while(t--){
31         int n;
32         scanf("%d", &n);
33         scanf("%s", st);
34         int flag = 0, ans = 0, sum = 0;
35         rep(i, 0, n - 1){
36             if(st[i] == 'A'){
37                 flag = 1;
38                 sum = 0;
39             }
40             if(st[i] == 'P' && flag){
41                 sum++;
42                 ans = max(ans, sum);
43             }
44         }
45         printf("%d\n", ans);
46     }
47     return 0;
48 }
View Code-A

 

B.Hyperset

Title effect: n-cards, each card has k features, each feature has three ( 'S', 'E' , 'T'), allows you to select a set composed of three cards, three cards that each feature are all equal or all different. As examples of the three "SETT", "TEST", "EEET" is set, "TEST", "ESTE" , "STES" is set and asked to elect a total of several sets.

This data is very small problem, n less than 1500, all the states directly instead ternary number is within the range longlong, how to do anything.

My solution is extremely complex, discrete counts for the transfer to digital.

Enumeration first two cards i and j, which can be obtained directly state the third card (if the first two the same, and the third is the same, if different from the first two, the third is the rest of that) the answer in line with the number of the states plus the card.

Long and stinking code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #include <map>
 7 #define rep(x, l, r) for(int x = l; x <= r; x++)
 8 #define repd(x, r, l) for(int x = r; x >= l; x--)
 9 #define clr(x, y) memset(x, y, sizeof(x))
10 #define all(x) x.begin(), x.end()
11 #define pb push_back
12 #define mp make_pair
13 #define MAXN 1505
14 #define MAXM 35
15 #define fi first
16 #define se second
17 #define SZ(x) ((int)x.size())
18 using namespace std;
19 typedef long long ll;
20 typedef vector<int> vi;
21 typedef pair<int, int> pii;
22 const int INF = 1 << 30;
23 const int p = 1000000009;
24 int lowbit(int x){ return x & (-x);}
25 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}
26 
27 map<ll, int> Rank;
28 char st[MAXM];
29 int a[MAXN][MAXM], c[MAXN], sum[MAXN];
30 ll num[MAXN], b[MAXN];
31 
32 int judge(char ch){
33     if(ch == 'S') return 0;
34     if(ch == 'E') return 1;
35     if(ch == 'T') return 2;
36 }
37 
38 int main(){
39     int n, len;
40     scanf("%d%d", &n, &len);
41     rep(i, 1, n){
42         scanf("%s", st);
43         rep(j, 0, len - 1){
44             num[i] = num[i] * 3 + judge(st[j]);
45             a[i][j + 1] = judge(st[j]);
46         }
47         b[i] = num[i];
48     }
49     sort(b + 1, b + n + 1);
50     rep(i, 1, n) Rank[b[i]] = i;
51     rep(i, 1, n) c[i] = Rank[num[i]];
52     int ans = 0;
53     rep(i, 1, n){
54         rep(j, i + 1, n){
55             ll s = 0;
56             rep(k, 1, len){
57                 int x = (6 - a[i][k] - a[j][k]) % 3;
58                 s = s * 3 + x;    
59             }
60             if(Rank.find(s) != Rank.end()) ans += sum[Rank[s]];
61         }
62         sum[c[i]]++;
63     }
64     printf("%d\n", ans);
65     return 0;
66 }
View Code-B

 

C.Garland

Title effect: there is arrayed a [1 ... n] thereof, wherein some of the numbers are removed (removal indicated by the number 0), the number of these will now be returned to the position of the spare. Each parity sequence have a different number of adjacent pairs increases the complexity of the sequence, the number of minimum complexity requirements.

This prompts the question I was in big brother was made out of.

First we see this parity, so polite, the digital direct to throw a few more than enough to leave a film 2.

Then the remaining problem now becomes a position adjacent to the discharge 0/1 minimize complexity.

Although codeforces algorithm Tag This question mark had a greedy, but I did not get to know how corrupt this problem.

However, we found that put the number of how many will only have an impact on the latter figure, so you can use dynamic programming.

I is the number of discharge state, with the j th 1, this number is put 0/1.

Transfer the i-th divided into two vacant positions and i - 1 th spare adjacent or non-adjacent positions.

Comparative adjacent words so long as the i-th and the i - 1 number, if not adjacent to the i-th comparator and its previous and the i - 1 a and the number of it.

There are several points Note:

  1. j + i-th than the first i - 1 large number, of course, smaller than i.
  2. (I'll give convenience i = 1 when the initial value assigned) to write the initial worth, they also need to be classified, to determine first whether a vacant position in the first place the original arrangement, or even determine the number and the first digital before it contribution to answer.
  3. Finally, do not forget that if the end of the last vacant position in the original sequence is not the last, but also to calculate the contribution.

code show as below:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(int x = l; x <= r; x++)
 7 #define repd(x, r, l) for(int x = r; x >= l; x--)
 8 #define clr(x, y) memset(x, y, sizeof(x))
 9 #define all(x) x.begin(), x.end()
10 #define pb push_back
11 #define mp make_pair
12 #define MAXN 105
13 #define fi first
14 #define se second
15 #define SZ(x) ((int)x.size())
16 using namespace std;
17 typedef long long ll;
18 typedef vector<int> vi;
19 typedef pair<int, int> pii;
20 const int INF = 1 << 30;
21 const int p = 1000000009;
22 int lowbit(int x){ return x & (-x);}
23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}
24 
25 int id[MAXN], a[MAXN], dp[MAXN][MAXN][2];
26 
27 int main(){
28     int n, m = 0;
29     scanf("%d", &n);
30     int tot1 = 0;
31     rep(i, 1, n){
32         int x;
33         scanf("%d", &x);
34         if(!x){
35             m++;
36             id[m] = i;
37             a[i] = -1;
38         }
39         else{
40             a[i] = x % 2;
41             if(x % 2) tot1++;
42         }
43     }
44     int l = (n + 1) / 2 - tot1;
45     rep(i, 1, m)
46         rep(j, 0, l)
47             rep(k, 0, 1) dp[i][j][k] = INF;
48     if(id[1] == 1){
49         dp[1][0][0] = 0;
50         dp[1][1][1] = 0;
51     }
52     else{
53         dp[1][0][0] = 0 ^ a[id[1] - 1];
54         dp[1][1][1] = 1 ^ a[id[1] - 1];
55     }
56     rep(i, 2, m)
57         rep(j, 0, min(i, l))
58             rep(k, 0, 1){
59                 dp[i][j][k] = INF;
60                 if(j < k) continue;
61                 if(id[i - 1] < id[i] - 1){
62                     dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - k][0] + (0 ^ a[id[i - 1] + 1]) + (k ^ a[id[i] - 1]));
63                     if(j >= k + 1) dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - k][1] + (1 ^ a[id[i - 1] + 1]) + (k ^ a[id[i] - 1]));
64                 }
65                 else{
66                     dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - k][0] + (0 ^ k));
67                     if(j >= k + 1) dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - k][1] + (1 ^ k));
68                 }
69             }
70     int s1 = dp[m][l][0], s2 = dp[m][l][1];
71     if(id[m] != n){
72         s1 += 0 ^ a[id[m] + 1];
73         s2 += 1 ^ a[id[m] + 1];
74     }
75     int ans = min(s1, s2);
76     rep(i, 1, n - 1){
77         if(a[i] == -1 || a[i + 1] == -1) continue;
78         ans += a[i] ^ a[i + 1];
79     }
80     printf("%d\n", ans);
81     return 0;
82 }
View Code-C

 

D.Numbers on Tree

题目大意:有一颗有根树,每一个节点有一个值ai,用ci表示以i为根的子树中比i小的节点的j个数(即aj < ai)。现在给你n和c数组,让你给出满足以上条件的任意一种a的方案。

这题一开始完全不会,百度了题解后才过的:大佬的题解

我们发现一个子树整体无论变化多少,这个子树中所有节点都是满足情况的。

并且一个子树中只要大小关系保持不变,怎么变化也都是满足情况的。

那么我们只要将当前节点的子树按照原来节点的值排个序。每个节点的值直接改成它的编号,还是满足条件。

至于当前节点u,在这些节点的上面不会产生影响,只需要在编号c[u]处插入,后面的节点都忘后移就好(反正n才2000,够我们玩)。

当一个点的时候直接是1(就是c[u] + 1)了。

这边为了方便排序使用pair,表示节点的值和这个节点的数。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define rep(x, l, r) for(int x = l; x <= r; x++)
 7 #define repd(x, r, l) for(int x = r; x >= l; x--)
 8 #define clr(x, y) memset(x, y, sizeof(x))
 9 #define all(x) x.begin(), x.end()
10 #define pb push_back
11 #define mp make_pair
12 #define MAXN 2005
13 #define fi first
14 #define se second
15 #define SZ(x) ((int)x.size())
16 using namespace std;
17 typedef long long ll;
18 typedef vector<int> vi;
19 typedef pair<int, int> pii;
20 const int INF = 1 << 30;
21 const int p = 1000000009;
22 int lowbit(int x){ return x & (-x);}
23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}
24 
25 int c[MAXN], ans[MAXN], sz[MAXN];
26 vi edge[MAXN];
27 vector<pii> ve[MAXN];
28 
29 void dfs(int u){
30     ve[u].clear();
31     sz[u] = 1;
32     rep(i, 0, SZ(edge[u]) - 1){
33         int v = edge[u][i];
34         dfs(v);
35         sz[u] += sz[v];
36         rep(j, 0, SZ(ve[v]) - 1) ve[u].pb(ve[v][j]);
37     }
38     if(c[u] >= sz[u]){
39         puts("NO");
40         exit(0);
41     }
42     sort(all(ve[u]));
43     rep(i, 0, SZ(ve[u]) - 1) ve[u][i].fi = i + 1;
44     ve[u].insert(ve[u].begin() + c[u], mp(c[u] + 1, u));
45     rep(i, c[u] + 1, SZ(ve[u]) - 1) ve[u][i].fi++;
46 }
47 
48 int main(){
49     int n;
50     scanf("%d", &n);
51     int root;
52     rep(i, 1, n){
53         int fa;
54         scanf("%d%d", &fa, &c[i]);
55         if(!fa) root = i;
56         else edge[fa].pb(i);
57     }
58     dfs(root);
59     puts("YES");
60     sort(all(ve[root]));
61     rep(i, 0, SZ(ve[root]) - 1) ans[ve[root][i].se] = ve[root][i].fi;
62     rep(i, 1, n) printf("%d ", ans[i]);
63     puts("");
64     return 0;
65 }
View Code-D

Guess you like

Origin www.cnblogs.com/nblyz2003/p/12157877.html