Codeforces Round #738 (Div. 2)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #738 (Div. 2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Codeforces Round #738 (Div. 2)
文章目錄
- A
- 題解:
- 代碼:
- B
- 題意:
- 題解:
- 代碼:
- C
- 題意:
- 題解:
- 代碼:
- D1
- 題意:
- 題解:
- 代碼:
| A | Mocha and Math | |
| B | Mocha and Red and Blue | |
| C | Mocha and Hiking | |
| D1 | Mocha and Diana (Easy Version) | |
| D2 | Mocha and Diana (Hard Version) | |
| E | Mocha and Stars |
A
題解:
可以任意選區間,可以操作多次,也就是任何數都可以進行&操作,所以答案就是所有&的結果
代碼:
// Problem: A. Mocha and Math // Contest: Codeforces - Codeforces Round #738 (Div. 2) // URL: https://codeforces.com/contest/1559/problem/A // Memory Limit: 256 MB // Time Limit: 1000 ms // Data:2021-08-16 19:15:42 // By Jozky#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; template <typename T> inline void read(T& x) {T f= 1;x= 0;char ch= getchar();while (0 == isdigit(ch)) {if (ch == '-')f= -1;ch= getchar();}while (0 != isdigit(ch))x= (x << 1) + (x << 3) + ch - '0', ch= getchar();x*= f; } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } int main() {//rd_test();int t;read(t);while (t--) {int n;read(n);ll ans= 0;for (int i= 1; i <= n; i++) {int x;read(x);if (i == 1)ans= x;elseans= ans & x;}printf("%d\n", ans);}return 0;//Time_test(); }B
題意:
長度為n的字符串,由BR?三種符號組成,現在要求你將?填上B/R,使得相同符號相鄰的情況最少
題解:
找到第一個非?的字符,然后從該點開始向前向后一次填充,每次填充?時填與其相鄰的相反元素
這樣保證最優
代碼:
// Problem: B. Mocha and Red and Blue // Contest: Codeforces - Codeforces Round #738 (Div. 2) // URL: https://codeforces.com/contest/1559/problem/B // Memory Limit: 256 MB // Time Limit: 1000 ms // Data:2021-08-16 19:24:33 // By Jozky#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; template <typename T> inline void read(T& x) {T f= 1;x= 0;char ch= getchar();while (0 == isdigit(ch)) {if (ch == '-')f= -1;ch= getchar();}while (0 != isdigit(ch))x= (x << 1) + (x << 3) + ch - '0', ch= getchar();x*= f; } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } int main() {//rd_test();int t;read(t);while (t--) {int n;read(n);string s;cin >> s;int pos= n + 1;for (int i= 0; i < s.length(); i++) {if (s[i] != '?') {pos= i;break;}}for (int i= pos - 1; i >= 0; i--) {if (s[i] == '?') {if (s[i + 1] == 'R')s[i]= 'B';elses[i]= 'R';}}for (int i= pos + 1; i < s.length(); i++) {if (s[i] == '?') {if (s[i - 1] == 'R')s[i]= 'B';elses[i]= 'R';}}cout << s << endl;}//Time_test(); }C
題意:
有n+1個點,其中1~n個點,1有條到2的邊,2有條到3的邊…n-1有條到n的邊
1到n這些點與n的連邊關系題目給出,問能否將所有點全走一遍(每個點最多只能走一次)
題解:
不難發現,從1出發,是可以到n的(經過了1到n所有點),現在還剩n+1
如果點n+1能到1或者n能到n+1,那就可以順利到點n+1
或者從aia_{i}ai?可以到n+1,從n+1也可以到ai+1a_{i+1}ai+1?,這樣也行
代碼:
// Problem: C. Mocha and Hiking // Contest: Codeforces - Codeforces Round #738 (Div. 2) // URL: https://codeforces.com/contest/1559/problem/C // Memory Limit: 256 MB // Time Limit: 1000 ms // Data:2021-08-16 19:47:58 // By Jozky#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; template <typename T> inline void read(T& x) {T f= 1;x= 0;char ch= getchar();while (0 == isdigit(ch)) {if (ch == '-')f= -1;ch= getchar();}while (0 != isdigit(ch))x= (x << 1) + (x << 3) + ch - '0', ch= getchar();x*= f; } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } const int maxn= 2e4 + 9; vector<int> vec[maxn]; int a[maxn]; int main() {//rd_test();int t;read(t);while (t--) {int n;read(n);for (int i= 1; i < n; i++)vec[i].push_back(i + 1);for (int i= 1; i <= n; i++) {cin >> a[i];}if (a[1] == 1) {printf("%d", n + 1);for (int i= 1; i <= n; i++) {printf(" %d", i);}printf("\n");continue;}else if (a[n] == 0) {printf("%d", 1);for (int i= 2; i <= n + 1; i++) {printf(" %d", i);}printf("\n");continue;}bool f= 0;for (int i= 1; i < n; i++) {if (a[i] == 0 && a[i + 1] == 1) {for (int j= 1; j <= n; j++) {printf("%d ", j);if (j == i)printf("%d ", n + 1);}printf("\n");break;}}if (f == 0)continue;elseprintf("-1\n");}//Time_test(); }D1
題意:
有兩個森林,現在要求你在第一個森林加邊,第二個森林會自動進行一樣的操作,兩個森林都不允許出現環,問最多能加多少邊?
題解:
利用并查集分開維護兩個森林,對于邊u和v,如果uv在第一個森林不在一個集合內,在第二個森林也不在一個集合內,才可以加入
O(n2n^2n2)
代碼:
// Problem: D1. Mocha and Diana (Easy Version) // Contest: Codeforces - Codeforces Round #738 (Div. 2) // URL: https://codeforces.com/contest/1559/problem/D1 // Memory Limit: 256 MB // Time Limit: 1000 ms // Data:2021-08-16 20:01:37 // By Jozky#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; template <typename T> inline void read(T& x) {T f= 1;x= 0;char ch= getchar();while (0 == isdigit(ch)) {if (ch == '-')f= -1;ch= getchar();}while (0 != isdigit(ch))x= (x << 1) + (x << 3) + ch - '0', ch= getchar();x*= f; } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } const int maxn= 3000; int vis[maxn]; int fa[maxn]; int find(int x) {if (fa[x] != x)return fa[x]= find(fa[x]);return x; } void join(int u, int v) {u= find(u);v= find(v);if (u != v)fa[u]= v; } int main() {//rd_test();int n, m1, m2;cin >> n >> m1 >> m2;if (m1 == n - 1 && m2 == n - 1) {printf("0");return 0;}int tot= min(n - m1 - 1, n - m2 - 1);printf("%d\n", tot);for (int i= 1; i <= 2 * n; i++)fa[i]= i;for (int i= 1; i <= m1; i++) {int u, v;cin >> u >> v;join(u, v);}for (int i= 1; i <= m2; i++) {int u, v;cin >> u >> v;join(u + n, v + n);}// for (int i= 1; i <= n; i++) {// printf("%d ", fa[i + n]);// }// printf("\n");for (int i= 1; i <= n; i++) {for (int j= 1; j <= n; j++) {if (i == j)continue;if (find(i) != find(j)) {if (find(i + n) != find(j + n)) {printf("%d %d\n", i, j);join(i, j);join(i + n, j + n);tot--;if (tot == 0)return 0;}}}}return 0;//Time_test(); }總結
以上是生活随笔為你收集整理的Codeforces Round #738 (Div. 2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 克罗谈投资策略04_感觉与现实
- 下一篇: Unfair contest(个人做法)