[2021-9-8补题]codeforces round 704E,693F

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define se second
#define fi first
#define pb push_back
#define debug(x) cout << #x << " is " << x << endl 
vector<vector<int> > g;
int n,m;
int max_diff = 0,diff_idx = 0;
vector<int> pos;
void output(vector<int> & tmp_ans){
	cout <<"YES"<<endl;
	for(int x : tmp_ans)cout << x <<" ";
	cout <<endl;
	exit(0);
}
void check(vector<int> tmp,bool flag){
	max_diff = 0;
	vector<int> tmp_pos;
	for(int i = 0;i < n;i++){
		int cur_diff = 0;
		if(flag)tmp_pos.clear();
		for(int j = 0; j < m;j++){
			if(tmp[j] != g[i][j]){
				cur_diff++;
				if(flag)tmp_pos.pb(j);
			}
		}
		if(cur_diff > max_diff){
			if(flag){
				diff_idx = i;
				pos.clear();
				for(int x:tmp_pos){
					pos.pb(x);
				}
			}
			max_diff = cur_diff;
		}
	}
}
void solve(){
	vector<int> tmp_ans = g[0];
	check(tmp_ans,1);
	if(max_diff > 4){
		cout <<"NO"<<endl;
		return;
	}
	//debug(max_diff);
	if(max_diff <= 2)
		output(tmp_ans);
	else if(max_diff == 3){
		for(int a = 0;a < 3;a++){
			tmp_ans[pos[a]] = g[diff_idx][pos[a]];
			//heck(tmp_ans,0);
			vector<int> ppos;
			int	m_diff = 0;
			int m_idx = 0;
			for(int i = 0;i <n;i++){
				vector<int> tmp_pos;
				int diff = 0;
				for(int j = 0;j < m;j++){
					if(g[i][j] != tmp_ans[j]){
						diff++;
						tmp_pos.pb(j);
					}
				}
				if(diff > m_diff){
					m_diff = diff;
					m_idx = i;
					ppos = tmp_pos;
				}
			}
			if(m_diff <=2)output(tmp_ans);
			if(m_diff == 3){
				for(int i = 0;i < 3;i++){
					tmp_ans[ppos[i]] = g[m_idx][ppos[i]];
					check(tmp_ans,0);
					if(max_diff <= 2)output(tmp_ans);
					tmp_ans[ppos[i]] = g[diff_idx][ppos[i]];
				}
			}
			tmp_ans[pos[a]] = g[0][pos[a]];
			
		}
		
	}
	else if(max_diff == 4){
		for(int i = 0;i <= 2;i++){
			tmp_ans[pos[i]] = g[diff_idx][pos[i]];
			for(int j = i + 1;j <= 3;j++){
				tmp_ans[pos[j]] = g[diff_idx][pos[j]];
				check(tmp_ans,0);
				if(max_diff <= 2)output(tmp_ans);
				tmp_ans[pos[j]] = g[0][pos[j]];
			}
			tmp_ans[pos[i]] = g[0][pos[i]];
		}
	}
	cout <<"No" <<endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	g = vector<vector<int> > (n,vector<int>(m,0)); 
	for(int i = 0;i < n;i++){
		for(int j = 0; j < m;j++){
			cin >> g[i][j];
		}
	}
	solve(); 
	return 0;
} 

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second 
void solve(){
	int n,m;
	vector<pair<int,int> > v;
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		int x,y;
		cin >> y >> x;
		v.pb(mp(x,y));
	}
	sort(v.begin(),v.end());
	if(m % 2 == 1){
		cout <<"NO"<<endl;
		return;
	}
	for(int i = 1;i < v.size();i += 2){
		auto p1 = v[i],p0 = v[i - 1];
		int r = p1.fi,l = p0.fi;
		int h1 = p1.se,h0 = p0.se;
		if(r == l)continue;
		if(i >= 2){
			if(v[i - 1].fi == v[i - 2].fi){
				cout << "NO" << endl;
				return;
			}
		}
		if(h0 == h1 && (r - l) % 2 == 0 ){
			cout <<"NO"<<endl;
			return;
		}
		if(h0 != h1 && (r - l)%2 == 1){
			cout << "NO" <<endl;
			return;
		}
	}
	cout << "YES" << endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t; 
	cin >> t;
	while(t--)
		solve(); 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值