Codeforces Round #612 (Div. 2)/A/B/C/D

A. Angry Students
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's a walking tour day in SIS.Winter, so tt groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.

Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P":

  • "A" corresponds to an angry student
  • "P" corresponds to a patient student

Such string describes the row from the last to the first student.

Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index ii in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.

Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.

Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.

Input

The first line contains a single integer tt — the number of groups of students (1t1001≤t≤100). The following 2t2t lines contain descriptions of groups of students.

The description of the group starts with an integer kiki (1ki1001≤ki≤100) — the number of students in the group, followed by a string sisi, consisting of kiki letters "A" and "P", which describes the ii-th group of students.

Output

For every group output single integer — the last moment a student becomes angry.

Examples
input
Copy
1
4
PPAP
output
Copy
1
input
Copy
3
12
APPAPPPAPPPP
3
AAP
3
PPA
output
Copy
4
1
0
Note

In the first test, after 11 minute the state of students becomes PPAA. After that, no new angry students will appear.

In the second tets, state of students in the first group is:

  • after 11 minute — AAPAAPPAAPPP
  • after 22 minutes — AAAAAAPAAAPP
  • after 33 minutes — AAAAAAAAAAAP
  • after 44 minutes all 1212 students are angry

In the second group after 11 minute, all students are angry.

 找一下最长的连续P序列即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define pii pair<int,int>
 6 #define pb push_back
 7 #define all(v) (v.begin(),v.end())
 8 #define mp make_pair
 9 
10 int main()
11 {
12     int n,t;
13     string str;
14     int f[111];
15     cin>>t;
16     while(t--){
17         cin>>n>>str;
18         int tmp=0,ans=0;
19         for(int i=n-1;i>=0;--i){
20             if(str[i]=='A'){
21                 ans=max(ans,tmp);
22                 tmp=0;
23             }else{
24                 tmp++;
25             }
26         }
27         cout<<ans<<endl;
28     }
29     return 0;
30 }
View Code

codeforces-1287-B

B. Hyperset
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.

Polina came up with a new game called "Hyperset". In her game, there are nn cards with kk features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with k=4k=4.

Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.

Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.

Input

The first line of each test contains two integers nn and kk (1n15001≤n≤1500, 1k301≤k≤30) — number of cards and number of features.

Each of the following nn lines contains a card description: a string consisting of kk letters "S", "E", "T". The ii-th character of this string decribes the ii-th feature of that card. All cards are distinct.

Output

Output a single integer — the number of ways to choose three cards that form a set.

Examples
input
Copy
3 3
SET
ETS
TSE
output
Copy
1
input
Copy
3 4
SETE
ETSE
TSES
output
Copy
0
input
Copy
5 4
SETT
TEST
EEET
ESTE
STES
output
Copy
2
Note

In the third example test, these two triples of cards are sets:

  1. "SETT", "TEST", "EEET"
  2. "TEST", "ESTE", "STES"

N^2遍历所有得(i,j)然后可以根据(i,j)字符串构造出来第三个T字符串,然后查找一下是否有这个T存在即可,注意最后答案要/3因为会重复出现。

查找时用了一个map,复杂度是O(N2log(N))这个样子,但是很慢,几乎跑满了。

下来测试了下是查找时候得锅,利用中括号下标查找很慢,换成M.count(T)飞快了就。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define pii pair<int,int>
 6 #define pb push_back
 7 #define all(v) (v.begin(),v.end())
 8 #define mp make_pair
 9 
10 string e[1505];
11 int n,K;
12 map<string,int>M;
13 
14 int main()
15 {
16     ios::sync_with_stdio(false);
17     int S='S'+'E'+'T';
18     long long ans=0;
19     cin>>n>>K;
20     for(int i=1;i<=n;++i)cin>>e[i],M[e[i]]++;
21     for(int i=1;i<=n;++i){
22         for(int j=i+1;j<=n;++j){
23             if(i==j)continue;
24             string T;
25             for(int o=0;o<K;++o){
26                 if(e[i][o]==e[j][o]) T+=e[i][o];
27                 else{
28                         T+=(char)(S-e[i][o]-e[j][o]);
29                 }
30             }
31             ans+=M[T];
32         }
33     }cout<<ans/3<<endl;
34     return 0;
35 }
View Code

codeforces-1287-C

C. Garland
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.

Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 22). For example, the complexity of 1 4 2 3 5 is 22 and the complexity of 1 3 5 7 6 4 2 is 11.

No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.

Input

The first line contains a single integer nn (1n1001≤n≤100) — the number of light bulbs on the garland.

The second line contains nn integers p1, p2, , pnp1, p2, …, pn (0pin0≤pi≤n) — the number on the ii-th bulb, or 00 if it was removed.

Output

Output a single number — the minimum complexity of the garland.

Examples
input
Copy
5
0 5 0 2 3
output
Copy
2
input
Copy
7
1 0 0 5 0 0 2
output
Copy
1
Note

In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5,4)(5,4) and (2,3)(2,3) are the pairs of adjacent bulbs that have different parity.

In the second case, one of the correct answers is 1 7 3 5 6 4 2.

 总算遇到了自己擅长得东西了,直接dp,f[i][j][k]表示当前处于下标i处,当前位置的数字奇偶性为j,且剩余未放置的数字中有k个是奇数,由于剩余未放置的数字中偶数得个数可以根据现有状态推得,所以没必要多开一维了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define pii pair<int,int>
 6 #define pb push_back
 7 #define all(v) (v.begin(),v.end())
 8 #define mp make_pair
 9 
