QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#139761#2211. IOI Problem Revisedcrazy_seaAC ✓833ms45236kbC++114.2kb2023-08-14 15:00:432023-08-14 15:00:44

Judging History

你现在查看的是最新测评结果

  • [2023-08-14 15:00:44]
  • 评测
  • 测评结果:AC
  • 用时:833ms
  • 内存:45236kb
  • [2023-08-14 15:00:43]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
#define mid ((l+r)>>1)
#define ll long long
using namespace std;
const int N=5e5+10;
const ll inf=3e17;
int n,k;
ll s[N],L,a[N],ans;
ll calc(int l,int r)
{
	if(l>=r) return 0;
	return a[mid]*(mid-l)-(s[mid-1]-s[l-1])+(s[r-1]-s[mid])-a[mid]*(r-mid-1);
}
namespace subtask1
{
	ll dp[N];
	int g[N];
	ll calc2(int i,int j){return dp[i]+2*calc(i,j);}
	int head,tail,lst[N];
	struct node{
		int l,r,k;
		ll res;
	}t[N];
	node create(int l,int r,int k){return node{l,r,k,calc2(k,l)};}
	void work(ll k)
	{
		int hd,tl;
		dp[1]=g[1]=0;
		t[hd=tl=1]=create(2,n+1,1);
		for(int i=2;;i++)
		{
			int w=t[hd].k;
			dp[i]=calc2(w,i)+k;
			g[i]=g[w]+1,lst[i]=w;
			if(i==n+1) break;
			if(t[hd].l==t[hd].r) hd++;
			else t[hd].l++,t[hd].res=calc2(t[hd].k,i+1);
			while(hd<=tl&&calc2(i,t[tl].l)<=t[tl].res) tl--;
			if(tl<hd) t[++tl]=create(i+1,n+1,i);
			else
			{
				int l=t[tl].l,r=t[tl].r+1,w=t[tl].k;
				while(l<r)
				{
					if(calc2(w,mid)>=calc2(i,mid)) r=mid;
					else l=mid+1;
				}
				t[tl].r=l-1;
				t[++tl]=create(l,n+1,i);
			}
		}
	}
	vector<int> work2()
	{
		int w=n+1;
		vector<int> v;
		while(w)
		{
			v.push_back(w);
			w=lst[w];
		}
		reverse(v.begin(),v.end());
		return v;
	}
	vector<int> solve()
	{
		ll l=-1,r=inf;
		while(l<r)
		{
			work(mid*2+1);
			if(g[n+1]>k) l=mid+1;
			else r=mid;
		}
		l=l*2+1;
		work(l);
		vector<int> v1=work2();
		if(g[n+1]==k) return v1;
		work(l-2);
		vector<int> v2=work2();
		if(g[n+1]==k) return v2;
		int m=v2.size();
		for(int i=1,j=0;i<m;i++)
		{
			while(v1[j]<v2[i]) j++;
			if(v2[i-1]>=v1[j-1])
			{
				if(m-i+j==k+1)
				{
					vector<int> v;
					for(int w=0;w<j;w++) v.push_back(v1[w]);
					for(int w=i;w<m;w++) v.push_back(v2[w]);
					return v;
				}
			}
		}
		return vector<int>();
	}
}
vector<int> ANS;
namespace subtask2
{
	ll f[N][2];
	int lst[N][2];
	void work(int l,int r,int L,int R,int p)
	{
		if(l>r) return;
		ll s=inf;
		for(int i=L;i<=R&&i<=mid;i++)
		{
			ll x=f[i][p^1]+calc(i,mid);
			if(x<s) s=x,lst[mid][p]=i;
		}
		f[mid][p]=s;
		work(l,mid-1,L,lst[mid][p],p);
		work(mid+1,r,lst[mid][p],R,p);
	}
	void solve(int l,int r,vector<pair<int,int>> v)
	{
		if(l>r) return;
		int m=(int)v.size();
		for(int i=v[0].first;i<=v[0].second;i++) f[i][0]=calc(mid,i);
		for(int i=1;i<m;i++)
			work(v[i].first,v[i].second,v[i-1].first,v[i-1].second,i&1);
		auto k=v.back();
		int w=k.first,p=(m-1)&1;
		vector<int> s;
		for(int i=k.first;i<=k.second;i++) f[i][p]+=calc(i,mid+n);
		for(int i=k.first+1;i<=k.second;i++) if(f[i][p]<f[w][p]) w=i;
		int flag=0;
		if(f[w][p]<ans) ans=f[w][p],flag=1;
		for(int i=m-1;i>=0;i--) s.push_back(w),w=lst[w][i&1];
		reverse(s.begin(),s.end());
		if(flag)
		{
			s.push_back(mid);
			ANS=s;
			s.pop_back();
		}
		if(l==r) return;
		vector<pair<int,int>> v1(m),v2(m);
		for(int i=0;i<m;i++)
		{
			v1[i]=make_pair(v[i].first,s[i]);
			v2[i]=make_pair(s[i],v[i].second);
		}
		solve(l,mid-1,v1);
		solve(mid+1,r,v2);
	}
	void solve(vector<int> v)
	{
		for(int i=0;i<=k;i++) v.push_back(v[i]+n);
		vector<pair<int,int>> V(k-1);
		int w=0;
		for(int i=0;i<k;i++)
			if(v[i+1]-v[i]<=n/k)
			{
				w=i;
				break;
			}
		for(int i=0;i<k-1;i++)
			V[i]=make_pair(v[w+i+1],v[w+i+2]);
		solve(v[w],v[w+1],V);
	}
}
void work(vector<int> &v)
{
	for(int i=0;i<(int)v.size();i++)
		if(v[i]>n) v[i]-=n;
	sort(v.begin(),v.end());
}
ll b[N];
int main()
{
	scanf("%d%d%lld",&n,&k,&L);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for(int i=n+1;i<=n*2;i++) a[i]=a[i-n]+L;
	for(int i=1;i<=2*n;i++) s[i]=s[i-1]+a[i];
	if(k==1)
	{
		ll ans=inf;
		int w=0;
		for(int i=1;i<=n;i++)
		{
			ll x=calc(i,i+n);
			if(x<ans) ans=x,w=i;
		}
		printf("%lld\n%d\n",ans,w);
		return 0;
	}
	vector<int> v=subtask1::solve();
	ans=0;
	for(int i=1;i<(int)v.size();i++) ans+=calc(v[i-1],v[i]);
	v.pop_back();
	ANS=v;
	subtask2::solve(v);
	work(ANS);
	printf("%lld\n",ans);
	ANS.push_back(ANS[0]+n);
	for(int i=0;i<k;i++)
	{
		int x=(ANS[i]+ANS[i+1])>>1;
		if(x>n) x-=n;
		b[i]=a[x];
	}
	sort(b,b+k);
	for(int i=0;i<k;i++) printf("%lld ",b[i]);
	printf("\n");
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 17472kb

Test #2:

score: 0
Accepted
time: 1ms
memory: 19476kb

Test #3:

score: 0
Accepted
time: 4ms
memory: 19552kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 19484kb

Test #5:

score: 0
Accepted
time: 1ms
memory: 19552kb

Test #6:

score: 0
Accepted
time: 1ms
memory: 19544kb

Test #7:

score: 0
Accepted
time: 6ms
memory: 19584kb

Test #8:

score: 0
Accepted
time: 3ms
memory: 19592kb

Test #9:

score: 0
Accepted
time: 11ms
memory: 19644kb

Test #10:

score: 0
Accepted
time: 11ms
memory: 19672kb

Test #11:

score: 0
Accepted
time: 103ms
memory: 25908kb

Test #12:

score: 0
Accepted
time: 81ms
memory: 20636kb

Test #13:

score: 0
Accepted
time: 118ms
memory: 23788kb

Test #14:

score: 0
Accepted
time: 89ms
memory: 22456kb

Test #15:

score: 0
Accepted
time: 103ms
memory: 24060kb

Test #16:

score: 0
Accepted
time: 610ms
memory: 41836kb

Test #17:

score: 0
Accepted
time: 668ms
memory: 38676kb

Test #18:

score: 0
Accepted
time: 661ms
memory: 38868kb

Test #19:

score: 0
Accepted
time: 773ms
memory: 35536kb

Test #20:

score: 0
Accepted
time: 600ms
memory: 42208kb

Test #21:

score: 0
Accepted
time: 610ms
memory: 41804kb

Test #22:

score: 0
Accepted
time: 612ms
memory: 44360kb

Test #23:

score: 0
Accepted
time: 706ms
memory: 38160kb

Test #24:

score: 0
Accepted
time: 685ms
memory: 38740kb

Test #25:

score: 0
Accepted
time: 638ms
memory: 39956kb

Test #26:

score: 0
Accepted
time: 709ms
memory: 35040kb

Test #27:

score: 0
Accepted
time: 701ms
memory: 38832kb

Test #28:

score: 0
Accepted
time: 663ms
memory: 38892kb

Test #29:

score: 0
Accepted
time: 750ms
memory: 35944kb

Test #30:

score: 0
Accepted
time: 561ms
memory: 44632kb

Test #31:

score: 0
Accepted
time: 624ms
memory: 42232kb

Test #32:

score: 0
Accepted
time: 618ms
memory: 43876kb

Test #33:

score: 0
Accepted
time: 613ms
memory: 42664kb

Test #34:

score: 0
Accepted
time: 653ms
memory: 41628kb

Test #35:

score: 0
Accepted
time: 625ms
memory: 41840kb

Test #36:

score: 0
Accepted
time: 571ms
memory: 44308kb

Test #37:

score: 0
Accepted
time: 624ms
memory: 41524kb

Test #38:

score: 0
Accepted
time: 603ms
memory: 41828kb

Test #39:

score: 0
Accepted
time: 775ms
memory: 33012kb

Test #40:

score: 0
Accepted
time: 573ms
memory: 45028kb

Test #41:

score: 0
Accepted
time: 772ms
memory: 34548kb

Test #42:

score: 0
Accepted
time: 610ms
memory: 42284kb

Test #43:

score: 0
Accepted
time: 742ms
memory: 37132kb

Test #44:

score: 0
Accepted
time: 600ms
memory: 44204kb

Test #45:

score: 0
Accepted
time: 559ms
memory: 44268kb

Test #46:

score: 0
Accepted
time: 599ms
memory: 43348kb

Test #47:

score: 0
Accepted
time: 699ms
memory: 39060kb

Test #48:

score: 0
Accepted
time: 735ms
memory: 35864kb

Test #49:

score: 0
Accepted
time: 759ms
memory: 35124kb

Test #50:

score: 0
Accepted
time: 646ms
memory: 38796kb

Test #51:

score: 0
Accepted
time: 636ms
memory: 42064kb

Test #52:

score: 0
Accepted
time: 633ms
memory: 40044kb

Test #53:

score: 0
Accepted
time: 734ms
memory: 36440kb

Test #54:

score: 0
Accepted
time: 706ms
memory: 37376kb

Test #55:

score: 0
Accepted
time: 617ms
memory: 42988kb

Test #56:

score: 0
Accepted
time: 763ms
memory: 33824kb

Test #57:

score: 0
Accepted
time: 722ms
memory: 34676kb

Test #58:

score: 0
Accepted
time: 738ms
memory: 34948kb

Test #59:

score: 0
Accepted
time: 657ms
memory: 40084kb

Test #60:

score: 0
Accepted
time: 569ms
memory: 43936kb

Test #61:

score: 0
Accepted
time: 723ms
memory: 36960kb

Test #62:

score: 0
Accepted
time: 662ms
memory: 39852kb

Test #63:

score: 0
Accepted
time: 761ms
memory: 32796kb

Test #64:

score: 0
Accepted
time: 563ms
memory: 45236kb

Test #65:

score: 0
Accepted
time: 699ms
memory: 32400kb

Test #66:

score: 0
Accepted
time: 689ms
memory: 35164kb

Test #67:

score: 0
Accepted
time: 657ms
memory: 40544kb

Test #68:

score: 0
Accepted
time: 662ms
memory: 40212kb

Test #69:

score: 0
Accepted
time: 832ms
memory: 32028kb

Test #70:

score: 0
Accepted
time: 829ms
memory: 33048kb

Test #71:

score: 0
Accepted
time: 833ms
memory: 30676kb

Test #72:

score: 0
Accepted
time: 827ms
memory: 31912kb

Extra Test:

score: 0
Extra Test Passed