Codeforces Round #738 (Div. 2)

北航出的手速场(qwq


A - Mocha and Math

题意

This day, Mocha got a sequence aa of length n. In each operation, she can select an arbitrary interval [l,r] and for all values i (0≤i≤r−l), replace al+i with al+i&ar−i at the same time, where && denotes the bitwise AND operation. This operation can be performed any number of times.

在区间内任意操作这些数,用与运算来操作,可以操作任意次

题解

我们发现,因为我们可以操作任意次,所以我们取的区间合适,在某一个位置的数都会与区间的其他数进行与操作。

同时与操作也不会增加数的大小,所以与的越多,数就越小

Code

// 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
// Code by: ING__
// 
// Edited on 2021-08-15 22:35:32

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define Endl "\n"
#define endl "\n"
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n;
int a[110];

int main(){
	cin >> T;
	while(T--){
		cin >> n;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		int ans = a[1];
		for(int i = 2; i <= n; i++){
			ans &= a[i];
		}
		cout << ans << endl;
	}
}

B - Mocha and Red and Blue

题意

有两种字符BR,在一个字符串中,用这两种字符填入?,要让这两个字符相邻的次数最小

题解

手算多种可能的情况:

BR中间有奇数个问号,有偶数个问号

BB中间有奇数个问号,有偶数个问号

计算过后发现:只要相邻的两个字符尽可能的不相同即可

Code

// 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
// Code by: ING__
// 
// Edited on 2021-08-15 22:35:35

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define Endl "\n"
#define endl "\n"
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n;
char a[110];

char zhuan(char c){
	if(c == 'R') return 'B';
	else return 'R';
}

int main(){
	cin >> T;
	while(T--){
		cin >> n;
		scanf("%s", a + 1);
		
		char b[110];
		
		int s = 0;
		
		for(int i = 1; i <= n; i++){ // 从左往右找到第一个字符,这样好从这个字符往前确定,剩下的就好办了
			if(a[i] != '?'){
				s = i;
				break;
			}
		}
		b[s] = a[s];
		for(int i = s - 1; i >= 1; i--){
			b[i] = zhuan(b[i + 1]);
		}
		for(int i = s + 1; i <= n; i++){
			if(a[i] == '?'){
				b[i] = zhuan(b[i - 1]);
			}
			else b[i] = a[i];
		}
		
		for(int i = 1; i <= n; i++){
			cout <<  b[i];
		}
		cout << endl;
	}
}

C - Mocha and Hiking

题意

给n+1个点,2n-1条边

其中n-1条边是从 i 到 i + 1点的有向边( 1 < = i < = n − 1 1<=i<=n-1 1<=i<=n1,注意没有从n到n+1的边)

另外n条边从输入里给出,1代表从 n+1 到 i,0代表从 i 到 n + 1

问能否有一种方案输出从某一个点开始经过每一个点且只经过一次的方案,并输出一种

题解

经过样例我们可以知道

1-n的点都已经连起来了,其中一种情况是,最后一个是0的话,即a[n]为0,直接顺序输出1-n+1就行

那a[n]不是0呢?我们可以画图发现:

在这里插入图片描述

只要出现01(要顺序01就可以有这样一个回路)

如果全是1呢?

在这里插入图片描述

就直接从6开始就行

所以根本就不会有-1的情况

直接写就行

Code

// 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
// Code by: ING__
// 
// Edited on 2021-08-15 22:35:39

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define Endl "\n"
#define endl "\n"
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n;
int a[10010];

int main(){
	cin >> T;
	while(T--){
		cin >> n;
		
		int flag = 1; // 全都是1
		
		for(int i = 1; i <= n; i++){
			cin >> a[i];
			if(!a[i]) flag = 0;
		}
		
		if(n == 1){
			if(a[1] == 0) cout << "1 2" << endl;
			else cout << "2 1" << endl;
			continue;
		}
		
		if(!a[n]){
			for(int i = 1; i <= n + 1; i++){
				cout << i << " ";
			}
			cout << endl;
		}
		else{
			if(flag){
				cout << n + 1 << " ";
				for(int i = 1; i <= n; i++){
					cout << i << " ";
				}
				cout << Endl;
				continue;
			}
			for(int i = 2; i <= n; i++){
				if(a[i] == 1 && a[i - 1] == 0){
					for(int j = 1; j <= i - 1; j++){
						cout << j << " ";
					}
					cout << n + 1 << " ";
					for(int j = i; j <= n; j++){
						cout << j << " ";
					}
					cout << endl;
					break;
				}
			}
		}
	}
}

D1 - Mocha and Diana (Easy Version)

题意

两个人都有n个点,给定其中一个人的m1条这些点的关系,另一个人m2条这些点的关系

问最多能连多少个边使得不会有重边(两个要同时连

题解

n=1000,直接暴力并查集判断模拟即可

记得路径压缩(有人没路径压缩t了qwq

Code

// 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
// Code by: ING__
// 
// Edited on 2021-08-15 22:35:42

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define Endl "\n"
#define endl "\n"
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int n, m1, m2;
// 结构体版
struct ufss{
	int f[2010];
	
	void init(){
		for(int i = 1; i <= n; i++){
			f[i] = i;
		}
	}
	
	int query(int p){
		if(f[p] == p) return p;
			else{
				int v = query(f[p]);
				f[p] = v;
				return v;
			}
	}

	void merge(int p1,int p2){
		int f1 = query(p1);
		int f2 = query(p2);
		if(f1 != f2){
			f[f1] = f2;
		}
	}
	
	bool pd(int p1, int p2){
		if(query(p1) == query(p2)) return true;
			else return false;
	}
    
    int count(int n){
        int ans = 0;
        for(int i = 1; i <= n; i++) if(f[i] == i) ans++;
        
	}
}u1, u2;

int main(){
	cin >> n >> m1 >> m2;
	u1.init();
	u2.init();
	
	for(int i = 1; i <= m1; i++){
		int a, b;
		cin >> a >> b;
		u1.merge(a, b);
	}
	
	for(int i = 1; i <= m2; i++){
		int a, b;
		cin >> a >> b;
		u2.merge(a, b);
	}
	
	vector<PII> ans;
	
	for(int i = 1; i <= n; i++){
		for(int j =i + 1; j <= n; j ++){
			if(!u1.pd(i, j) && !u2.pd(i, j)){
				u1.merge(i, j);
				u2.merge(i, j);
				ans.push_back({i, j});
			}
		}
	}
	
	cout << ans.size() << endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i].x << " " << ans[i].y << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值