답안 #319240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319240 2020-11-04T14:49:06 Z AmineWeslati 코알라 (APIO17_koala) C++14
100 / 100
86 ms 624 KB
//Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#ifndef LOCAL
#include "koala.h"
#endif
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}

#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int P[100];
int N,W;

#ifdef LOCAL
void playRound(int *B, int *R) {
    int i, j;

    int S = 0;
    for (i=0;i<N;++i) {
        if ( !(B[i] >= 0 && B[i] <= W) ) {
            printf("Invalid query.\n");
            exit(0);
        }
        S += B[i];
    }
    if (S > W) {
        printf("Invalid query.\n");
        exit(0);
    }

    /*numQueries++;
    if (numQueries > maxQueries) {
        printf("Too many queries.\n");
        exit(0);
    }*/

    int cache[2][205];
    int num[2][205];
    char taken[105][205];

    for (i=0;i<205;++i) {
        cache[1][i] = 0;
        num[1][i] = 0;
    }

    for (i=0;i<N;++i) {
        int v = B[i]+1;
        int ii = i&1;
        int o = ii^1;
        for (j=0;j<=W;++j) {
            cache[ii][j] = cache[o][j];
            num[ii][j] = num[o][j];
            taken[i][j] = 0;
        }
        for (j=W;j>=v;--j) {
            int h = cache[o][j-v] + P[i];
            int hn = num[o][j-v] + 1;
            if (h > cache[ii][j] || (h == cache[ii][j] && hn > num[ii][j])) {
                cache[ii][j] = h;
                num[ii][j] = hn;
                taken[i][j] = 1;
            } else {
                taken[i][j] = 0;
            }
        }
    }

    int cur = W;
    for (i=N-1;i>=0;--i) {
        R[i] = taken[i][cur] ? (B[i] + 1) : 0;
        cur -= R[i];
    }
}
#endif

int B[100],R[100];

int minValue(int NN, int WW) {
	N=NN; W=WW;

	fill(B,B+N,0);
	B[0]=1;

	playRound(B,R);

	FOR(i,0,N) if(R[i]==0) return i;
	return 0;
}

int maxValue(int NN, int WW) {
	N=NN; W=WW;

	vi v; FOR(i,0,N) v.pb(i);
	while(sz(v)>1){
		int k=W/sz(v);
		fill(B,B+N,0);
		for(auto i: v) B[i]=k;

		playRound(B,R);

		v.clear();
		FOR(i,0,N) if(R[i]>k) v.pb(i);
	}
	return v[0];
}

int greaterValue(int NN, int WW) {
	N=NN; W=WW;

	int l=1,r=9;
	int cnt=0;
	while(l<=r){
		int m=(l+r)/2;
		fill(B,B+N,0);
		B[0]=m; B[1]=m; 

		playRound(B,R);

		if(R[0]>m && R[1]>m) l=m+1;
		else if(R[0]<=m && R[1]<=m) r=m-1;
		else return (R[0]<R[1]);
	}
	return 0;
}

bool compare(int i, int j){
	fill(B,B+N,0);
	B[i]=N; B[j]=N;

	playRound(B,R);

	return (R[i]>R[j]);
}

vi merge_sort(vi v){
	if(sz(v)==1) return v;

	vi a,b;
	FOR(i,0,(sz(v)+1)/2) a.pb(v[i]);
	FOR(i,(sz(v)+1)/2,sz(v)) b.pb(v[i]);

	a=merge_sort(a); b=merge_sort(b);

	int i=0,j=0;
	vi vec;
	while(i<sz(a) || j<sz(b)){
		if(i<sz(a) && j<sz(b)){
			if(!compare(a[i],b[j])) vec.pb(a[i]),i++;
			else vec.pb(b[j]),j++;
		}
		else if(i<sz(a)) vec.pb(a[i]),i++;
		else vec.pb(b[j]),j++;
	}
	return vec;
}

vi solve(vi v, int l=1, int r=100){
	if(sz(v)==1) return v;

	int x=W/sz(v);
	x=1;
	while(((x+1)*(x+2))/2<=r && (x+1)*(r-l+1)<=W) x++;

	fill(B,B+N,0);
	for(auto i: v) B[i]=x;
	playRound(B,R);
	
	vi a,b;
	for(auto i: v){
		if(R[i]>x) b.pb(i);
		else a.pb(i);
	}
	//dbgv(a) dbgv(b)

	a=solve(a,l,l+sz(a)-1); b=solve(b,r-sz(b)+1,r);
	for(auto x: b) a.pb(x);
	return a;
}	

void allValues(int NN, int WW, int *PP){
	N=NN; W=WW;

	vi v; FOR(i,0,N) v.pb(i);
	if(W==2*N){
		vi res=merge_sort(v);
		FOR(i,0,N) PP[res[i]]=i+1;
	}
	else{
		vi res=solve(v);
		FOR(i,0,N) PP[res[i]]=i+1;
	}
}

