답안 #69271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
69271 2018-08-20T10:53:46 Z istlemin Chessboard (IZhO18_chessboard) C++14
47 / 100
2000 ms 50384 KB
#include<bits/stdc++.h>

using namespace std;

#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;

ll n,k;

ll ceilDiv(ll a,ll b){
	return a/b + (a%b!=0);
}

ll getNumBlack(ll x1,ll y1,ll x2,ll y2, ll sq){
	//cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;

	ll ans = 0;

    rep(i,x1,x2){
		rep(j,y1,y2){
			if((i/sq+j/sq)%2==0){
				ans++;
				//cout<<"#";
			} //else cout<<".";
		}
		//cout<<endl;
	}
    ll x12 = ceilDiv(x1,sq);
	ll y12 = ceilDiv(y1,sq);
	ll x22 = x2/sq;
	ll y22 = y2/sq;

    if((x12+y12)%2==0){ //black in left lower corner
        ll r1,r2;
        if(x22<x12){
            r1 = 0;
            r2 = x2-x1;
		} else if((x22-x12+2)%2==1){
            r1 = ((x22-x12)/2+1)*sq;
            r2 = ((x22-x12)/2)*sq + (x2-x22*sq) + (x12*sq-x1);
        } else {
            r1 = (x22-x12)/2*sq + (x2-x22*sq);
            r2 = (x22-x12)/2*sq + (x12*sq-x1);
        }

        ll c1,c2;
        if(y22<y12){
            c1 = 0;
            c2 = y2-y1;
		} else if((y22-y12+2)%2==1){
            c1 = ((y22-y12)/2+1)*sq;
            c2 = ((y22-y12)/2)*sq + (y2-y22*sq) + (y12*sq-y1);
        } else {
            c1 = (y22-y12)/2*sq + (y2-y22*sq);
            c2 = (y22-y12)/2*sq + (y12*sq-y1);
        }
        assert(ans==r1*c1+r2*c2);
		return r1*c1+r2*c2;
    } else {
		ll r1,r2;
        if(x22<x12){
            r1 = 0;
            r2 = x2-x1;
		} else if((x22-x12+2)%2==1){
            r1 = ((x22-x12)/2+1)*sq;
            r2 = ((x22-x12)/2)*sq + (x2-x22*sq) + (x12*sq-x1);
        } else {
            r1 = (x22-x12)/2*sq + (x2-x22*sq);
            r2 = (x22-x12)/2*sq + (x12*sq-x1);
        }

        ll c1,c2;
		if(y22<y12){
            c1 = 0;
            c2 = y2-y1;
		} else if((y22-y12)%2==1){
            c1 = ((y22-y12)/2+1)*sq;
            c2 = ((y22-y12)/2)*sq + (y2-y22*sq) + (y12*sq-y1);
        } else {
            c1 = (y22-y12)/2*sq + (y2-y22*sq);
            c2 = (y22-y12)/2*sq + (y12*sq-y1);
        }
        assert(ans==r1*c2+r2*c1);
		return r1*c2+r2*c1;
    }
}

vector<tuple<ll,ll,ll,ll> > v;

ll getCost(ll sq, bool startWhite){
    ll blackToBlack = 0;
    ll blackToWhite = 0;

    ll whiteToWhite, whiteToBlack;

    if((n/sq)%2==1){
		whiteToWhite = ((n/sq)*(n/sq)/2)*sq*sq;
		whiteToBlack = ((n/sq)*(n/sq)/2+1)*sq*sq;
    } else {
		whiteToWhite = ((n/sq)*(n/sq)/2)*sq*sq;
		whiteToBlack = ((n/sq)*(n/sq)/2)*sq*sq;
    }
    if(startWhite) swap(whiteToBlack,whiteToWhite);

    rep(i,0,k){
		ll x1,y1,x2,y2;
		tie(x1,y1,x2,y2) = v[i];
		ll sz = (x2-x1)*(y2-y1);
        ll numBlack = getNumBlack(x1,y1,x2,y2,sq);
        if(startWhite)
			numBlack = sz-numBlack;

		whiteToBlack -= numBlack;
        blackToBlack += numBlack;
		whiteToWhite -= sz-numBlack;
        blackToWhite += sz-numBlack;
    }

    return whiteToBlack + blackToWhite;
}

