結果

問題 No.5002 stick xor
ユーザー ts_ts_
提出日時 2018-05-27 06:18:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 985 ms / 1,000 ms
コード長 5,747 bytes
コンパイル時間 35,249 ms
実行使用メモリ 1,564 KB
スコア 43,465
最終ジャッジ日時 2018-05-27 06:19:06
ジャッジサーバーID
(参考情報)
judge8 /
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 985 ms
1,564 KB
testcase_01 AC 984 ms
1,560 KB
testcase_02 AC 984 ms
1,564 KB
testcase_03 AC 983 ms
1,560 KB
testcase_04 AC 984 ms
1,560 KB
testcase_05 AC 984 ms
1,564 KB
testcase_06 AC 984 ms
1,564 KB
testcase_07 AC 984 ms
1,560 KB
testcase_08 AC 984 ms
1,560 KB
testcase_09 AC 984 ms
1,560 KB
testcase_10 AC 985 ms
1,560 KB
testcase_11 AC 984 ms
1,560 KB
testcase_12 AC 984 ms
1,564 KB
testcase_13 AC 984 ms
1,564 KB
testcase_14 AC 984 ms
1,564 KB
testcase_15 AC 984 ms
1,560 KB
testcase_16 AC 983 ms
1,560 KB
testcase_17 AC 985 ms
1,560 KB
testcase_18 AC 984 ms
1,560 KB
testcase_19 AC 984 ms
1,564 KB
testcase_20 AC 984 ms
1,560 KB
testcase_21 AC 984 ms
1,560 KB
testcase_22 AC 984 ms
1,564 KB
testcase_23 AC 984 ms
1,560 KB
testcase_24 AC 984 ms
1,564 KB
testcase_25 AC 984 ms
1,564 KB
testcase_26 AC 984 ms
1,560 KB
testcase_27 AC 984 ms
1,560 KB
testcase_28 AC 983 ms
1,564 KB
testcase_29 AC 984 ms
1,564 KB
testcase_30 AC 984 ms
1,560 KB
testcase_31 AC 984 ms
1,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

