답안 #52878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52878 2018-06-27T05:58:34 Z zetapi 휴가 (IOI14_holiday) C++14
100 / 100
1878 ms 24168 KB
//#include <holiday.h>
#include <bits/stdc++.h>
using namespace std;
 
#define pb  push_back
#define mp  make_pair
#define ll long long
#define itr ::iterator 
 
typedef pair<ll,ll>  pii;
 
const int MAX=3e5;
 
struct data
{
	ll ind;
	ll sum;
}	seg[MAX];
 
vector<pii> sorted;
 
ll N,D,Start,pos[MAX],R[MAX],f[MAX][2],g[MAX][2],fans[MAX][2],gans[MAX][2];
 
void build(int low,int high,int node)
{
	if(low==high)
	{
		if(low<=sorted.size())
		{
			seg[node].ind=1;
			seg[node].sum=sorted[low-1].first;
		}
		else
		{
			seg[node].ind=0;
			seg[node].sum=0;
		}
		return ;
	}
	int mid=(low+high)/2;
	build(low,mid,2*node);
	build(mid+1,high,2*node+1);
	seg[node].ind=seg[2*node].ind+seg[2*node+1].ind;
	seg[node].sum=seg[2*node].sum+seg[2*node+1].sum;
	return ;
}
 
void update(int low,int high,int node,int idx,int delta)
{
	if(idx>sorted.size() or idx<1)
		return ;
	if(low==high)
	{
		R[low]=delta;
		seg[node].ind=delta;
		if(delta)
			seg[node].sum=sorted[low-1].first;
		else
			seg[node].sum=0;
		return ;
	}
	int mid=(low+high)/2;
	if(idx>=low and idx<=mid)
		update(low,mid,2*node,idx,delta);
	else
		update(mid+1,high,2*node+1,idx,delta);
	seg[node].ind=seg[2*node].ind+seg[2*node+1].ind;
	seg[node].sum=seg[2*node].sum+seg[2*node+1].sum;
	return ;
} 
 
ll query(int low,int high,int node,int K)
{
	if(low==high)
		return seg[node].sum;
	int mid=(low+high)/2;
	if(seg[2*node].ind>=K)
		return query(low,mid,2*node,K);
	return seg[2*node].sum+query(mid+1,high,2*node+1,K-seg[2*node].ind);
}
 
// int active[MAX];

ll cal(int idx,int qlow,int qhigh,int type)
{
	int rem;
	pii res=mp(0,-qlow);
	//for(int A=qhigh+1;A<=N;A++)
	//	/if(active[A])
	///	{
	//		cout<<"ho ho "<<A<<" "<<type<<" "<<qlow<<" "<<qhigh<<" "<<idx<<"\n";
	//	}
	//	update(1,N,1,pos[qhigh+1],0);
	for(int A=qhigh;A>=qlow;A--)
	{
		rem=idx-(type+1)*(A-Start);
		if(rem>0)
			res=max(res,mp(query(1,N,1,rem),(ll)-A));
		update(1,N,1,pos[A],0);
	}
	for(int A=qlow;A<=qhigh;A++)
		update(1,N,1,pos[A],1);
	fans[idx][type]=res.first;
	return -res.second;
}
 
//int active[MAX];

ll cal2(int idx,int qlow,int qhigh,int type)
{
	int rem;
	pii res=mp(0,qhigh);
	//for(int A=1;A<qlow;A++)
	//	update(1,N,1,pos[qlow-1],0);
	for(int A=qlow;A<=qhigh;A++)
	{	
		rem=idx-(type+1)*(Start-A);
		if(rem>0)
			res=max(res,mp(query(1,N,1,rem),(ll)A));
		update(1,N,1,pos[A],0);
	}
	for(int A=qlow;A<=qhigh;A++)
		update(1,N,1,pos[A],1);
	gans[idx][type]=res.first;
	return res.second;
}
 
void cal(int low,int high,int qlow,int qhigh,int type)
{
	if(low>high)
	{
		for(int A=qlow+1;A<=qhigh;A++)
			update(1,N,1,pos[A],0);
		return ;
	}
	//cout<<low<<" "<<high<<" "<<qlow<<" "<<qhigh<<"\n";
	int mid=(low+high)/2;
	f[mid][type]=cal(mid,qlow,qhigh,type);
	if(low==high)
	{
		//if(qlow<qhigh)
		//	cout<<"iactive "<<qlow+1<<" "<<qhigh<<"\n"; 
		for(int A=qlow+1;A<=qhigh;A++)
			update(1,N,1,pos[A],0);
		//cout<<active[5]<<"dekho \n";
		return ;
	}
	cal(mid+1,high,f[mid][type],qhigh,type);
	cal(low,mid-1,qlow,f[mid][type],type);
	return ;
}
 
