답안 #48995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48995 2018-05-21T06:20:08 Z Namnamseo 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
97 / 100
9000 ms 9596 KB
#include "elephants.h"
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#define BS 350
using namespace std;

int n;
int l;

struct buck {
	int pos[1010];
	int dp [1010];
	int ep [1010];
	int sz;
	buck(){ sz=0; }

	void recalcDP(int until){
		for(int i=until; 0<=i; --i){
			auto it=upper_bound(pos, pos+sz, pos[i]+l);

			if(it == pos+sz){
				dp[i] = 0;
				ep[i] = pos[i];
			} else {
				dp[i] = 1+dp[it-pos];
				ep[i] = ep[it-pos];
			}
		}
	}

	void ins(int x){
		if(sz == 0){
			pos[0] = x;
			dp [0] = 0;
			ep [0] = x;
			sz = 1;
			return;
		}
		auto it=upper_bound(pos, pos+sz, x);
		if(it != pos+sz){
			rotate(it, pos+sz, pos+sz+1);
			rotate(dp+(it-pos), dp+sz, dp+sz+1);
			rotate(ep+(it-pos), ep+sz, ep+sz+1);
		}
		*it = x;
		++sz;

		recalcDP(it-pos);
	}

	void rem(int x){
		int p = lower_bound(pos, pos+sz, x)-pos;
		rotate(pos+p, pos+p+1, pos+sz);
		rotate(dp +p, dp +p+1, dp +sz);
		rotate(ep +p, ep +p+1, ep +sz);
		--sz;

		if(sz) recalcDP(min(sz-1,p));
	}
} bucks[510];
int bn;

int findBuck(int x){
	if(x < bucks[0].pos[0]) return 0;
	for(int i=0; i<bn; ++i){
		if(bucks[i].pos[0]<=x && (i==bn-1 || x<bucks[i+1].pos[0])) return i;
	}
	exit(12345);
	return -1;
}

int sqrtN;

int pos   [150001];
int sorted[150001];

void recreateBuck(bool init){
	if(init){
		memcpy(sorted, pos, n*sizeof(int));
	} else {
		int ind = 0;
		for(int i=0; i<bn; ++i){
			int *arr = bucks[i].pos;
			int sz=bucks[i].sz;
			for(int j=0; j<sz; ++j){
				sorted[ind++] = arr[j];
			}
			bucks[i].sz = 0;
		}
	}

	bn = 0;
	for(int i=0; i<n; ++i){
		int cb=i/BS;
		if(cb >= bn) ++bn;
		int& sz=bucks[cb].sz;
		bucks[cb].pos[sz++] = sorted[i];
	}
	for(int i=0; i<bn; ++i){
		bucks[i].recalcDP(bucks[i].sz-1);
	}
}

void init(int N, int L, int X[])
{
	n = N; l = L;

	for(int i=0; i<n; ++i) pos[i] = X[i];

	recreateBuck(1);
}

int timer = 0;

int getAns(){
	int lastP = bucks[0].ep[0];
	int ans = bucks[0].dp[0];

	for(int i=1; i<bn; ++i){
		if(lastP + l >= bucks[i].pos[bucks[i].sz-1]) continue;
		++ans;
		int ind = upper_bound(bucks[i].pos, bucks[i].pos+bucks[i].sz, lastP+l)-bucks[i].pos;
		ans += bucks[i].dp[ind];
		lastP = bucks[i].ep[ind];
	}
	++ans;
	return ans;
}

int update(int i, int y)
{
	int t = findBuck(pos[i]);
	int nt = findBuck(y);
	bucks[t].rem(pos[i]);

	pos[i]=y;

	bucks[nt].ins(y);

	++timer;
	if(timer >= BS){
		recreateBuck(0);
		timer = 0;
	}
	return getAns();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2424 KB Output is correct
2 Correct 4 ms 2424 KB Output is correct
3 Correct 5 ms 2612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2648 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
3 Correct 3 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 696 ms 3652 KB Output is correct
2 Correct 756 ms 3900 KB Output is correct
3 Correct 1176 ms 5052 KB Output is correct
4 Correct 1442 ms 5100 KB Output is correct
5 Correct 1522 ms 5100 KB Output is correct
6 Correct 1442 ms 5100 KB Output is correct
7 Correct 1378 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 759 ms 5100 KB Output is correct
2 Correct 1183 ms 5100 KB Output is correct
3 Correct 1965 ms 5216 KB Output is correct
4 Correct 2332 ms 6116 KB Output is correct
5 Correct 2420 ms 6116 KB Output is correct
6 Correct 1427 ms 6116 KB Output is correct
7 Correct 2537 ms 6116 KB Output is correct
8 Correct 2413 ms 6116 KB Output is correct
9 Correct 2452 ms 6116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 9014 ms 9596 KB Time limit exceeded
2 Halted 0 ms 0 KB -