[CF632F]Magic Matrix

题目

传送门

题解

首先,这道题的洛谷题目翻译有点问题,题目意思是这样的:

满足下列条件的矩阵称为魔法矩阵:

  1. \(a[i][j]=a[j][i]\)
  2. \(a[i][i]=0\)
  3. \(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;
}
posted @ 2020-07-20 08:48  Arextre  阅读(157)  评论(0编辑  收藏  举报