Codeforces Round #648 (Div. 2) A-F

Codeforces Round #648 (Div. 2)
A

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 100+10;
const int mod = 1e9+7;

int t, n, m, a, x[N], y[N];

int main(){
	IO;
	cin >> t;
	while(t--){
		cin >> n >> m;
		memset(x, 0, sizeof(int)*(n+1));
		memset(y, 0, sizeof(int)*(m+1));
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				cin >> a;
				if(a == 1) x[i] = 1, y[j] = 1;
			}
		}
		int cntx = 0, cnty = 0;
		for(int i = 0; i < n; i++){
			if(x[i] == 0) cntx++;
		}
		for(int j = 0; j < m; j++){
			if(y[j] == 0) cnty++;
		}
		int ans = min(cntx, cnty);
		if(ans%2) cout << "Ashish\n";
		else cout << "Vivek\n";
	}
	return 0;
}

B

如果同时有类型0和类型1的时,一定可以
否则只需要判断是否是排序好的

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 500+10;
const int mod = 1e9+7;

int t, n;
struct node{
	int a, b;
}e[N];


int main(){
	IO;
	cin >> t;
	while(t--){
		cin >> n;
		int cnt1 = 0, cnt2 = 0;
		for(int i = 0; i < n; i++){
			cin >> e[i].a;
		}
		for(int i = 0; i < n; i++){
			cin >> e[i].b;
			if(e[i].b) cnt1++;
			else cnt2++;
		}
		int flag = 1;
		for(int i = 1; i < n; i++){
			if(e[i].a < e[i-1].a){
				flag = 0;
			}
		}
		if(cnt1 && cnt2){
			flag = 1;
		}
		if(flag) cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

C

每个数匹配到与他相等的数都有两种移动方式,向左移和向右移,记录数对移动步数的贡献。 定义cnt[2*n]  i < n, cnt[i]表示向右移i步的数的数量,cnt[i+n]表示向左移i步的数的数量
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 2e5+10;
const int mod = 1e9+7;

int n, a[N], b[N], k[2*N];


int main(){
	IO;
	cin >> n;
	int x;
	for(int i = 0; i < n; i++){
		cin >> x;
		a[x] = i;
	}
	for(int i = 0; i < n; i++){
		cin >> x;
		b[x] = i;
	}
	for(int i = 1; i <= n; i++){
		if(a[i] >= b[i]){
			k[a[i]-b[i]]++;
			k[n-a[i]+b[i]+n]++;
		}
		else{
			k[b[i]-a[i]+n]++;
			k[a[i]-b[i]+n]++;
		}
	}
	int ans = 0;
	for(int i = 0; i < 2*n; i++){
		ans = max(ans, k[i]);
	}
	cout << ans << "\n";
	return 0;
}

D 暴力

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 100+10;
const int mod = 1e9+7;

int t, n, m;
char a[N][N];
vector< pair<int, int> > p;
int vis[N][N], d[N][N];

void dfs(int x, int y){
	if(vis[x][y]) return ;
	vis[x][y] = 1;
	if(x-1 >= 0 && a[x-1][y] != '#') {
		d[x-1][y] = 1;
		dfs(x-1, y);
	}
	if(x+1 < n && a[x+1][y] != '#') {
		d[x+1][y] = 1;
		dfs(x+1, y);
	}
	if(y-1 >= 0 && a[x][y-1] != '#') {
		d[x][y-1] = 1;
		dfs(x, y-1);
	}
	if(y+1 < m && a[x][y+1] != '#'){
		d[x][y+1] = 1;
		dfs(x, y+1);
	}
}

int main(){
	IO;
	cin >> t;
	while(t--){
		p.clear();
		cin >> n >> m;
		for(int j = 0; j < n; j++){
			for(int k = 0; k < m; k++)
				vis[j][k] = 0;
		}
		int flag = 1;
		for(int i = 0; i < n; i++)
			cin >> a[i];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(a[i][j] == 'B'){
					if(i-1 >= 0){
						if(a[i-1][j] == 'G') flag = 0;
						else if(a[i-1][j] != 'B')a[i-1][j] = '#';
					}
					if(i+1 < n){
						if(a[i+1][j] == 'G') flag = 0;
						else if(a[i+1][j] != 'B')a[i+1][j] = '#';
					}
					if(j-1 >= 0){
						if(a[i][j-1] == 'G') flag = 0;
						else if(a[i][j-1] != 'B')a[i][j-1] = '#';
					}
					if(j+1 < m){
						if(a[i][j+1] == 'G') flag = 0;
						else if(a[i][j+1] != 'B')a[i][j+1] = '#';
					}
				}
				else if(a[i][j] == 'G') p.push_back(mk(i, j));
			}
		}
		if(flag == 0){
			cout << "No\n";
			continue;
		}
		memset(d, -1, sizeof(d));
		if(a[n-1][m-1] != '#'){
			d[n-1][m-1] = 1;
			dfs(n-1, m-1);
		}
		for(int i = 0; i < p.size(); i++){
			int x = p[i].first, y = p[i].second;
			if(d[x][y] == -1){
				flag = 0; break;
			}
		}
		if(flag == 0){
			cout << "No\n";
		}
		else cout << "Yes\n";
	}
	return 0;
}

E

当n>=3,k<=3时,max(1, k-2) = 1, 选3个最优
当k=4时,max(1, k-2) = 2,假设取三个最优为110,再加一个后,第一位不会变成1,因为第一位最多一个,达不到两个,k > 3时,解小于等于k==3的解,所以nnn枚举就好

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 500+10;
const int mod = 1e9+7;

int n;
ll a[N];


int main(){
	IO;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	ll ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = i; j < n; j++){
			for(int k = j; k < n; k++){
				ans = max(ans, a[i] | a[j] | a[k]);
			}
		}
	}
	cout << ans << "\n";
	return 0;
}

F

无论怎么交换,对称位置都是一起交换的,交换后还是对称的
所以n*n枚举对称点放置的位置

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;

int t, n, a[N], b[N];

int main(){
	IO;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 0; i < n; i++)
			cin >> a[i];
		for(int i = 0; i < n; i++)
			cin >> b[i];
		vector<bool> vis(n);
		for(int i = 0; i < n/2; i++){
			for(int j = 0; j < n; j++){
				if(!vis[j]){
					if(a[i] == b[j] && a[n-i-1] == b[n-j-1]){
						vis[j] = vis[n-j-1] = true;
						break;
					}
				}
			}
		}
		if(n%2 && a[n/2] == b[n/2]) vis[n/2] = true;
		int flag = 1;
		for(int i = 0; i < n; i++){
			if(!vis[i]) {
				flag = 0; break;
			}
		}
		if(flag) cout << "yes\n";
		else cout << "no\n";
	}
	return 0;
}

你可能感兴趣的:(codeforces)