3.29 练习赛

//比赛时的代码感觉写得真心难看......Orz,还有一堆低级错误贴出来晒晒吧

Problem A     CodeForces 329A     Purification 

2A

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 char mat[110][110];
 4 int n;
 5 int row_Col[110], col_Row[110];
 6 
 7 bool dir_row()
 8 {
 9     int i, j;
10     bool fail;
11     for(i = 1; i <= n; ++i) {
12         fail = 1;
13         for(j = 1; j <= n; ++j) {
14             if(mat[i][j] == '.') {
15                 fail = 0;
16                 row_Col[i] = j;
17                 break;
18             }
19         }
20         if(fail)
21             return 0;
22     }
23     return 1;
24 }
25 
26 bool dir_col()
27 {
28     int i, j;
29     bool fail;
30     for(i = 1; i <= n; ++i) {
31         fail = 1;
32         for(j = 1; j <= n; ++j) {
33             if(mat[j][i] == '.') {
34                 fail = 0;
35                 col_Row[i] = j;
36                 break;
37             }
38         }
39         if(fail)
40             return 0;
41     }
42     return 1;
43 }
44 
45 
46 int main()
47 {
48     scanf("%d", &n);
49     int i, j;
50     for(i = 1; i <= n; ++i)
51         scanf("%s", mat[i] + 1);
52     bool a = dir_row();
53     if(!a && !dir_col()) {
54         printf("-1
");
55         return 0;
56     }
57     if(a) {
58         for(i = 1; i <= n; ++i) {
59             printf("%d %d
", i, row_Col[i]);
60         }
61     }
62     else {
63         for(i = 1; i <= n; ++i) {
64             printf("%d %d
", col_Row[i], i);
65         }
66     }
67 }

Wrong answer on test 4

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 char mat[110][110];
 4 int n;
 5 int row_Col[110], col_Row[110];
 6 
 7 bool dir_row()
 8 {
 9     int i, j;
10     bool fail;
11     for(i = 1; i <= n; ++i) {
12         fail = 1;
13         for(j = 1; j <= n; ++j) {
14             if(mat[i][j] == '.') {
15                 fail = 0;
16                 row_Col[i] = j;
17                 break;
18             }
19         }
20         if(fail)
21             return 0;
22     }
23     return 1;
24 }
25 
26 bool dir_col()
27 {
28     int i, j;
29     bool fail;
30     for(i = 1; i <= n; ++i) {
31         fail = 1;
32         for(j = 1; j <= n; ++j) {
33             if(mat[j][i] == '.') {
34                 fail = 0;
35                 col_Row[i] = j;
36                 break;
37             }
38         }
39         if(fail)
40             return 0;
41     }
42     return 1;
43 }
44 
45 
46 int main()
47 {
48     scanf("%d", &n);
49     int i, j;
50     for(i = 1; i <= n; ++i)
51         scanf("%s", mat[i] + 1);
52     bool a = dir_row();
53     if(!a && !dir_col()) {
54         printf("-1
");
55         return 0;
56     }
57     if(a) {
58         for(i = 1; i <= n; ++i) {
59             printf("%d %d
", i, row_Col[i]);
60         }
61     }
62     else {
63         for(i = 1; i <= n; ++i) {
64             printf("%d %d
", i, col_Row[i]);   //写反了......
65         }
66     }
67 }

Problem B     CodeForces 329B     Biridian Forest 

2A

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 char mat[1010][1010];
 4 
 5 int r, c;
 6 int s_row, s_col;
 7 queue<pair<int, int> > q;
 8 queue<int> q_dis;
 9 int dx[] = {0, 0, 1, -1};
10 int dy[] = {1, -1, 0, 0};
11 bool vis[1010][1010];
12 
13 int bfs()
14 {
15     q.push(make_pair(s_row, s_col));
16     q_dis.push(0);
17     pair<int, int> tmp;
18     vis[s_row][s_col] = 1;
19 
20     int res = 0;
21     int dis, Limit = 300000000;
22     while(!q.empty()) {
23         tmp = q.front();
24         q.pop();
25         dis = q_dis.front();
26         q_dis.pop();
27         int next_dis = dis + 1;
28         if(next_dis > Limit)
29             break;
30         int i, row, col;
31         for(i = 0; i < 4; ++i) {
32             row = tmp.first + dx[i];
33             col = tmp.second + dy[i];
34             if(!vis[row][col] && mat[row][col] != 'T' && row >= 1 && row <= r && col >= 1 && col <= c) {
35                 q.push(make_pair(row, col));
36                 q_dis.push(next_dis);
37                 vis[row][col] = 1;
38                 if(mat[row][col] == 'S') {
39                     Limit = next_dis;
40                 }
41                 else {
42                     res += (mat[row][col] - '0');
43                 }
44             }
45         }
46     }
47     return res;
48 }
49 
50 int main()
51 {
52     scanf("%d%d", &r, &c);
53     int i, j;
54     bool notFind = 1;
55     for(i = 1; i <= r; ++i) {
56         scanf("%s", mat[i] + 1);
57         for(j = 1; notFind && j <= c; ++j) {
58             if(mat[i][j] == 'E') {
59                 s_row = i;
60                 s_col = j;
61                 notFind = 0;
62                 break;
63             }
64         }
65     }
66     printf("%d
", bfs());
67 }

Wrong answer on test 9

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 char mat[1010][1010];
 4 
 5 int r, c;
 6 int s_row, s_col;
 7 queue<pair<int, int> > q;
 8 queue<int> q_dis;
 9 int dx[] = {0, 0, 1, -1};
10 int dy[] = {1, -1, 0, 0};
11 bool vis[1010][1010];
12 
13 int bfs()
14 {
15     q.push(make_pair(s_row, s_col));
16     q_dis.push(0);
17     pair<int, int> tmp;
18     vis[s_row][s_col] = 1;
19 
20     int res = 0;
21     int dis, Limit = 3000;  //以为在1000 * 1000 的图上最长的一条路是从 1 1 走到 1000 1000 最多需要走 999 × 2步,其实是忘了考虑树的情况......
22     while(!q.empty()) {
23         tmp = q.front();
24         q.pop();
25         dis = q_dis.front();
26         q_dis.pop();
27         int next_dis = dis + 1;
28         if(next_dis > Limit)
29             break;
30         int i, row, col;
31         for(i = 0; i < 4; ++i) {
32             row = tmp.first + dx[i];
33             col = tmp.second + dy[i];
34             if(!vis[row][col] && mat[row][col] != 'T' && row >= 1 && row <= r && col >= 1 && col <= c) {
35                 q.push(make_pair(row, col));
36                 q_dis.push(next_dis);
37                 vis[row][col] = 1;
38                 if(mat[row][col] == 'S') {
39                     Limit = next_dis;
40                 }
41                 else {
42                     res += (mat[row][col] - '0');
43                 }
44             }
45         }
46     }
47     return res;
48 }
49 
50 int main()
51 {
52     scanf("%d%d", &r, &c);
53     int i, j;
54     bool notFind = 1;
55     for(i = 1; i <= r; ++i) {
56         scanf("%s", mat[i] + 1);
57         for(j = 1; notFind && j <= c; ++j) {
58             if(mat[i][j] == 'E') {
59                 s_row = i;
60                 s_col = j;
61                 notFind = 0;
62                 break;
63             }
64         }
65     }
66     printf("%d
", bfs());
67 }

Problem C     CodeForces 327A     Flipping Game

1A

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 int n, ans[110], sum[110];
 4 
 5 int main()
 6 {
 7     scanf("%d", &n);
 8     int i;
 9     for(i = 1; i <= n; ++i) {
10         scanf("%d", &ans[i]);
11         sum[i] = sum[i - 1] + ans[i];
12     }
13     int j;
14     int res = 0;
15     for(i = 1; i <= n; ++i) {
16         for(j = i; j <= n; ++j) {
17             int tmp = sum[j] - sum[i - 1];
18             int Max = sum[n] - tmp;
19             tmp = j - i + 1 - tmp;
20             Max += tmp;
21             res = max(res, Max);
22         }
23     }
24     printf("%d
", res);
25 }

Problem D     CodeForces 327B     Hungry Sequence 

1A

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 int n;
 4 
 5 int main()
 6 {
 7     scanf("%d", &n);
 8     int i;
 9     printf("%d", n);
10     for(i = n + 1; i <= n + n - 1; ++i) {
11         printf(" %d", i);
12     }
13     printf("
");
14 }

Problem E     CodeForces 327C     Magic Five

3A

比赛的时候竟然忘了矩阵乘法...更别提怎么构造矩阵了...重新学习了一下矩阵乘法才把这题过掉,感觉是时候完善一下模板了

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 const __int64 mod = 1e9 + 7;
 4 char a[100010];
 5 int k;
 6 __int64 res = 0;
 7 
 8 struct mat
 9 {
10     __int64 ans[3][3];
11 };
12 
13 mat mul(mat a, mat b)
14 {
15     mat c;
16     memset(&c, 0, sizeof(c));
17     int i, j, k;
18     for(i = 1; i <= 2; ++i) {
19         for(j = 1; j <= 2; ++j) {
20             for(k = 1; k <= 2; ++k) {
21                 c.ans[i][j] += a.ans[i][k] * b.ans[k][j];
22             }
23             c.ans[i][j] %= mod;
24         }
25     }
26     return c;
27 }
28 
29 int main()
30 {
31     scanf("%s%d", &a, &k);
32     int len = strlen(a);
33     int i;
34     __int64 Base = 1;
35     for(i = 0; i < len; ++i) {
36         if(a[i] == '0' || a[i] == '5') {
37             res = (res + Base) % mod;
38         }
39         Base <<= 1;
40         Base %= mod;
41     }
42     __int64 base = Base + 1;
43     --k;
44     mat m1;
45     m1.ans[1][1] = res;
46     m1.ans[1][2] = res;
47     mat _1;
48     _1.ans[1][1] = Base;
49     _1.ans[1][2] = 0;
50     _1.ans[2][1] = 1;
51     _1.ans[2][2] = 1;
52     while(k) {
53         if(k & 1) {
54             m1 = mul(m1, _1);
55         }
56         _1 = mul(_1, _1);
57         k >>= 1;
58     }
59     printf("%I64d
", m1.ans[1][1] % mod);
60 }

Wrong answer on test 5

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 const __int64 mod = 1e9 + 7;
 4 char a[100010];
 5 int k;
 6 __int64 res = 0;
 7 
 8 int main()
 9 {
10     scanf("%s%d", &a, &k);
11     int len = strlen(a);
12     int i;
13     __int64 Base = 1;
14     for(i = 0; i < len; ++i) {
15         if(a[i] == '0' || a[i] == '5') {
16             res = (res + Base) % mod;
17         }
18         Base <<= 1;
19         Base %= mod;
20     }
21     __int64 base = Base + 1;
22     --k;
23     __int64 ans = 1;  //自己傻逼把公式推错了,后来才发现应该写矩阵快速幂......
24     while(k) {
25         if(k & 1) {
26             ans = (ans * base) % mod;
27         }
28         base = (base * base) % mod;
29         k >>= 1;
30     }
31     printf("%I64d
", res * ans % mod);
32 }

Wrong answer on test 5

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 const __int64 mod = 1e9 + 7;
 4 char a[10];  //第一次交的时候数组都没开对......2333,In the first line you're given a string a (1 ≤ |a| ≤ 105),
 5 int k;
 6 __int64 res = 0;
 7 
 8 int main()
 9 {
10     scanf("%s%d", &a, &k);
11     int len = strlen(a);
12     int i;
13     for(i = 0; i < len; ++i) {
14         if(a[i] == '0' || a[i] == '5') {
15             res = (res + (1 << i)) % mod;
16         }
17     }
18     __int64 base = (1 << len) + 1;
19     --k;
20     __int64 ans = 1;
21     while(k) {
22         if(k & 1) {
23             ans = (ans * base) % mod;
24         }
25         base = (base * base) % mod;
26         k >>= 1;
27     }
28     printf("%I64d
", res * ans % mod);
29 }

Problem F     CodeForces 322A     Ciel and Dancing 

1A

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int n, m;
 7     scanf("%d%d", &n, &m);
 8     printf("%d
", n + m - 1);
 9     int i;
10     for(i = 1; i <= m; ++i)
11         printf("1 %d
", i);
12     for(i = 2; i <= n; ++i)
13         printf("%d 1
", i);
14 }

Problem G     CodeForces 322B     Ciel and Flowers 

4A

//这题竟然是4A...最后AC的思路是先让尽可能的让 3 种不同颜色的花组成花束,但是这时有可能拆开一些花束让同色的花之间自由组合最终能得到更多的花束,注意到拆开 3 束三色花束和不拆对最终的结果没有影响,所以枚举一下拆开 0, 1, 2 束三色花束就可以了

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int a, b, c;
 4 __int64 res = 0, res_2 = 0;
 5 
 6 int main()
 7 {
 8     scanf("%d%d%d", &a, &b, &c);
 9     int base = min(a, min(b, c));
10     int i;
11     for(i = 0; i <= 2 && base >= 0; ++i, --base) {
12         __int64 temp = (a - base) / 3 + (b - base) / 3 + (c - base) / 3 + base;
13         res = max(res, temp);
14     }
15     printf("%I64d
", res);
16 }

Wrong answer on test 34

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int a, b, c;
 4 __int64 res = 0, res_2 = 0;
 5 
 6 int main()
 7 {
 8     scanf("%d%d%d", &a, &b, &c);
 9     int base = min(a, min(b, c));
10     int i;
11     for(i = 0; i <= 2; ++i, --base) {  //没考虑到可能把三色花束拆完的情况......
12         __int64 temp = (a - base) / 3 + (b - base) / 3 + (c - base) / 3 + base;
13         res = max(res, temp);
14     }
15     printf("%I64d
", res);
16 }

Wrong answer on test 10

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int a, b, c;
 4 __int64 res = 0;
 5 
 6 int main()
 7 {
 8     scanf("%d%d%d", &a, &b, &c);
 9     res += min(a, min(b, c));  //愚蠢的贪心法......
10     a -= res, b -= res, c -= res;
11     res += (a / 3);
12     res += (b / 3);
13     res += (c / 3);
14     printf("%I64d
", res);
15 }
 
Wrong answer on test 6
 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int a, b, c;
 4 __int64 res = 0;
 5 
 6 int main()
 7 {
 8     scanf("%d%d%d", &a, &b, &c);
 9     res += (a / 3);  //愚蠢的贪心法......
10     a %= 3;
11     res += (b / 3);
12     b %= 3;
13     res += (c / 3);
14     c %= 3;
15     res += min(a, min(b, c));
16     printf("%I64d
", res);
17 }

Problem H     CodeForces 320A     Magic Numbers

2A

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int n;
 4 
 5 int main()
 6 {
 7     scanf("%d", &n);
 8     int num_4 = 0, last;
 9     bool fail = 0;
10     while(n) {
11         last = n % 10;
12         n /= 10;
13         if((last != 1) && (last != 4)) {
14             fail = 1;
15             break;
16         }
17         if(last == 4) {
18             ++num_4;
19             if(num_4 > 2) {
20                 fail = 1;
21                 break;
22             }
23         }
24         else {
25             num_4 = 0;
26         }
27     }
28     if(fail || last == 4) {
29         puts("NO");
30     }
31     else {
32         puts("YES");
33     }
34 }
 
Wrong answer on test 8
 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int n;
 4 
 5 int main()
 6 {
 7     scanf("%d", &n);
 8     int num_4 = 0, last;
 9     bool fail = 0;
10     while(n) {
11         last = n % 10;
12         n /= 10;
13         if((last != 1) && (last != 4)) {
14             fail = 1;
15             break;
16         }
17         if(last == 4) {
18             ++num_4;
19             if(num_4 > 2) {
20                 fail = 1;
21                 break;
22             }
23         }
24         else {
25             num_4 = 0;
26         }
27     }
28     if(fail) {  //只发现连续的 4 超过两个是不合法的,但是没考虑到 4 在最高位的情况也是不合法的......
29         puts("NO");
30     }
31     else {
32         puts("YES");
33     }
34 }

//H 题还有一种做法是:观察出 1 是 14 的前缀,14 是 144 的前缀,所以可以优先考虑 144 是否可以匹配,否则考虑 14 是否可以匹配,否则再考虑 1 是否可以匹配,否则原串不满足Magic Numbers的要求。

Problem I     CodeForces 320B     Ping-Pong (Easy Version)

2A

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int tot;
 4 pair<int, int> Set[110];
 5 int n, l, r;
 6 bool vis[110];
 7 
 8 inline bool IN(int pos, int index)
 9 {
10     if(Set[index].first < pos && pos < Set[index].second)
11         return 1;
12     return 0;
13 }
14 
15 bool dfs(int index)
16 {
17     if(index == r)
18         return 1;
19     vis[index] = 1;
20     int i;
21     for(i = 1; i <= tot; ++i) {
22         if(!vis[i] && (IN(Set[index].first, i) || IN(Set[index].second, i))) {
23             if(dfs(i))
24                 return 1;
25         }
26     }
27     return 0;
28 }
29 
30 int main()
31 {
32     scanf("%d", &n);
33     int i;
34     tot = 0;
35     for(i = 1; i <= n; ++i) {
36         int cmd;
37         scanf("%d%d%d", &cmd, &l, &r);
38         switch(cmd) {
39         case 1:
40             Set[++tot] = make_pair(l, r);
41             break;
42         case 2:
43             memset(vis, 0, sizeof(vis));
44             printf(dfs(l)? "YES
": "NO
");
45             break;
46         }
47     }
48 }

Wrong answer on test 4

 1 #include "bitsstdc++.h"
 2 using namespace std;
 3 int tot;
 4 pair<int, int> Set[110];
 5 int n, l, r;
 6 bool vis[110];
 7 
 8 inline bool IN(int pos, int index)
 9 {
10     if(Set[index].first < pos && pos < Set[index].second)
11         return 1;
12     return 0;
13 }
14 
15 bool dfs(int index)
16 {
17     if(index == r)
18         return 1;
19     vis[index] = 1;
20     int i;
21     for(i = 1; i <= tot; ++i) {
22         if(!vis[i] && (IN(Set[index].first, i) || IN(Set[index].second, i))) {
23             if(dfs(i))
24                 return 1;
25         }
26     }
27     return 0;
28 }
29 
30 int main()
31 {
32     scanf("%d", &n);
33     int i;
34     tot = 0;
35     for(i = 1; i <= n; ++i) {
36         int cmd;
37         scanf("%d%d%d", &cmd, &l, &r);
38         switch(cmd) {
39         case 1:
40             Set[++tot] = make_pair(l, r);
41             break;
42         case 2:
43             printf(dfs(l)? "YES
": "NO
");  //没初始化 dfs 的入栈标记......
44             break;
45         }
46     }
47 }
原文地址:https://www.cnblogs.com/AC-Phoenix/p/4376235.html