Codeforces Round #738 (Div. 2)

A. Mocha and Math

任何两个数都可以&,所以把所有的数&一遍就是最小的

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 110;
 4 int a[maxn];
 5 int main() {
 6   int T, ans, n;
 7   cin >> T;
 8   while (T--) {
 9     cin >> n;
10     ans = 0;
11     for (int i = 1; i <= n; i++) {
12       cin >> a[i];
13       ans = max(ans, a[i]);
14     }
15     for (int i = 1; i <= n; i++) {
16       ans &= a[i];
17     }
18     cout << ans << endl;
19   }
20   return 0;
21 }

B. Mocha and Red and Blue

我的乱搞:找到第一个非'?'字符,往前交替填充,每个字符与后面的不同。此后遇到空位就往后交替填充,每个字符与前面的不同。特判全空的序列。

在草稿纸上分类比划一下发现的确是最优解。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 string s;
 5 int main() {
 6   int T;
 7   cin >> T;
 8   while (T--) {
 9     cin >> n >> s;
10     int i;
11     for (i = 0; i < n; i++) {
12       if (s[i] != '?') {
13         if (i == 0) break;
14         for (int k = i - 1; k >= 0; k--) s[k] = s[k + 1] == 'R' ? 'B' : 'R';
15         break;
16       }
17       if (i == n - 1) {
18         s[0] = 'B';
19         i = 1;
20         break;
21       }
22     }
23     
24     for (i; i < n; i++) {
25       if (s[i] == '?') s[i] = s[i - 1] == 'R' ? 'B' : 'R';
26     }
27     cout << s << endl;
28   }
29   return 0;
30 }

C. Mocha and Hiking

观察题中的图结构,其特点是1~n依次连接,它们作为答案的子序列一定是连续的,能够一笔画的无非一下三种情况:

1.n -> n+1

2.n+1 -> 1

3. x -> n+1 -> x+1

分别判断即可

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n, a[10004];
 4 
 5 int main() {
 6   int T;
 7   cin >> T;
 8   while (T--) {
 9     cin >> n;
10     for (int i = 1; i <= n; i++) cin >> a[i];
11     if (a[n] == 0) {
12       for (int i = 1; i <= n + 1; i++) cout << i << " ";
13       cout << "\n";
14       continue;
15     }
16     else if (a[1] == 1) {
17       cout << n + 1 << " ";
18       for (int i = 1; i <= n; i++) cout << i << " ";
19       cout << "\n";
20       continue;
21     }
22     else {
23       int flag = 0;
24       for (int i = 1; i <= n; i++) 
25         if (!a[i] && a[i + 1]) {flag = i; break;}
26       if (flag) {
27         for (int i = 1; i <= flag; i++) cout << i << " ";
28         cout << n + 1 << " ";
29         for (int i = flag + 1; i <= n; i++) cout << i << " ";
30         cout << "\n";
31         continue;
32       }
33     }
34     cout << -1 << endl;
35   }
36   return 0;
37 }

D1. Mocha and Diana (Easy Version)

看到题目描述就联想到最小生成树(克鲁斯卡尔算法)。。。然后写一写竟然就过了。。。

不过我不知道为什么是对的。。。(我太弱了)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 3e5 + 10;
 4 int x, y, n, m1, m2, f1[maxn], f2[maxn];
 5 
 6 void init() { for (int i = 1; i <= n; i++) f1[i] = f2[i] = i;}
 7 int find1(int x) { return x == f1[x] ? f1[x] : f1[x] = find1(f1[x]); }
 8 bool check1(int x, int y) { return find1(x) == find1(y); }
 9 void merge1(int x, int y) { f1[find1(x)] = find1(y); }
10 int find2(int x) { return x == f2[x] ? f2[x] : f2[x] = find2(f2[x]); }
11 bool check2(int x, int y) { return find2(x) == find2(y); }
12 void merge2(int x, int y) { f2[find2(x)] = find2(y); }
13 
14 int main() {
15   cin >> n >> m1 >> m2; init();
16   for (int i = 1; i <= m1; i++) { cin >> x >> y; merge1(x, y); }
17   for (int i = 1; i <= m2; i++) { cin >> x >> y; merge2(x, y); }
18   vector<pair<int, int> > ans;
19   for (int i = 1; i <= n; i++)
20     for (int j = i + 1; j <= n; j++)
21       if (find1(i) != find1(j) && find2(i) != find2(j)) { merge1(i, j); merge2(i, j); ans.push_back(make_pair(i, j)); }
22   cout << ans.size() << endl;
23   for (int i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].first, ans[i].second);
24   return 0;
25 }

待续

 

 

上一篇:Codeforces Round #738 (Div. 2)


下一篇:leetcode 738. 单调递增的数字