Codeforces Round #374 (Div. 2)

A. One-dimensional Japanese Crossword
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).

Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.

The example of encrypting of a single row of japanese crossword.

Help Adaltik find the numbers encrypting the row he drew.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' — to white square in the row that Adaltik drew).

Output

The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.

The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.

Examples
input
3
BBW
output
1
2
input
5
BWBWB
output
3
1 1 1
input
4
WWWW
output
0
input
4
BBBB
output
1
4
input
13
WBBBBWWBWBBBW
output
3
4 1 3
Note

The last sample case correspond to the picture in the statement.

水题啊!!!

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int n,B[200],L=0;
 5 string s;
 6 
 7 int main()
 8 {
 9     int ans=0;
10     cin>>n;
11     cin>>s;
12     for(int i=0;i<n;i++)
13     {
14         if(s[i]=='B')
15             B[L]++;
16         if(s[i]=='W'&&B[L]!=0) L++; 
17     }
18     if(s[n-1]=='B') L++;
19     cout<<L<<endl;
20     for(int i=0;i<L;i++)
21         cout<<B[i]<<" ";
22     return 0;
23 }
 
 
B. Passwords
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.

Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.

Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.

Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).

Input

The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.

The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.

The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.

Output

Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.

Examples
input
5 2
cba
abc
bb1
abC
ABC
abc
output
1 15
input
4 100
11
22
1
2
22
output
3 4
Note

Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2and only then the right one, spending 4 seconds at all.

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int n,k;
 7 string S[200],pass;
 8 
 9 bool cmp(string a,string b)
10 {
11     if(a.size()!=b.size()) return a.size()<b.size();
12     return a==pass&&b!=pass;
13 }
14 
15 bool cmp_2(string a,string b)
16 {
17     if(a.size()!=b.size()) return a.size()<b.size();
18     return a!=pass&&b==pass;
19 }
20 
21 int main()
22 {
23     cin>>n>>k;
24     for(int i=0;i<n;i++)
25         cin>>S[i];
26     cin>>pass;
27     sort(S,S+n,cmp);
28     int w=0,ans1=0;
29     for(int i=0;i<n;i++)
30     {
31         ans1++;
32         if(S[i]!=pass) 
33         {
34             w++;
35             if(w==k) ans1+=5,w=0;
36         }
37         else break;
38     }
39     sort(S,S+n,cmp_2);
40     w=0;
41     int ans2=0;
42     for(int i=0;i<n;i++)
43     {
44         ans2++;
45         if(S[i]!=pass) 
46         {
47             w++;
48             if(w==k) ans2+=5,w=0;
49         }
50         else break;
51     }
52     cout<<ans1<<" "<<ans2;
53     return 0;
54 }

这次水题怎么这么水,不过第一次提交的时候还是不小心忘记把测试输出给删了。。。

C. Journey
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.

Input

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

Output

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

Examples
input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5

第三题还没来得急提交,比赛就结束了。。。明天来测试一下
 1 #include<iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 struct Edge
 9 {
10     int v,w;
11 };
12 struct Map
13 {
14     bool *U,*V;
15     int p,w;
16 };
17 int n,m,T,ans=0;
18 vector<Edge> P[5001];
19 bool f;
20 
21 bool cmp(Edge a,Edge b)
22 {
23     return a.w<b.w;
24 }
25 
26 int main()
27 {
28     cin>>n>>m>>T;
29     for(int i=0;i<m;i++)
30     {
31         int u,v,t;
32         cin>>u>>v>>t;
33         P[u].push_back(Edge{v,t});
34         P[v].push_back(Edge{u,t});
35     }
36     for(int i=1;i<=n;i++) sort(P[i].begin(),P[i].end(),cmp);
37     queue<Map> Q;
38     bool u[5001],v[5001];
39     memset(u,0,sizeof(u));memset(v,0,sizeof(v));
40     Q.push(Map{u,v,1,T});
41     while(!Q.empty())
42     {
43         Map E=Q.front();
44         Q.pop();
45         f=0;
46         bool u[5001],v[5001];
47         memcpy(u,E.U,sizeof(bool)*5001);
48         memcpy(v,E.V,sizeof(bool)*5001);
49         if(u[E.p]) v[E.p]=1;
50         u[E.p]=1;
51         for(int i=0;i<P[E.p].size();i++)
52         {
53             if(!v[P[E.p][i].v]&&E.w>=P[E.p][i].w)
54             {
55                 E.w-=P[E.p][i].w;
56                 E.p=P[E.p][i].v;
57                 Q.push({u,v,E.p,E.w});
58                 f=1;
59             }
60         }
61         if(!f)
62         {
63             for(int i=1;i<=n;i++)
64                 if(E.U[i]) ans++;
65             cout<<ans<<endl;
66             for(int i=1;i<=n;i++)
67                 if(E.U[i]) cout<<i<<" ";
68             return 0;
69         }
70     }
71     return 0;
72 }

 果然有是没理解题目→_→

总之就是BFS+DP

pre数组用来存上一个城市

 1 #include<iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #include<queue>
 5 #include<utility>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 typedef pair<int,int> PP;
10 struct Edge
11 {
12     int v,w;
13 };
14 int n,m,T,ans=0;
15 int DP[5001][5001];
16 bool vis[5001][5001]={0};
17 int pre[5001][5001];
18 vector<Edge> P[5001];
19 
20 void bfs()
21 {
22     queue<PP> Q;
23     Q.push({1,1});
24     vis[1][1]=1;
25     DP[1][1]=0;
26     while(!Q.empty())
27     {
28         PP q=Q.front();
29         Q.pop();
30         int x=q.first;
31         int y=q.second;
32         vis[x][y]=0;
33         for(int i=0;i<P[x].size();i++)
34         {
35             int v=P[x][i].v;
36             int w=P[x][i].w;
37             if(DP[x][y]+w<=T&&DP[v][y+1]>DP[x][y]+w)
38             {
39                 DP[v][y+1]=DP[x][y]+w;
40                 pre[v][y+1]=x;
41                 if(!vis[v][y+1])
42                 {
43                     vis[v][y+1]=1;
44                     Q.push({v,y+1});
45                 }
46             }
47         }
48     }
49 }
50 
51 int j;
52 
53 void print()
54 {
55     vector<int>res;
56     res.push_back(n);
57     while(pre[n][j])
58     {
59         res.push_back(pre[n][j]);
60         n=pre[n][j];
61         j--;
62     }
63     int ans=res.size();
64     printf("%d
",ans);
65     for(int i=ans-1;i>=0;i--)
66     {
67         printf("%d ",res[i]);
68     }
69 }
70 
71 int main()
72 {
73     cin>>n>>m>>T;
74     memset(DP,0x7f,sizeof(DP));
75     for(int i=0;i<m;i++)
76     {
77         int u,v,t;
78         cin>>u>>v>>t;
79         P[u].push_back(Edge{v,t});
80     }
81     bfs();
82     
83     for(int i=n;i>=1;i--)
84         if(DP[n][i]<=T)
85         {
86             j=i;
87             break;
88         }
89     print();
90     return 0;
91 }
原文地址:https://www.cnblogs.com/InWILL/p/5925281.html