Submission #538670

# Submission time Handle Problem Language Result Execution time Memory
538670 2022-03-17T12:19:29 Z myrcella Jousting tournament (IOI12_tournament) C++17
100 / 100
596 ms 5964 KB
//by szh
#include<bits/stdc++.h>
using namespace std;

/*

#define inbuf_len 1 << 16
#define outbuf_len 1 << 16

int GetBestPosition(int N, int C, int R, int *K, int *S, int *E);

int main() {
  int tmp;

  // Set input and output buffering
  char *inbuf, *outbuf;
  inbuf = (char*) malloc(inbuf_len * sizeof(char));
  outbuf = (char*) malloc(outbuf_len * sizeof(char));
  tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
  assert(tmp == 0);
  tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
  assert(tmp == 0);

  int N, C, R;
  int *K, *S, *E;
  tmp = scanf("%d %d %d", &N, &C, &R);
  assert(tmp == 3);
  K = (int*) malloc((N-1) * sizeof(int));
  S = (int*) malloc(C * sizeof(int));
  E = (int*) malloc(C * sizeof(int));
  int i;
  for (i = 0; i < N-1; i++) {
    tmp = scanf("%d", &K[i]);
    assert(tmp == 1);
  }
  for (i = 0; i < C; i++) {
    tmp = scanf("%d %d", &S[i], &E[i]);
    assert(tmp == 2);
  }

  printf("%d\n", GetBestPosition(N, C, R, K, S, E));

  return 0;

}
*/

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}

const int maxn = 1e5+10;

int bit[maxn];
vector <pii> range;
int lst[maxn],nxt[maxn];

void update(int pos,int val) {
	while (pos<maxn) {
		bit[pos]+=val;
		pos+=lowbit(pos);
	}
}

int query(int pos) {
	int ret=0;
	while (pos) {
		ret+=bit[pos];
		pos-=lowbit(pos);
	}
	return ret;
}


int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
	rep(i,1,N+2) update(i,1),nxt[i] = i+1;
	rep(i,0,C) {
		S[i]++,E[i]++;
		int l = 1,r = N;
		while (l<r) {
			int mid=l+r>>1;
			if (query(mid)<S[i]) l = mid+1;
			else r = mid; 
		}
		int cur = nxt[l];
		while (S[i]<E[i]) {
			update(cur,-1);
			cur = nxt[cur];
			S[i]++;
		}
		nxt[l] = cur;
		range.pb({l,cur});
//		cout<<l<<" "<<cur<<endl; 
	}
	sort(ALL(range));
	int tmp = -1;
	rep(i,0,N-1) {
		lst[i+1] = tmp;
		if (K[i]>R) tmp=i+1;
	}
	lst[N] = tmp;
	tmp = N+1;
	for (int i=N-2;i>=0;i--) {
		nxt[i+2] = tmp;
		if (K[i]>R) tmp=i+2;
	}
	nxt[1] = tmp;
	int bg=0,ed=0;
	memset(bit,0,sizeof(bit));
	int cnt = -1,ans = 0;
	rep(i,1,N+1) {
		while (ed<SZ(range) and range[ed].fi<=i) update(range[ed].se,1),ed++;
		while (bg<ed and range[bg].fi<=lst[i]) update(range[bg].se,-1),bg++;
		int cur = query(nxt[i])-query(i);
		if (cur>cnt) cnt=cur,ans = i;
		debug(cur);
	}
	return ans-1;
}


/*
5 3 3
1
0
2
4
1 3
0 1
0 1
*/

Compilation message

tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:96:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   96 |    int mid=l+r>>1;
      |            ~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 700 KB Output is correct
3 Correct 4 ms 588 KB Output is correct
4 Correct 4 ms 724 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 4 ms 596 KB Output is correct
9 Correct 3 ms 692 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 700 KB Output is correct
2 Correct 27 ms 968 KB Output is correct
3 Correct 30 ms 804 KB Output is correct
4 Correct 29 ms 964 KB Output is correct
5 Correct 27 ms 852 KB Output is correct
6 Correct 30 ms 900 KB Output is correct
7 Correct 26 ms 968 KB Output is correct
8 Correct 26 ms 900 KB Output is correct
9 Correct 25 ms 796 KB Output is correct
10 Correct 29 ms 984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 2768 KB Output is correct
2 Correct 532 ms 5740 KB Output is correct
3 Correct 518 ms 3308 KB Output is correct
4 Correct 593 ms 5964 KB Output is correct
5 Correct 509 ms 5696 KB Output is correct
6 Correct 546 ms 4900 KB Output is correct
7 Correct 596 ms 5856 KB Output is correct
8 Correct 579 ms 5784 KB Output is correct
9 Correct 558 ms 3008 KB Output is correct
10 Correct 534 ms 3108 KB Output is correct