結果

問題 No.5002 stick xor
ユーザー ts_
提出日時 2018-05-26 00:43:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 957 ms / 1,000 ms
コード長 2,981 bytes
コンパイル時間 33,178 ms
実行使用メモリ 1,764 KB
スコア 39,687
最終ジャッジ日時 2018-05-26 00:43:36
ジャッジサーバーID
(参考情報)
judge10 /
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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.85;
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,W0;
int L[501];
string s[61];
vector<vector<int> > g(101,vector<int>(101,0)),tg(101,vector<int>(101,0));
vector<pair<pint,pint> > ans,tmp;
pair<pint,pint> calc(int length){
    int best=-1000100010;
    int lx,rx,ly,ry;
    vector<pair<pint,pint> > cand;
    rep(i,n)rep(j,n-length+1){
        int score=0;
        rep(k,length){
            if(g[i][j+k]==1) ++score;
            else --score;
        }
        if(best<score){
            cand.clear();
            cand.pb(pint(i+1,i+1),pint(j+1,j+length));
            best=score;
        }
        else if(best==score){
            cand.pb(pint(i+1,i+1),pint(j+1,j+length));
        }
    }
    rep(i,n)rep(j,n-length+1){
        int score=0;
        rep(k,length){
            if(g[j+k][i]==1) ++score;
            else --score;
        }
        if(best<score){
            cand.clear();
            cand.pb(pint(j+1,j+length),pint(i+1,i+1));
            best=score;
        }
        else if(best==score){
            cand.pb(pint(j+1,j+length),pint(i+1,i+1));
        }
    }
    int sz=cand.size();
    pair<pint,pint> ret=cand[xor128()%sz];
    ly=ret.first.first,ry=ret.first.second,lx=ret.second.first,rx=ret.second.second;
    FOR(i,ly,ry+1)FOR(j,lx,rx+1) g[i-1][j-1]^=1;
    return make_pair(pint(ly,lx),pint(ry,rx));
}
int get_score(){
    int ret=0;
    rep(i,n)rep(j,n)if(g[i][j]==0) ++ret;
    return ret;
}
int main(){
    Timer timer;
    timer.reset();
    cin>>n>>k;
    rep(i,k) cin>>L[i];
    rep(i,n) cin>>s[i];;
    tmp.resize(k);ans.resize(k);
    rep(i,n)rep(j,n){
        g[i][j]=s[i][j]-'0';
    }
    tg=g;
    int bestscore=-1000100010;
    while(timer.get()<timeLimit){
        rep(i,k){
            pair<pint,pint> ret=calc(L[i]);
            tmp[i]=ret;
            //cout<<ret.first.first<<" "<<ret.first.second<<" "<<ret.second.first<<" "<<ret.second.second<<endl;
        }
        int cur=get_score();
        if(bestscore<cur){
            ans=tmp;bestscore=cur;
        }
        g=tg;
    }
    rep(i,k){
        cout<<ans[i].first.first<<" "<<ans[i].first.second<<" "<<ans[i].second.first<<" "<<ans[i].second.second<<endl;
    }
    return 0;
}
0