int main(){
	cin.sync_with_stdio(false);
    cin>>n>>k;
    rep(i,0,k){
		ll x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		--x1;--y1;
        v.emplace_back(x1,y1,x2,y2);
    }

    ll bestCost = 1e18;

    rep(i,1,n){
		if(n%i==0){
			bestCost = min(bestCost,getCost(i,false));
			bestCost = min(bestCost,getCost(i,true));
		}
    }
    cout<<bestCost<<endl;

	//cout<<getNumBlack(3,5,5,7,4)<<endl;
	//return 0;
    /*srand(time(NULL));
    rep(i,0,1000){
		ll x2 = rand()%10+1;
		ll y2 = rand()%10+1;
		ll x1 = rand()%x2;
		ll y1 = rand()%y2;
		cout<<getNumBlack(x1,y1,x2,y2,rand()%10+1)<<endl;
    }*/


    /*ll w = 7;
    ll h = 5;
    rep(i,0,10){
		rep(j,0,10){
			cout<<(getNumBlack(j,i,j+w,i+h,2)==(w*h/2))<<" ";
		}
        cout<<endl;
    }*/
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 520 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 2 ms 680 KB Output is correct
7 Correct 4 ms 684 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 4276 KB Output is correct
2 Correct 14 ms 4276 KB Output is correct
3 Correct 31 ms 5732 KB Output is correct
4 Correct 31 ms 6472 KB Output is correct
5 Correct 68 ms 7732 KB Output is correct
6 Correct 32 ms 8728 KB Output is correct
7 Correct 12 ms 8728 KB Output is correct
8 Correct 31 ms 9672 KB Output is correct
9 Correct 79 ms 13384 KB Output is correct
10 Correct 43 ms 13384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13384 KB Output is correct
2 Correct 2 ms 13384 KB Output is correct
3 Correct 3 ms 13384 KB Output is correct
4 Correct 5 ms 13384 KB Output is correct
5 Correct 4 ms 13384 KB Output is correct
6 Correct 4 ms 13384 KB Output is correct
7 Correct 3 ms 13384 KB Output is correct
8 Correct 4 ms 13384 KB Output is correct
9 Correct 3 ms 13384 KB Output is correct
10 Correct 3 ms 13384 KB Output is correct
11 Correct 3 ms 13384 KB Output is correct
12 Correct 5 ms 13384 KB Output is correct
13 Correct 4 ms 13384 KB Output is correct
14 Correct 4 ms 13384 KB Output is correct
15 Correct 3 ms 13384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13384 KB Output is correct
2 Correct 2 ms 13384 KB Output is correct
3 Correct 3 ms 13384 KB Output is correct
4 Correct 5 ms 13384 KB Output is correct
5 Correct 4 ms 13384 KB Output is correct
6 Correct 4 ms 13384 KB Output is correct
7 Correct 3 ms 13384 KB Output is correct
8 Correct 4 ms 13384 KB Output is correct
9 Correct 3 ms 13384 KB Output is correct
10 Correct 3 ms 13384 KB Output is correct
11 Correct 3 ms 13384 KB Output is correct
12 Correct 5 ms 13384 KB Output is correct
13 Correct 4 ms 13384 KB Output is correct
14 Correct 4 ms 13384 KB Output is correct
15 Correct 3 ms 13384 KB Output is correct
16 Correct 30 ms 13384 KB Output is correct
17 Correct 59 ms 16908 KB Output is correct
18 Correct 111 ms 18272 KB Output is correct
19 Correct 488 ms 19872 KB Output is correct
20 Correct 569 ms 21300 KB Output is correct
21 Correct 53 ms 22900 KB Output is correct
22 Correct 4 ms 22900 KB Output is correct
23 Correct 77 ms 22900 KB Output is correct
24 Correct 90 ms 24904 KB Output is correct
25 Correct 19 ms 24904 KB Output is correct
26 Correct 67 ms 24904 KB Output is correct
27 Correct 99 ms 27164 KB Output is correct
28 Correct 119 ms 28520 KB Output is correct
29 Correct 24 ms 28520 KB Output is correct
30 Correct 5 ms 28520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 4276 KB Output is correct
2 Correct 14 ms 4276 KB Output is correct
3 Correct 31 ms 5732 KB Output is correct
4 Correct 31 ms 6472 KB Output is correct
5 Correct 68 ms 7732 KB Output is correct
6 Correct 32 ms 8728 KB Output is correct
7 Correct 12 ms 8728 KB Output is correct
8 Correct 31 ms 9672 KB Output is correct
9 Correct 79 ms 13384 KB Output is correct
10 Correct 43 ms 13384 KB Output is correct
11 Correct 4 ms 13384 KB Output is correct
12 Correct 2 ms 13384 KB Output is correct
13 Correct 3 ms 13384 KB Output is correct
14 Correct 5 ms 13384 KB Output is correct
15 Correct 4 ms 13384 KB Output is correct
16 Correct 4 ms 13384 KB Output is correct
17 Correct 3 ms 13384 KB Output is correct
18 Correct 4 ms 13384 KB Output is correct
19 Correct 3 ms 13384 KB Output is correct
20 Correct 3 ms 13384 KB Output is correct
21 Correct 3 ms 13384 KB Output is correct
22 Correct 5 ms 13384 KB Output is correct
23 Correct 4 ms 13384 KB Output is correct
24 Correct 4 ms 13384 KB Output is correct
25 Correct 3 ms 13384 KB Output is correct
26 Correct 30 ms 13384 KB Output is correct
27 Correct 59 ms 16908 KB Output is correct
28 Correct 111 ms 18272 KB Output is correct
29 Correct 488 ms 19872 KB Output is correct
30 Correct 569 ms 21300 KB Output is correct
31 Correct 53 ms 22900 KB Output is correct
32 Correct 4 ms 22900 KB Output is correct
33 Correct 77 ms 22900 KB Output is correct
34 Correct 90 ms 24904 KB Output is correct
35 Correct 19 ms 24904 KB Output is correct
36 Correct 67 ms 24904 KB Output is correct
37 Correct 99 ms 27164 KB Output is correct
38 Correct 119 ms 28520 KB Output is correct
39 Correct 24 ms 28520 KB Output is correct
40 Correct 5 ms 28520 KB Output is correct
41 Correct 413 ms 31212 KB Output is correct
42 Correct 101 ms 33192 KB Output is correct
43 Correct 294 ms 35456 KB Output is correct
44 Correct 106 ms 37572 KB Output is correct
45 Correct 80 ms 39628 KB Output is correct
46 Correct 474 ms 41992 KB Output is correct
47 Correct 64 ms 44212 KB Output is correct
48 Correct 157 ms 46312 KB Output is correct
49 Correct 96 ms 48376 KB Output is correct
50 Execution timed out 2064 ms 50384 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 520 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 2 ms 680 KB Output is correct
7 Correct 4 ms 684 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
9 Correct 44 ms 4276 KB Output is correct
10 Correct 14 ms 4276 KB Output is correct
11 Correct 31 ms 5732 KB Output is correct
12 Correct 31 ms 6472 KB Output is correct
13 Correct 68 ms 7732 KB Output is correct
14 Correct 32 ms 8728 KB Output is correct
15 Correct 12 ms 8728 KB Output is correct
16 Correct 31 ms 9672 KB Output is correct
17 Correct 79 ms 13384 KB Output is correct
18 Correct 43 ms 13384 KB Output is correct
19 Correct 4 ms 13384 KB Output is correct
20 Correct 2 ms 13384 KB Output is correct
21 Correct 3 ms 13384 KB Output is correct
22 Correct 5 ms 13384 KB Output is correct
23 Correct 4 ms 13384 KB Output is correct
24 Correct 4 ms 13384 KB Output is correct
25 Correct 3 ms 13384 KB Output is correct
26 Correct 4 ms 13384 KB Output is correct
27 Correct 3 ms 13384 KB Output is correct
28 Correct 3 ms 13384 KB Output is correct
29 Correct 3 ms 13384 KB Output is correct
30 Correct 5 ms 13384 KB Output is correct
31 Correct 4 ms 13384 KB Output is correct
32 Correct 4 ms 13384 KB Output is correct
33 Correct 3 ms 13384 KB Output is correct
34 Correct 30 ms 13384 KB Output is correct
35 Correct 59 ms 16908 KB Output is correct
36 Correct 111 ms 18272 KB Output is correct
37 Correct 488 ms 19872 KB Output is correct
38 Correct 569 ms 21300 KB Output is correct
39 Correct 53 ms 22900 KB Output is correct
40 Correct 4 ms 22900 KB Output is correct
41 Correct 77 ms 22900 KB Output is correct
42 Correct 90 ms 24904 KB Output is correct
43 Correct 19 ms 24904 KB Output is correct
44 Correct 67 ms 24904 KB Output is correct
45 Correct 99 ms 27164 KB Output is correct
46 Correct 119 ms 28520 KB Output is correct
47 Correct 24 ms 28520 KB Output is correct
48 Correct 5 ms 28520 KB Output is correct
49 Correct 413 ms 31212 KB Output is correct
50 Correct 101 ms 33192 KB Output is correct
51 Correct 294 ms 35456 KB Output is correct
52 Correct 106 ms 37572 KB Output is correct
53 Correct 80 ms 39628 KB Output is correct
54 Correct 474 ms 41992 KB Output is correct
55 Correct 64 ms 44212 KB Output is correct
56 Correct 157 ms 46312 KB Output is correct
57 Correct 96 ms 48376 KB Output is correct
58 Execution timed out 2064 ms 50384 KB Time limit exceeded
59 Halted 0 ms 0 KB -