#ifdef LOCAL
int main() {
    boost; IO();

    FOR(i,0,100) P[i]=i+1;
    //cout << maxValue(100,100) << endl;

    /*P[0]=99; P[1]=100; P[99]=1; P[98]=2;
    cout << greaterValue(100,100) << endl;*/

    swap(P[0],P[1]);
    allValues(100,100,P);
    dbgv(P);

    return 0;
}
#endif


/* Careful!!!
    .Array bounds
    .Infinite loops
    .Uninitialized variables / empty containers
    .Multisets are shit

   Some insights:
    .Binary search
    .Graph representation
    .Write brute force code
    .Change your approach
*/

Compilation message

koala.cpp: In function 'int greaterValue(int, int)':
koala.cpp:168:6: warning: unused variable 'cnt' [-Wunused-variable]
  168 |  int cnt=0;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 364 KB Output is correct
2 Correct 6 ms 364 KB Output is correct
3 Correct 6 ms 364 KB Output is correct
4 Correct 6 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 364 KB Output is correct
2 Correct 18 ms 400 KB Output is correct
3 Correct 18 ms 364 KB Output is correct
4 Correct 18 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 364 KB Output is correct
2 Correct 86 ms 364 KB Output is correct
3 Correct 74 ms 364 KB Output is correct
4 Correct 72 ms 364 KB Output is correct
5 Correct 72 ms 524 KB Output is correct
6 Correct 73 ms 364 KB Output is correct
7 Correct 74 ms 484 KB Output is correct
8 Correct 75 ms 364 KB Output is correct
9 Correct 74 ms 364 KB Output is correct
10 Correct 71 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 364 KB Output is correct
2 Correct 46 ms 484 KB Output is correct
3 Correct 46 ms 364 KB Output is correct
4 Correct 45 ms 396 KB Output is correct
5 Correct 44 ms 364 KB Output is correct
6 Correct 44 ms 488 KB Output is correct
7 Correct 44 ms 364 KB Output is correct
8 Correct 44 ms 384 KB Output is correct
9 Correct 43 ms 364 KB Output is correct
10 Correct 43 ms 364 KB Output is correct
11 Correct 44 ms 364 KB Output is correct
12 Correct 26 ms 364 KB Output is correct
13 Correct 44 ms 364 KB Output is correct
14 Correct 41 ms 364 KB Output is correct
15 Correct 41 ms 484 KB Output is correct
16 Correct 39 ms 364 KB Output is correct
17 Correct 38 ms 364 KB Output is correct
18 Correct 40 ms 364 KB Output is correct
19 Correct 40 ms 364 KB Output is correct
20 Correct 40 ms 524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 364 KB Output is correct
2 Correct 6 ms 624 KB Output is correct
3 Correct 5 ms 364 KB Output is correct
4 Correct 5 ms 364 KB Output is correct
5 Correct 5 ms 364 KB Output is correct
6 Correct 5 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 5 ms 364 KB Output is correct
9 Correct 5 ms 364 KB Output is correct
10 Correct 5 ms 364 KB Output is correct
11 Correct 5 ms 364 KB Output is correct
12 Correct 4 ms 364 KB Output is correct
13 Correct 5 ms 364 KB Output is correct
14 Correct 5 ms 364 KB Output is correct
15 Correct 5 ms 364 KB Output is correct
16 Correct 5 ms 492 KB Output is correct
17 Correct 5 ms 364 KB Output is correct
18 Correct 4 ms 364 KB Output is correct
19 Correct 5 ms 364 KB Output is correct
20 Correct 5 ms 364 KB Output is correct
21 Correct 5 ms 364 KB Output is correct
22 Correct 5 ms 364 KB Output is correct
23 Correct 5 ms 364 KB Output is correct
24 Correct 5 ms 364 KB Output is correct
25 Correct 5 ms 364 KB Output is correct
26 Correct 5 ms 364 KB Output is correct
27 Correct 5 ms 364 KB Output is correct
28 Correct 5 ms 364 KB Output is correct
29 Correct 5 ms 364 KB Output is correct
30 Correct 5 ms 364 KB Output is correct
31 Correct 5 ms 364 KB Output is correct
32 Correct 5 ms 360 KB Output is correct
33 Correct 5 ms 364 KB Output is correct
34 Correct 5 ms 364 KB Output is correct
35 Correct 5 ms 364 KB Output is correct
36 Correct 5 ms 364 KB Output is correct
37 Correct 5 ms 364 KB Output is correct
38 Correct 4 ms 364 KB Output is correct
39 Correct 5 ms 364 KB Output is correct
40 Correct 5 ms 364 KB Output is correct