QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#887977 | #2211. IOI Problem Revised | lizhous | AC ✓ | 1004ms | 75324kb | C++14 | 5.6kb | 2025-02-07 21:09:09 | 2025-02-07 21:09:10 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<unordered_map>
#include<map>
#include<random>
#define int long long
using namespace std;
long long n,k,L,a[1000001],sum[1000001];
long long ans;
vector <long long> rans;
int w(int l,int r)
{
if(l==0) l=1;
if(l>r) return 0;
int mid=l+r>>1;
return a[mid]*(mid-l+1)-(sum[mid]-sum[l-1])+(sum[r]-sum[mid])-a[mid]*(r-mid);
}
struct seg
{
int l,r;
};
vector <seg> jmp,jmp2;
namespace wqs
{
struct segg
{
int i,l,r;
};
deque <segg> q;
int f[1000001],frm[2][1000001],cnt[1000001];
bool op(int a,int b,bool c)
{
if(c==0) return a>b;
return a>=b;
}
int get(int mid,bool c)
{
// cerr<<mid<<"\n";
while(!q.empty()) q.pop_front();
q.push_back({0,1,n});
for(int i=1;i<=n;i++)
{
while(!q.empty()&&q.front().r<i) q.pop_front();
f[i]=f[q.front().i]+w(q.front().i+1,i)+mid;
// cerr<<q.front().i<<" -> "<<i<<" "<<f[i]<<"\n";
if(!c) cnt[i]=cnt[q.front().i]+1;
frm[c][i]=q.front().i;
while(!q.empty()&&q.front().r<=i) q.pop_front();
while(!q.empty()&&q.back().l>i&&op(f[q.back().i]+w(q.back().i+1,q.back().l),f[i]+w(i+1,q.back().l),c))
{
q.pop_back();
}
if(q.size())
{
int l=max(i+1,q.back().l),r=q.back().r,real=r+1;
// cerr<<l<<' '<<r<<"\n";
while(l<=r)
{
int mid=l+r>>1;
if(op(f[q.back().i]+w(q.back().i+1,mid),f[i]+w(i+1,mid),c))
{
r=mid-1;
real=mid;
}
else
{
l=mid+1;
}
}
q.back().r=real-1;
if(q.back().l==real) q.pop_back();
if(real<=n) q.push_back((segg){i,real,n});
}
else
{
q.push_back((segg){i,i+1,n});
}
}
return cnt[n];
}
void mian()
{
int l=0,r=1000000000000000000,real=r;
while(l<=r)
{
int mid=l+r>>1;
if(get(mid,0)<=k)
{
r=mid-1;
real=mid;
}
else
{
l=mid+1;
}
}
get(real,0);
get(real,1);
// for(int i=1;i<=n;i++)
// {
// cerr<<frm[0][i]<<' '<<frm[1][i]<<" "<<cnt[i]<<"\n";
// }
// return ;
int ned=k,now=n,las=n;
while(ned&&now)
{
// cerr<<ned<<' '<<now<<"\n";
if(cnt[frm[1][now]]<ned)
{
// cerr<<"JER";
now=frm[1][now];
ned--;
}
else
{
for(int i=frm[0][now];i<=frm[1][now];i++)
{
if(f[i]+w(i+1,now)+real==f[now])
{
// cerr<<i<<" : "<<cnt[i]<<"\n";
if(cnt[i]==ned-1)
{
now=i;
ned--;
break;
}
}
}
}
jmp.push_back((seg){now+1,las});
las=now;
}
}
}
namespace jumper
{
int f[2][1000001],frm[2][1000001],I;
void wk(seg a,seg b) //from a to b
{
if(b.l>b.r) return;
if(b.l==b.r)
{
f[I][b.l]=4000000000000000000;
for(int i=a.l;i<=a.r;i++)
{
if(f[I^1][i]+w(i,b.l-1)<f[I][b.l])
{
f[I][b.l]=f[I^1][i]+w(i,b.l-1);
frm[I][b.l]=i;
}
}
// cerr<<frm[I][b.l]<<" -> "<<b.l<<" "<<f[I][b.l]<<"\n";
return;
}
int mid=b.l+b.r>>1;
f[I][mid]=4000000000000000000;
for(int i=a.l;i<=a.r;i++)
{
if(f[I^1][i]+w(i,mid-1)<f[I][mid])
{
f[I][mid]=f[I^1][i]+w(i,mid-1);
frm[I][mid]=i;
}
}
//cerr<<frm[I][mid]<<" -> "<<mid<<' '<<f[I][mid]<<"\n";
wk((seg){a.l,frm[I][mid]},(seg){b.l,mid-1});
wk((seg){frm[I][mid],a.r},(seg){mid+1,b.r});
}
vector <int> run(int st,vector <seg> jmp)
{
//cerr<<st<<" : \n";
//for(seg w:jmp) cerr<<w.l<<" "<<w.r<<"\n";
jmp[0]={st,st};
f[I][st]=0;
for(int i=1;i<k;i++)
{
I^=1;
wk(jmp[i-1],jmp[i]);
}
I^=1;
wk(jmp[k-1],(seg){st+n,st+n});
//cerr<<">>"<<f[I][st+n]<<"\n";
vector <int> ret;
int now=st+n,II=I;
while(now!=st)
{
// cerr<<now<<"\n";
now=frm[I][now];
ret.push_back(now);
I^=1;
//cout<<now<<" ";
}
//cout<<'\n';
reverse(ret.begin(),ret.end());
if(f[II][st+n]<ans)
{
ans=f[II][st+n];
rans.clear();
for(int i=0;i<k-1;i++)
{
rans.push_back(((ret[i]+(ret[i+1]-1))/2));
}
// //cout<<
rans.push_back(((ret[k-1]+(ret[0]+n-1))/2));
}
return ret;
}
void work(int l,int r,vector<seg> jmp) //begin pot in [l,r] jumping
{
if(l>r) return;
int mid=l+r>>1;
vector <int> pm=run(mid,jmp);
vector <seg> nex;
for(int i=0;i<k;i++)
{
nex.push_back((seg){jmp[i].l,pm[i]});
}
work(l,mid-1,nex);
nex.clear();
for(int i=0;i<k;i++)
{
nex.push_back((seg){pm[i],jmp[i].r});
}
work(mid+1,r,nex);
}
void mian()
{
int miw=0;
for(int i=0;i<k;i++)
{
if(jmp[i].r-jmp[i].l<jmp[miw].r-jmp[miw].l) miw=i;
}
for(int i=0;i<k;i++)
{
jmp2.push_back(jmp[(i+miw)%k]);
}
for(int i=1;i<k;i++)
{
if(jmp2[i].l<jmp2[i-1].l)
{
jmp2[i].l+=n;
jmp2[i].r+=n;
}
//cerr<<jmp2[i].l<<' '<<jmp2[i].r<<'\n';
}
jmp=jmp2;
work(jmp[0].l,jmp[0].r,jmp);
}
}
signed main()
{
//freopen("compete.in","r",stdin);
//freopen("compete.out","w",stdout);
// freopen("mc\\compete\\compete5.in","r",stdin);
// freopen("mc\\compete\\compete1.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n>>k>>L;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
for(int i=n+1;i<=n+n+n;i++)
{
a[i]=a[i-n]+L;
sum[i]=sum[i-1]+a[i];
}
ans=4000000000000000000;
wqs::mian();
reverse(jmp.begin(),jmp.end());
for(int i=0;i<k;i++) jmp[i].r++;
if(jmp[0].l!=1) return -1;
jumper::mian();
cout<<ans<<"\n";
int len=0;
for(int w:rans)
{
sum[++len]=a[(w%n==0?n:w%n)]%L;
}
sort(sum+1,sum+len+1);
for(int i=1;i<=len;i++)
cout<<sum[i]<<' ';
}
/*
5 2 10
0 1 2 6 9
*/
这程序好像有点Bug,我给组数据试试?
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 18156kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 18152kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 17992kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 18156kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 18156kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 18024kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 18068kb
Test #8:
score: 0
Accepted
time: 4ms
memory: 18220kb
Test #9:
score: 0
Accepted
time: 12ms
memory: 20492kb
Test #10:
score: 0
Accepted
time: 10ms
memory: 18072kb
Test #11:
score: 0
Accepted
time: 126ms
memory: 23348kb
Test #12:
score: 0
Accepted
time: 126ms
memory: 23560kb
Test #13:
score: 0
Accepted
time: 132ms
memory: 20892kb
Test #14:
score: 0
Accepted
time: 121ms
memory: 25128kb
Test #15:
score: 0
Accepted
time: 122ms
memory: 23740kb
Test #16:
score: 0
Accepted
time: 933ms
memory: 70516kb
Test #17:
score: 0
Accepted
time: 902ms
memory: 58996kb
Test #18:
score: 0
Accepted
time: 899ms
memory: 58468kb
Test #19:
score: 0
Accepted
time: 931ms
memory: 47152kb
Test #20:
score: 0
Accepted
time: 944ms
memory: 71688kb
Test #21:
score: 0
Accepted
time: 941ms
memory: 74832kb
Test #22:
score: 0
Accepted
time: 935ms
memory: 71188kb
Test #23:
score: 0
Accepted
time: 887ms
memory: 56452kb
Test #24:
score: 0
Accepted
time: 901ms
memory: 59560kb
Test #25:
score: 0
Accepted
time: 928ms
memory: 59488kb
Test #26:
score: 0
Accepted
time: 897ms
memory: 57048kb
Test #27:
score: 0
Accepted
time: 894ms
memory: 59396kb
Test #28:
score: 0
Accepted
time: 920ms
memory: 61192kb
Test #29:
score: 0
Accepted
time: 892ms
memory: 50260kb
Test #30:
score: 0
Accepted
time: 991ms
memory: 74748kb
Test #31:
score: 0
Accepted
time: 937ms
memory: 70560kb
Test #32:
score: 0
Accepted
time: 938ms
memory: 72144kb
Test #33:
score: 0
Accepted
time: 957ms
memory: 70996kb
Test #34:
score: 0
Accepted
time: 934ms
memory: 70980kb
Test #35:
score: 0
Accepted
time: 928ms
memory: 72224kb
Test #36:
score: 0
Accepted
time: 986ms
memory: 74836kb
Test #37:
score: 0
Accepted
time: 928ms
memory: 70880kb
Test #38:
score: 0
Accepted
time: 966ms
memory: 73176kb
Test #39:
score: 0
Accepted
time: 940ms
memory: 49192kb
Test #40:
score: 0
Accepted
time: 983ms
memory: 75324kb
Test #41:
score: 0
Accepted
time: 945ms
memory: 44476kb
Test #42:
score: 0
Accepted
time: 954ms
memory: 70056kb
Test #43:
score: 0
Accepted
time: 914ms
memory: 49152kb
Test #44:
score: 0
Accepted
time: 955ms
memory: 72976kb
Test #45:
score: 0
Accepted
time: 969ms
memory: 73028kb
Test #46:
score: 0
Accepted
time: 958ms
memory: 72784kb
Test #47:
score: 0
Accepted
time: 896ms
memory: 58340kb
Test #48:
score: 0
Accepted
time: 906ms
memory: 49492kb
Test #49:
score: 0
Accepted
time: 919ms
memory: 49480kb
Test #50:
score: 0
Accepted
time: 921ms
memory: 64228kb
Test #51:
score: 0
Accepted
time: 914ms
memory: 73184kb
Test #52:
score: 0
Accepted
time: 935ms
memory: 72008kb
Test #53:
score: 0
Accepted
time: 885ms
memory: 52204kb
Test #54:
score: 0
Accepted
time: 889ms
memory: 50108kb
Test #55:
score: 0
Accepted
time: 964ms
memory: 72852kb
Test #56:
score: 0
Accepted
time: 914ms
memory: 44468kb
Test #57:
score: 0
Accepted
time: 898ms
memory: 53224kb
Test #58:
score: 0
Accepted
time: 896ms
memory: 50836kb
Test #59:
score: 0
Accepted
time: 900ms
memory: 60808kb
Test #60:
score: 0
Accepted
time: 977ms
memory: 75004kb
Test #61:
score: 0
Accepted
time: 885ms
memory: 49228kb
Test #62:
score: 0
Accepted
time: 911ms
memory: 62568kb
Test #63:
score: 0
Accepted
time: 933ms
memory: 41524kb
Test #64:
score: 0
Accepted
time: 979ms
memory: 73804kb
Test #65:
score: 0
Accepted
time: 993ms
memory: 51244kb
Test #66:
score: 0
Accepted
time: 980ms
memory: 57228kb
Test #67:
score: 0
Accepted
time: 944ms
memory: 57548kb
Test #68:
score: 0
Accepted
time: 943ms
memory: 59812kb
Test #69:
score: 0
Accepted
time: 1004ms
memory: 43876kb
Test #70:
score: 0
Accepted
time: 1000ms
memory: 44940kb
Test #71:
score: 0
Accepted
time: 998ms
memory: 43036kb
Test #72:
score: 0
Accepted
time: 975ms
memory: 42476kb
Extra Test:
score: 0
Extra Test Passed