Codeforces Round #738 (Div. 2) A--D1
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #738 (Div. 2) A--D1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原題戳這里Codeforces Round #738 (Div. 2)
A. Mocha and Math
題目大意
-
通過對某個區間內的數進行題目所給操作
-
希望最小化序列中的最大值
思路
-
萬惡的位運算竟然出現在第一題說明只要轉個小彎就能看破
-
一個數和其他數做位與運算(以下自己結合運算特點想一想就能想通)
- 要么不變(與自己相同的數)
- 要么變小(與自己不同的數)
-
所以和別的元素相與時不虧的,所以全部與一遍就好啦
-
因為操作次數不限,所以一定能實現上述
代碼
#include<bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int n, m, t; int a[N];int main() {cin >> t;while(t --){cin >> n;for(int i = 0; i < n; i ++) cin >> a[i];int ans = a[0];for(int i = 1; i < n;i ++) ans &= a[i];cout << ans << endl;}return 0; }B. Mocha and Red and Blue
題目大意
- 序列中出現一次連續的兩個相同字符就有一個不完美度
- 求怎樣涂色才能使不完美度最小
思路
- 容易想到兩種字符盡量交錯為最優解
- 那么問題來了,怎樣能不漏的填完呢
- 正著填一遍再倒著填一遍就可以做到不漏自己也覺得很神奇
- 全是問號的情況下交錯輸出即可
代碼
#include<bits/stdc++.h>using namespace std;int n, m, t; string s;int main() {cin >> t;while(t --){cin >> n;s.clear();cin >> s;int flg = 1;for(int i = 1; i < n; i ++){if(s[i] == '?'){if(s[i - 1] == 'R') s[i] = 'B';if(s[i - 1] == 'B') s[i] = 'R';}}for(int i = n - 2; i >= 0; i --){if(s[i] == '?'){if(s[i +1] == 'R') s[i] = 'B';if(s[i + 1] == 'B') s[i] = 'R';}}if(s[0] == '?'){for(int i = 0; i < n; i ++){if(i & 1) cout << 'B';else cout << 'R';}puts("");continue;}cout << s << "\n";}return 0; }C. Mocha and HikingC. Mocha and Hiking
題目大意
-
n+1個村子由2n-1條道路相連
-
道路有兩種
- n-1條道路是從 i村到 i + 1村,所有1≤ i ≤ n-1
- a[i] 為0表示可以從i村到 i+1村,為1表示i + 1村可以到i村
-
問是否錯在一條路徑走完所有村子
思路
- 很容易發現存在連續的01序列就滿足條件
- 但是以下兩種情況需要注意某人因為這個wa了好幾發
- 11110類也可以,按順序訪問村子的情況
- 11111類也可以,先訪問n+1村再依次訪問其他
代碼
#include<bits/stdc++.h>using namespace std;const int N = 1e4 + 10;int n, m, t; int a[N];int main() {cin >> t;while(t --){cin >> n;for(int i = 1; i <= n; i ++) cin >> a[i];a[n + 1] = 1;int flg = 0, res = 1;for(int i = 1; i <= n; i ++){if(a[i] == 1);if(a[i] == 0 && a[i + 1 ]== 1){res = i;flg = 1;break;}}if(flg){for(int i = 1; i <= n; i ++){if(i == res) cout << i << " " << n + 1 << " ";else cout << i << " ";}puts("");}else if(!flg && a[1] == 1){cout << n + 1 << " ";for(int i = 1; i <= n; i ++) cout << i << " ";puts(" ");flg = 1;}else if(!flg) puts("-1");}return 0; }D1. Mocha and Diana (Easy Version)
題目大意
- 兩人分別有n個點,最初這些點中有些被連成聯通塊
- 連詞邊的操作在兩波分同時進行
- 已經在同一連通塊中兩點不能連
- 問最多可以連幾條
思路
- 與連通塊相關指向并查集
- 維護兩個并查集,記錄可連邊即可
代碼
#include<bits/stdc++.h>using namespace std;const int N = 1010;int n, m1, m2; int p[N], q[N];struct node {int x, y; }pi[N];int find1(int x) {if(p[x] != x) p[x] = find1(p[x]);return p[x]; }int find2(int x) {if(q[x] != x) q[x] = find2(q[x]);return q[x]; }int main() {cin >> n >> m1 >> m2;for(int i = 1; i <= n; i ++) p[i] = i;for(int i = 1; i <= n; i ++) q[i] = i;while(m1 --){int u, v;cin >> u >> v;p[find1(u)] = find1(v);}while(m2 --){int u, v;cin >> u >> v;q[find2(u)] = find2(v);}int cnt = 0;for(int i = 1; i <= n; i ++)for(int j = 1; j <= n; j ++){int a1, a2, a3, a4;if(i != j){a1 = find1(i), a2 = find1(j);a3 = find2(i), a4 = find2(j);if(a1 != a2 && a3 != a4){pi[cnt ++] = {i, j};p[a1] = a2;q[a3] = a4;}}}cout << cnt << "\n";for(int i = 0; i < cnt ; i ++){cout << pi[i].x << " " << pi[i].y << "\n";}return 0; }總結 耐心,細心,塌心,靜心
總結
以上是生活随笔為你收集整理的Codeforces Round #738 (Div. 2) A--D1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刚找工作闲谈
- 下一篇: 定时/计数器应用3——脉冲宽度的测量