传送门
题意: 给nnn个长度为mmm的数组,要求构造一个长度为mmm的数组,使得这个数组与前面nnn个数组同一位置最多两个元素不同。
思路: 我们为了方便构造,可以先把要构造的数组看成nnn个数组的第一个数组,让后在保证第一个数组改变不超过两个元素的情况下,能否使其与其他n−1n-1n−1个数组同一位置最多两个元素不同。首先可以发现,如果初始状态第一个数组与其他数组不同元素的个数<=2<=2<=2可以直接输出第一个数组,如果个数>4>4>4那么无解。现在就剩下不同的个数<=4<=4<=4的情况了。因为我们知道第一个数组最多改变两个位置的元素,那么我们找到n−1n-1n−1个数组中不同元素个数最多的一个,设为数组vposv_{pos}vpos。因为最多修改两个元素,所以我们枚举两个不同位置,把枚举的第一个位置设置为vpos[i]v_{pos}[i]vpos[i],第二个设置为−1-1−1,−1-1−1表示这个位置还不确定,检查的时候碰到需要的值的时候可以变成需要的值。 我们考虑−1-1−1这个位置是否在需要的时候变成需要的值是随意的呢?也就是对检查的正确性没有影响的呢? 可以分析一下,对于前面已经检查过的数组,−1-1−1这个位置与前面数都不相同,所以在前面计算的时候就已经把−1-1−1当做与其元素不同的元素看待,所以对前面的没有影响。对于后面未检查的数组,可能会存在影响,但是鉴于我们枚举的位置是全部的情况,所以有解的话一定可以枚举到正解的,所以可以忽略。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=250010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m;
vector<int>v[N];
bool st[N];bool check(vector<int>&vv)
{for(int i=1;i<n;i++){int cnt=0;for(int j=0;j<m;j++) if(v[i][j]!=vv[j]) cnt++;if(cnt>3) return false;if(cnt==3){int pos=1;for(int j=0;j<m;j++) if(v[i][j]!=vv[j]&&vv[j]==-1) { pos=0; vv[j]=v[i][j]; break; }if(pos) return false;}}return true;
}bool check()
{int c=0,id,mx=0;for(int i=1;i<n;i++){int cnt=0;for(int j=0;j<m;j++) if(v[i][j]!=v[0][j]) cnt++;if(cnt>4) return false;if(cnt<=2) c++;if(cnt>mx) mx=cnt,id=i;}if(c==n-1){puts("Yes");for(int i=0;i<m;i++) printf("%d ",v[0][i]); puts("");return true;}vector<int>pos;for(int i=0;i<m;i++) if(v[0][i]!=v[id][i]) pos.pb(i);for(int i=0;i<pos.size();i++)//枚举修改的两个位置{for(int j=0;j<pos.size();j++){if(i==j) continue;vector<int>tmp=v[0];tmp[pos[i]]=v[id][pos[i]]; tmp[pos[j]]=-1;if(check(tmp)){puts("Yes");for(int i=0;i<m;i++) printf("%d ",tmp[i]==-1? v[0][i]:tmp[i]); puts("");return true;}}}return false;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d%d",&n,&m);for(int i=0;i<n;i++) for(int j=0;j<m;j++) { int x; scanf("%d",&x); v[i].pb(x); }if(!check()) puts("No");return 0;
}
/**/