unsigned long xor128(){
    static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
    unsigned long t=(x^(x<<11));
    x=y;y=z;z=w;
    return (w=(w^(w>>19))^(t^(t>>8)));
}
double timeLimit=0.91;
const int64_t CYCLES_PER_SEC=2800000000;
struct Timer {
	int64_t start;
	Timer() { reset(); }
	void reset() { start = getCycle(); }
	void plus(double a) { start -= (a * CYCLES_PER_SEC); }
	inline double get() { return (double)(getCycle() - start) / CYCLES_PER_SEC; }
	inline int64_t getCycle() {
		uint32_t low, high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((int64_t)low) | ((int64_t)high << 32);
	}
};
int n,k;
pint L[501],tL[501];
string s[61];
bool g[61][61],g2[61][61];
//vector<pair<pint,pint> > cand(7201);
vector<pair<pint,pint> > ans1,ans2;
int lx,rx,ly,ry;
pair<pint,pint> calc(int length){
    pair<pint,pint> ret;
    pint best={-1000100010,0};
    pint score;
    int slide=1;
    rep(i,n)rep(j,n-length+1)if(g[i][j]|g[i][j+length-1]){
        score={0,0};
        slide=1;
        while(j-slide>=0&&g[i][j-slide]!=g[i][j]&&slide<3) ++score.second,++slide;
        //while(j-slide>=0&&g[i][j-slide]^g[i][j]) ++score.second,++slide;
        slide=length;
        while(j+slide<n&&g[i][j+slide]!=g[i][j+length-1]&&slide<2+length) ++score.second,++slide;
        //while(j+slide<n&&g[i][j+slide]^g[i][j+length-1]) ++score.second,++slide;
        rep(k,length){
            g[i][j+k]?++score.first:--score.first;
            if(k>0&&g[i][j+k-1]==g[i][j+k]) ++score.second;
            
            slide=1;
            while(i-slide>=0&&g[i][j+k]!=g[i-slide][j+k]&&slide<3) ++score.second,++slide;
            slide=1;
            while(i+slide<n&&g[i][j+k]!=g[i+slide][j+k]&&slide<3) ++score.second,++slide;
            
        }
        if(best<score){
            ret=make_pair(pint(i+1,j+1),pint(i+1,j+length));
            best=score;
        }
    }
    
    rep(i,n)rep(j,n-length+1)if(g[j][i]|g[j+length-1][i]){
        score={0,0};
        slide=1;
        while(j-slide>=0&&g[j-slide][i]!=g[j][i]&&slide<3) ++score.second,++slide;
        //while(j-slide>=0&&g[j-slide][i]^g[j][i]) ++score.second,++slide;
        slide=length;
        while(j+slide<n&&g[j+slide][i]!=g[j+length-1][i]&&slide<2+length) ++score.second,++slide;
        //while(j+slide<n&&g[j+slide][i]^g[j+length-1][i]) ++score.second,++slide;
        rep(k,length){
            g[j+k][i]?++score.first:--score.first;
            if(k>0&&g[j+k-1][i]==g[j+k][i]) ++score.second;
            
            slide=1;
            while(i-slide>=0&&g[j+k][i]!=g[j+k][i-slide]&&slide<3) ++score.second,++slide;
            slide=1;
            while(i+slide<n&&g[j+k][i]!=g[j+k][i+slide]&&slide<3) ++score.second,++slide;
            
        }
        if(best<score){
            ret=make_pair(pint(j+1,i+1),pint(j+length,i+1));
            best=score;
        }
    }
    ly=ret.first.first,lx=ret.first.second,ry=ret.second.first,rx=ret.second.second;
    FOR(i,ly,ry+1)FOR(j,lx,rx+1) g[i-1][j-1]^=1;
    return ret;
}
pair<pint,pint> calc2(pair<pint,pint> &tget,bool g[61][61]){
    pair<pint,pint> ret;
    pint best={-1000100010,0};
    //int lx,rx,ly,ry;
    ly=tget.first.first,lx=tget.first.second,ry=tget.second.first,rx=tget.second.second;
    FOR(i,ly,ry+1)FOR(j,lx,rx+1) g[i-1][j-1]^=1;
    int length=(ry-ly)==0?rx-lx+1:ry-ly+1;
    int slide=1;
    rep(i,n)rep(j,n-length+1)if(g[i][j]|g[i][j+length-1]){
        pint score={0,0};
        slide=1;
        while(j-slide>=0&&g[i][j-slide]^g[i][j]) ++score.second,++slide;
        slide=length;
        while(j+slide<n&&g[i][j+slide]^g[i][j+length-1]) ++score.second,++slide;
        rep(k,length){
            g[i][j+k]?++score.first:--score.first;
            if(k>0&&g[i][j+k-1]==g[i][j+k]) ++score.second;
            
        }
        if(best<score){
            ret=make_pair(pint(i+1,j+1),pint(i+1,j+length));
            best=score;
        }
    }
    
    rep(i,n)rep(j,n-length+1)if(g[j][i]|g[j+length-1][i]){
        pint score={0,0};
        slide=1;
        while(j-slide>=0&&g[j-slide][i]^g[j][i]) ++score.second,++slide;
        slide=length;
        while(j+slide<n&&g[j+slide][i]^g[j+length-1][i]) ++score.second,++slide;
        rep(k,length){
            g[j+k][i]?++score.first:--score.first;
            if(k>0&&g[j+k-1][i]==g[j+k][i]) ++score.second;
            
        }
        if(best<score){
            ret=make_pair(pint(j+1,i+1),pint(j+length,i+1));
            best=score;
        }
    }
    ly=ret.first.first,lx=ret.first.second,ry=ret.second.first,rx=ret.second.second;
    FOR(i,ly,ry+1)FOR(j,lx,rx+1) g[i-1][j-1]^=1;
    return ret;
}
int get_score(bool g[61][61]){
    int ret=0;
    rep(i,n)ret+=count(g[i],g[i]+n,0);
    return ret;
}
int main(){
    Timer timer;
    timer.reset();
    cin>>n>>k;
    int Li;
    rep(i,k){
        cin>>Li;
        L[i]={Li,i};
    }
    //sort(L,L+k,greater<pint>());
    rep(i,n) cin>>s[i];
    ans1.resize(k);
    rep(i,n)rep(j,n){
        g[i][j]|=s[i][j]-'0';
    }
    rep(i,k){
        ans1[L[i].second]=calc(L[i].first);
    }
    memcpy(g2,g,sizeof(g));
    ans2=ans1;
    int id;
    double t;
    while(1){
        t=timer.get();
        if(t>timeLimit) break;
        id=xor128()%k;
        if(xor128()%2==0)ans1[id]=calc2(ans1[id],g);
        else ans2[id]=calc2(ans2[id],g2);
    }
    if(get_score(g)<get_score(g2)) ans1=ans2;
    rep(i,k){
        cout<<ans1[i].first.first<<" "<<ans1[i].first.second<<" "<<ans1[i].second.first<<" "<<ans1[i].second.second<<endl;
    }
    return 0;
}
0