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();
}