[CF632F]Magic Matrix
阿新 • • 發佈:2020-07-20
題目
題解
首先,這道題的洛谷題目翻譯有點問題,題目意思是這樣的:
滿足下列條件的矩陣稱為魔法矩陣:
- \(a[i][j]=a[j][i]\);
- \(a[i][i]=0\);
- \(a[i][j]\le \min\{\max(a[i][k],a[k][j])\}\)
現給你一個 \(n\times n\) 的矩陣,試判斷它是不是魔法矩陣,若是,輸出
MAGIC
,否則輸出NOT MAGIC
對於要求 \(1,2\),我們可以先用 \(n^2\) 直接判斷,唯一的難點在於條件三的判斷,如果直接做,我們需要 \(\mathcal O(n^3)\) 的複雜度,但是隻比時限超了一點點,我們能否考慮用一些卡常技巧卡過去?
考慮將一個值 \(k\) 作為分界點,小於其的看做 \(1\),大於的看做 \(0\),對於某一個 \((x,y)\) 如果其位置上的值為 \(k\),那麼,將其所在排、列的二進位制串並起來,如果最後得到 \(0\),那麼合法,否則不合法
為什麼這樣做是合法的?首先,條件三最裡面的 \(\max(a[i][k],a[k][j])\) 是一對一對來的,而按位並也是按位來的,而如果這一位為 \(0\) 則說明對應的倆數中大的一個比 \(k\) 大,而我們要求所有位置中都要比 \(k\) 大,所以最後的結果也必須為 \(0\)
二進位制則可以使用 bitset
進行優化,加一個 \(\frac{1}{32}\)
程式碼
#include<vector> #include<cstdio> #include<algorithm> #include<bitset> #include<utility> using namespace std; #define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i) #define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i) #define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to) #define writc(a,b) fwrit(a),putchar(b) #define mp(a,b) make_pair(a,b) #define ft first #define sd second typedef long long LL; typedef pair<int,int> pii; typedef unsigned long long ull; typedef unsigned uint; #define Endl putchar('\n') // #define int long long // #define int unsigned // #define int unsigned long long #define cg (c=getchar()) template<class T>inline void read(T& x){ char c;bool f=0; while(cg<'0'||'9'<c)f|=(c=='-'); for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48)); if(f)x=-x; } template<class T>inline T read(const T sample){ T x=0;char c;bool f=0; while(cg<'0'||'9'<c)f|=(c=='-'); for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48)); return f?-x:x; } template<class T>void fwrit(const T x){//just short,int and long long if(x<0)return (void)(putchar('-'),fwrit(-x)); if(x>9)fwrit(x/10); putchar(x%10^48); } template<class T>inline T Max(const T x,const T y){return x>y?x:y;} template<class T>inline T Min(const T x,const T y){return x<y?x:y;} template<class T>inline T fab(const T x){return x>0?x:-x;} inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;} inline void getInv(int inv[],const int lim,const int MOD){ inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD; } inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod; } const int MAXN=2500; int n,a[MAXN+5][MAXN+5]; pii t[MAXN*MAXN+5]; inline void Init(){ n=read(1); rep(i,1,n)rep(j,1,n)a[i][j]=read(1); } inline bool cmp(const pii i,const pii j){ return a[i.first][i.second]<a[j.first][j.second]; } vector<pii>pos[MAXN*MAXN+5];//離散化之後數值為 i 的位置 int hcnt; inline void hasMatrix(){ rep(i,1,n)rep(j,1,n)t[(i-1)*n+j]=mp(i,j); sort(t+1,t+(n*n)+1,cmp); int pre=a[t[1].first][t[1].second],hasnum=1; a[t[1].first][t[1].second]=1; rep(i,2,n*n){ hasnum+=(a[t[i].first][t[i].second]!=pre); pos[hasnum].push_back(t[i]); pre=a[t[i].first][t[i].second]; a[t[i].first][t[i].second]=hasnum; }hcnt=hasnum; } bitset<MAXN+5>x[MAXN+5],y[MAXN+5],res; inline void check(){ int sz,p,q; rep(i,1,hcnt){ sz=pos[i].size(); rep(j,0,sz-1){ p=pos[i][j].first,q=pos[i][j].second; res=x[p]&y[q]; if(res.any()){ puts("NOT MAGIC");return; } } rep(j,0,sz-1){ p=pos[i][j].first,q=pos[i][j].second; x[p][q]=y[q][p]=1; } } puts("MAGIC"); } signed main(){ Init(); rep(i,1,n)rep(j,1,n){ if(a[i][j]!=a[j][i] || (i==j && a[i][j]!=0)){ puts("NOT MAGIC"); return 0; } } hasMatrix(); check(); return 0; }