void cal2(int low,int high,int qlow,int qhigh,int type)
{
	if(low>high)
	{
		for(int A=qlow;A<qhigh;A++)
			update(1,N,1,pos[A],0);
		return ;
	}
	int mid=(low+high)/2;
	g[mid][type]=cal2(mid,qlow,qhigh,type);
	if(low==high)
	{
		for(int A=qlow;A<qhigh;A++)
			update(1,N,1,pos[A],0);
		return ;
	}
	cal2(mid+1,high,qlow,g[mid][type],type);
	cal2(low,mid-1,g[mid][type],qhigh,type);
	return ;
}
 
ll find(int D)
{
	ll res=0;
	for(int A=0;A<=D;A++)
	{
		res=max(res,fans[A][1]+gans[D-A][0]);
		res=max(res,gans[A][1]+fans[D-A][0]);
	}
	return res;
}
 
long long int findMaxAttraction(int n,int start,int d,int attraction[])
{
	N=n;
	D=d;
	Start=start+1;
	ll cur,res=0,tem=attraction[start];
	attraction[start]=0;
	for(int A=start;A<N;A++)
		sorted.pb(mp(attraction[A],A+1));
	sort(sorted.begin(),sorted.end());
	reverse(sorted.begin(),sorted.end());
	for(int A=0;A<sorted.size();A++)
		pos[sorted[A].second]=A+1;
	build(1,N,1);
	cal(1,D,Start,N,0);
	build(1,N,1);
	cal(1,D,Start,N,1);
	sorted.clear();
	for(int A=0;A<=start;A++)
		sorted.pb(mp(attraction[A],A+1));
	sort(sorted.begin(),sorted.end());
	reverse(sorted.begin(),sorted.end());
	for(int A=0;A<sorted.size();A++)
		pos[sorted[A].second]=A+1;
	build(1,N,1);
	cal2(1,D,1,Start,0);
	build(1,N,1);
	cal2(1,D,1,Start,1);
	f[0][0]=Start;
	f[0][1]=Start;
	g[0][0]=Start;
	g[0][1]=Start;
	res=max(res,find(D));
	res=max(res,tem+find(D-1));
	return res;
}
 
/*int main()
{
	ios_base::sync_with_stdio(false);
 
 
	int n,d,s,attraction[100000]={10,2,20,30,1};
	cout<<findMaxAttraction( n=5, s=2, d=7, attraction);
	return 0;
}*/

Compilation message

holiday.cpp: In function 'void build(int, int, int)':
holiday.cpp:28:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(low<=sorted.size())
      ~~~^~~~~~~~~~~~~~~
holiday.cpp: In function 'void update(int, int, int, int, int)':
holiday.cpp:50:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(idx>sorted.size() or idx<1)
     ~~~^~~~~~~~~~~~~~
holiday.cpp: In function 'long long int findMaxAttraction(int, int, int, int*)':
holiday.cpp:196:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int A=0;A<sorted.size();A++)
              ~^~~~~~~~~~~~~~
holiday.cpp:207:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int A=0;A<sorted.size();A++)
              ~^~~~~~~~~~~~~~
holiday.cpp:190:5: warning: unused variable 'cur' [-Wunused-variable]
  ll cur,res=0,tem=attraction[start];
     ^~~
grader.cpp: In function 'int main()':
grader.cpp:7:12: warning: variable 'n_s' set but not used [-Wunused-but-set-variable]
     int i, n_s;
            ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 2 ms 544 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1424 ms 20964 KB Output is correct
2 Correct 1456 ms 21028 KB Output is correct
3 Correct 1733 ms 21028 KB Output is correct
4 Correct 1762 ms 21028 KB Output is correct
5 Correct 1772 ms 21028 KB Output is correct
6 Correct 592 ms 21028 KB Output is correct
7 Correct 887 ms 21028 KB Output is correct
8 Correct 1148 ms 21028 KB Output is correct
9 Correct 534 ms 21028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 21028 KB Output is correct
2 Correct 28 ms 21028 KB Output is correct
3 Correct 28 ms 21028 KB Output is correct
4 Correct 26 ms 21028 KB Output is correct
5 Correct 23 ms 21028 KB Output is correct
6 Correct 8 ms 21028 KB Output is correct
7 Correct 7 ms 21028 KB Output is correct
8 Correct 7 ms 21028 KB Output is correct
9 Correct 8 ms 21028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1581 ms 24060 KB Output is correct
2 Correct 1610 ms 24168 KB Output is correct
3 Correct 622 ms 24168 KB Output is correct
4 Correct 27 ms 24168 KB Output is correct
5 Correct 9 ms 24168 KB Output is correct
6 Correct 9 ms 24168 KB Output is correct
7 Correct 9 ms 24168 KB Output is correct
8 Correct 1878 ms 24168 KB Output is correct
9 Correct 1827 ms 24168 KB Output is correct