10 int n,a[111],f[105][2][105],tot[105],pre[105];
11 int main()
12 {
13     memset(f,inf,sizeof(f));
14     cin>>n;
15     int ji=0,ou=0,sum=0;
16     for(int i=1;i<=n;++i) {
17             cin>>a[i];
18             tot[a[i]]++;
19             if(a[i]==0){
20                 pre[i]=pre[i-1]+1;
21                 a[i]=-1;
22             }else{
23                 pre[i]=pre[i-1];
24                 a[i]%=2;
25             }
26     }
27     for(int i=1;i<=n;++i){
28         if(!tot[i]){
29             if(i%2==0)ou++;
30             else ji++;
31         }
32     }//cout<<ji<<' '<<ou<<endl;
33     sum=ji+ou;
34     if(a[1]!=-1) f[1][a[1]][ji]=0;
35     else{
36         if(ji>0) f[1][1][ji-1]=0;
37         if(ou>0) f[1][0][ji]=0;
38     }
39     for(int i=1;i<=n;++i){
40         for(int j=0;j<=1;++j){
41             for(int k1=0;k1<=ji;++k1){
42                 int k2=sum-k1-pre[i];
43                 if(f[i][j][k1]!=inf){
44                     if(a[i+1]==0){
45                         if(j==0){
46                             f[i+1][0][k1]=min(f[i+1][0][k1],f[i][j][k1]);
47                         }else{
48                             f[i+1][0][k1]=min(f[i+1][0][k1],f[i][j][k1]+1);
49                         }
50                     }else if(a[i+1]==1){
51                         if(j==1){
52                              f[i+1][1][k1]=min(f[i+1][1][k1],f[i][j][k1]);
53                         }else{
54                             f[i+1][1][k1]=min(f[i+1][1][k1],f[i][j][k1]+1);
55                         }
56                     }else{
57                         //ji
58                         if(k1>0){
59                             if(j==1){
60                                f[i+1][1][k1-1]=min(f[i+1][1][k1-1],f[i][j][k1]);
61                             }else{
62                                f[i+1][1][k1-1]=min(f[i+1][1][k1-1],f[i][j][k1]+1);
63                             }
64                         }
65                         //ou
66                          if(k2>0){
67                             if(j==0){
68                                f[i+1][0][k1]=min(f[i+1][0][k1],f[i][j][k1]);
69                             }else{
70                                f[i+1][0][k1]=min(f[i+1][0][k1],f[i][j][k1]+1);
71                             }
72                         }
73                     }
74                 }
75             }
76         }
77     }
78     //cout<<f[2][1][1]<<endl;
79     int ans=inf;
80     for(int k1=0;k1<=ji;++k1)ans=min(ans,min(f[n][0][k1],f[n][1][k1]));
81     cout<<ans<<endl;
82     return 0;
83 }
View Code

codeforces-1287-D

D. Numbers on Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aiai written on it. For each vertex ii, Evlampiy calculated cici — the number of vertices jj in the subtree of vertex ii, such that aj<aiaj<ai.

Illustration for the second example, the first integer is aiai and the integer in parentheses is cici

After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of cici, but he completely forgot which integers aiai were written on the vertices.

Help him to restore initial integers!

Input

The first line contains an integer n(1n2000)(1≤n≤2000) — the number of vertices in the tree.

The next nn lines contain descriptions of vertices: the ii-th line contains two integers pipi and cici (0pin0≤pi≤n; 0cin10≤ci≤n−1), where pipi is the parent of vertex ii or 00 if vertex ii is root, and cici is the number of vertices jj in the subtree of vertex ii, such that aj<aiaj<ai.

It is guaranteed that the values of pipi describe a rooted tree with nn vertices.

Output

If a solution exists, in the first line print "YES", and in the second line output nn integers aiai (1ai109)(1≤ai≤109). If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all aiai are between 11 and 109109.

If there are no solutions, print "NO".

Examples
input
Copy
3
2 0
0 2
2 0
output
Copy
YES
1 2 1 
input
Copy
5
0 1
1 3
2 1
3 0
2 0
output
Copy
YES
2 3 2 1 2



借鉴了https://blog.csdn.net/qq_45458915/article/details/103856822得思路。

假设当前节点得所有儿子已经处理好为了一个数列,数列的下标代表这个元素的rank,根据当前的c[u]就可以确定应该把u插入到这个序列的哪个位置。

所以只要有解的话用1-n这n个数恰好能构成一个解,题目把范围说那么大纯属唬人>_<

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define pii pair<int,int>
 6 #define pb push_back
 7 #define all(v) (v.begin(),v.end())
 8 #define mp make_pair
 9 
10 const int maxn=2010;
11 int n,c[maxn],r[maxn],p,root,ok=1;
12 vector<int>son[maxn];
13 vector<int> dfs(int u){
14     vector<int>res;
15     for(auto v:son[u]){
16         vector<int>tp=dfs(v);
17         res.insert(res.end(),tp.begin(),tp.end());
18     }
19 
20     if(c[u]>res.size()){cout<<"NO"<<endl;exit(0);}
21     res.insert(res.begin()+c[u],u);
22     return res;
23 }
24 int main()
25 {
26     cin>>n;
27     for(int i=1;i<=n;++i){
28         cin>>p>>c[i];
29         if(p==0)root=i;
30         son[p].pb(i);
31     }
32     vector<int>res = dfs(root);
33     cout<<"YES"<<endl;
34     for(int i=0;i<n;++i)r[res[i]]=i+1;
35     for(int i=1;i<=n;++i)cout<<r[i]<<' ';cout<<endl;
36     return 0;
37 }
View Code
原文地址:https://www.cnblogs.com/zzqc/p/12154912.html