답안 #113407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
113407 2019-05-25T12:14:34 Z TadijaSebez Sails (IOI07_sails) C++11
50 / 100
1000 ms 4148 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100050;
int sum[N],cnt[N];
ll suf[N];
const int M=2*N;
int ls[M],rs[M],tsz,root;
pair<int,int> mn[M];
pair<int,int> Get(int c, int ss, int se, int qs, int qe)
{
	if(qs<=ss && qe>=se) return mn[c];
	int mid=ss+se>>1;
	if(qe<=mid) return Get(ls[c],ss,mid,qs,qe);
	if(qs>mid) return Get(rs[c],mid+1,se,qs,qe);
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
void Set(int &c, int ss, int se, int qi, int f)
{
	if(!c) c=++tsz;
	if(ss==se){ mn[c]={f,ss};return;}
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,f);
	else Set(rs[c],mid+1,se,qi,f);
	mn[c]=min(mn[ls[c]],mn[rs[c]]);
}
int main()
{
	int n,h,k,mxh=0;
	scanf("%i",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%i %i",&h,&k);
		cnt[h]++;
		sum[h-k+1]++;
		sum[h+1]--;
		mxh=max(mxh,h);
	}
	for(int i=1;i<=mxh;i++) sum[i]+=sum[i-1];

	/*while(1)
	{
		bool ok=0;
		for(int i=mxh-1;i>=1;i--) if(sum[i]<sum[i+1])
		{
			int z=sum[i]+sum[i+1];
			sum[i]=(z+1)/2;
			sum[i+1]=z/2;
			ok=1;
		}
		if(!ok) break;
	}*/
	Set(root,1,mxh,1,sum[1]);
	for(int i=2;i<=mxh;i++)
	{
		while(1)
		{
			pair<int,int> p=Get(root,1,mxh,1,i-1);
			if(p.first>=sum[i]) break;
			sum[p.second]++;
			sum[i]--;
			Set(root,1,mxh,p.second,sum[p.second]);
		}
		Set(root,1,mxh,i,sum[i]);
	}

	ll ans=0;
	for(int i=1;i<=mxh;i++) ans+=(ll)sum[i]*(sum[i]-1)/2;
	/*for(int i=mxh;i>=1;i--) suf[i]=suf[i+1]+(ll)sum[i]*(sum[i]-1)/2;
	ll tmp=0,ans=suf[1];
	for(int i=1;i<=mxh;i++)
	{
		tmp+=sum[i];
		ll all=tmp/i,ost=tmp%i;
		ll now=suf[i+1]+(i-ost)*all*(all-1)/2+ost*all*(all+1)/2;
		ans=min(ans,now);
	}*/

	printf("%lld\n",ans);
	return 0;
}

Compilation message

sails.cpp: In function 'std::pair<int, int> Get(int, int, int, int, int)':
sails.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
sails.cpp: In function 'void Set(int&, int, int, int, int)':
sails.cpp:22:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
sails.cpp: In function 'int main()':
sails.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
sails.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&h,&k);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 604 KB Output is correct
2 Correct 365 ms 4148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1068 ms 960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 1516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1059 ms 1112 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1066 ms 1204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1067 ms 1432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1075 ms 1500 KB Time limit exceeded
2 Halted 0 ms 0 KB -