QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#45457 | #2211. IOI Problem Revised | yuyue | AC ✓ | 2555ms | 50768kb | C++20 | 4.4kb | 2022-08-23 23:08:47 | 2023-05-04 22:32:42 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
#define mp make_pair
//#define OO(x) fixed<<setprecision(x)
#define int LL
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read(){
char ch=getchar(); LL w=1,c=0;
for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
return w*c;
}
const int M=7.5e5+10;
int n,m;
LL L,a[M],s[M];
LL calc(int l,int r){
// cerr<<l<<" "<<r<<"\n";
assert(r<=3*n);
if (l>=r) return 0;
int mid=(l+r>>1);
// assert((mid-l+1)*a[mid]-(s[mid]-s[l-1])+(s[r]-s[mid])-(r-mid)*a[mid]>=0);
return (mid-l+1)*a[mid]-(s[mid]-s[l-1])+(s[r]-s[mid])-(r-mid)*a[mid];
}
int q[M],qk[M],len[M],pre[M];
LL dp[M];
int O;
int cmp(int x,int y){
int l=y+1,r=n,ret=l-1;
while (l<=r){
int mid=(l+r>>1);
// O++;
if (mp(dp[x]+calc(x+1,mid),len[x])<mp(dp[y]+calc(y+1,mid),len[y])) ret=mid,l=mid+1;
else r=mid-1;
}
return ret;
}
void work1(LL k){
dp[1]=0;
// cerr<<k<<" erfen \n";
q[1]=1; qk[1]=n;
int l=1,r=1;
F(i,2,n){
while (l<=r && qk[l]<i) l++;
dp[i]=dp[q[l]]+calc(q[l]+1,i)+k;
// cerr<<q[l]<<" "<<i<<" "<<dp[i]<<" "<<qk[l]<<" "<<l<<" "<<r<<" ???\n";
pre[i]=q[l];
len[i]=len[q[l]]+1;
int la=n;
while (l<r && (la=cmp(q[r],i))<=qk[r-1]) r--;
qk[r]=(l==r ? cmp(q[r],i) : la);
q[++r]=i; qk[r]=n;
}
}
vector<int> getp(){
LL l=0,r=n*L/m,ret=r;
while (l<=r){
LL mid=(l+r>>1);
// O=0;
work1(mid);
// cerr<<O<<"\n";
if (len[n]<=m) ret=mid,r=mid-1;
else l=mid+1;
}
work1(ret);
vector<int> A; int tmp=n; while (tmp) A.pb(tmp),tmp=pre[tmp];
reverse(A.begin(),A.end());
// cerr<<A.size()<<"\n";
if (A.size()==m+1) return A;
work1(ret-1);
vector<int> B; tmp=n; while (tmp) B.pb(tmp),tmp=pre[tmp];
reverse(B.begin(),B.end());
int del=m-A.size()+1;
// cerr<<A.size()<<" "<<B.size()<<" ???\n";
F(i,0,SZ(A)-1){
if (A[i]<=B[i+del] && B[i+del+1]<=A[i+1]){
vector<int> ret;
F(j,0,i+del) ret.pb(B[j]);
F(j,i+1,SZ(A)) ret.pb(A[j]);
assert(ret.size()==m+1);
return ret;
}
}
assert(0);
}
LL ans=1e18;
vector<int> rec;
LL f[M];
void dnc(int id,int l,int r,int ll,int rr){
if (l>r || ll>rr) return ;
int mid=(ll+rr>>1),bm=l;
f[id+1+mid]=1e18;
F(i,l,r){
if (f[id+i]+calc(i+1,mid)<f[id+1+mid]) f[id+1+mid]=f[id+i]+calc(i+1,mid),pre[id+1+mid]=id+i,bm=i;
}
dnc(id,l,bm,ll,mid-1);
dnc(id,bm,r,mid+1,rr);
}
void solve(vector<int> ql,vector<int> qr){
if (ql[0]>qr[0]) return ;
// cerr<<ql[0]<<" "<<qr[0]<<"\n";
vector<int> mid; mid.resize(m);
mid[0]=(ql[0]+qr[0]>>1);
LL cur=0;
if (m==1){
cur=calc(mid[0],mid[0]+n-1);
}
else{
F(i,ql[1],qr[1]){
pre[i+1]=mid[0];
f[i+1]=calc(mid[0]+1,i);
}
F(i,1,m-2)
dnc(i,ql[i],qr[i],ql[i+1],qr[i+1]);
cur=1e18;
int bp=ql[m-1];
F(i,ql[m-1],qr[m-1]){
if (f[m-1+i]+calc(i+1,mid[0]+n)<=cur){
cur=f[m-1+i]+calc(i+1,mid[0]+n);
bp=i+m-1;
}
}
DF(i,m-1,1) mid[i]=bp-i,bp=pre[bp];
}
if (cur<=ans){
ans=cur;
rec=mid;
}
mid[0]-=1;
solve(ql,mid);
mid[0]+=2;
solve(mid,qr);
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read(); m=read(); L=read();
// n=200000; m=10; L=1e12;
F(i,1,n) a[i]=read();
// F(i,1,n) a[i]=rnd()%L;
// sort(a+1,a+n+1);
//
F(i,1,n) a[i+n]=a[i]+L;
F(i,1,n) a[i+n+n]=a[i+n]+L;
F(i,1,3*n) s[i]=s[i-1]+a[i];
// F(i,1,n){
// F(j,i+1,n){
// cerr<<calc(i,j)<<" ";
// }
// cerr<<'\n';
// }
vector<int> div=getp();
div.pop_back();
F(i,0,m-1) div.pb(div[i]+n);
// return 0;
int mi=1e9,id=-1;
F(i,0,m-1){
if (div[i+1]-div[i]<mi){
mi=div[i+1]-div[i];
id=i;
}
}
vector<int> ql,qr; ql.resize(m);qr.resize(m);
F(i,1,m){
ql[i-1]=div[i+id-1]; qr[i-1]=div[i+id];
}
solve(ql,qr);
cout<<ans<<"\n";
rec.pb(rec[0]+n);
vector<LL> as;
F(i,0,m-1){
as.pb(a[(rec[i]+rec[i+1]+1>>1)]%L);
}
sort(as.begin(),as.end());
for (int x:as) cout<<x<<" ";
cout<<"\n";
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
这程序好像有点Bug,我给组数据试试?
Details
Test #1:
score: 100
Accepted
time: 5ms
memory: 13684kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 13624kb
Test #3:
score: 0
Accepted
time: 2ms
memory: 13756kb
Test #4:
score: 0
Accepted
time: 2ms
memory: 13636kb
Test #5:
score: 0
Accepted
time: 4ms
memory: 13580kb
Test #6:
score: 0
Accepted
time: 2ms
memory: 13680kb
Test #7:
score: 0
Accepted
time: 9ms
memory: 13772kb
Test #8:
score: 0
Accepted
time: 2ms
memory: 13792kb
Test #9:
score: 0
Accepted
time: 19ms
memory: 14020kb
Test #10:
score: 0
Accepted
time: 14ms
memory: 15980kb
Test #11:
score: 0
Accepted
time: 209ms
memory: 17272kb
Test #12:
score: 0
Accepted
time: 170ms
memory: 22380kb
Test #13:
score: 0
Accepted
time: 246ms
memory: 17092kb
Test #14:
score: 0
Accepted
time: 162ms
memory: 18628kb
Test #15:
score: 0
Accepted
time: 210ms
memory: 17448kb
Test #16:
score: 0
Accepted
time: 1324ms
memory: 43788kb
Test #17:
score: 0
Accepted
time: 1412ms
memory: 37184kb
Test #18:
score: 0
Accepted
time: 1408ms
memory: 36704kb
Test #19:
score: 0
Accepted
time: 1890ms
memory: 26656kb
Test #20:
score: 0
Accepted
time: 1301ms
memory: 43948kb
Test #21:
score: 0
Accepted
time: 1303ms
memory: 42480kb
Test #22:
score: 0
Accepted
time: 1285ms
memory: 44448kb
Test #23:
score: 0
Accepted
time: 1535ms
memory: 30572kb
Test #24:
score: 0
Accepted
time: 1389ms
memory: 37660kb
Test #25:
score: 0
Accepted
time: 1308ms
memory: 38660kb
Test #26:
score: 0
Accepted
time: 1525ms
memory: 31780kb
Test #27:
score: 0
Accepted
time: 1456ms
memory: 31748kb
Test #28:
score: 0
Accepted
time: 1357ms
memory: 39088kb
Test #29:
score: 0
Accepted
time: 1632ms
memory: 31388kb
Test #30:
score: 0
Accepted
time: 1241ms
memory: 47788kb
Test #31:
score: 0
Accepted
time: 1309ms
memory: 40852kb
Test #32:
score: 0
Accepted
time: 1322ms
memory: 43836kb
Test #33:
score: 0
Accepted
time: 1317ms
memory: 45420kb
Test #34:
score: 0
Accepted
time: 1315ms
memory: 41596kb
Test #35:
score: 0
Accepted
time: 1254ms
memory: 44092kb
Test #36:
score: 0
Accepted
time: 1257ms
memory: 47120kb
Test #37:
score: 0
Accepted
time: 1329ms
memory: 41268kb
Test #38:
score: 0
Accepted
time: 1281ms
memory: 47420kb
Test #39:
score: 0
Accepted
time: 2031ms
memory: 27792kb
Test #40:
score: 0
Accepted
time: 1199ms
memory: 50768kb
Test #41:
score: 0
Accepted
time: 1898ms
memory: 25792kb
Test #42:
score: 0
Accepted
time: 1311ms
memory: 41984kb
Test #43:
score: 0
Accepted
time: 1747ms
memory: 26880kb
Test #44:
score: 0
Accepted
time: 1271ms
memory: 43908kb
Test #45:
score: 0
Accepted
time: 1187ms
memory: 45736kb
Test #46:
score: 0
Accepted
time: 1263ms
memory: 44360kb
Test #47:
score: 0
Accepted
time: 1431ms
memory: 35344kb
Test #48:
score: 0
Accepted
time: 1649ms
memory: 29288kb
Test #49:
score: 0
Accepted
time: 1738ms
memory: 26016kb
Test #50:
score: 0
Accepted
time: 1371ms
memory: 38740kb
Test #51:
score: 0
Accepted
time: 1352ms
memory: 39780kb
Test #52:
score: 0
Accepted
time: 1292ms
memory: 41452kb
Test #53:
score: 0
Accepted
time: 1597ms
memory: 31868kb
Test #54:
score: 0
Accepted
time: 1582ms
memory: 29508kb
Test #55:
score: 0
Accepted
time: 1319ms
memory: 42132kb
Test #56:
score: 0
Accepted
time: 1770ms
memory: 27320kb
Test #57:
score: 0
Accepted
time: 1465ms
memory: 27756kb
Test #58:
score: 0
Accepted
time: 1837ms
memory: 28000kb
Test #59:
score: 0
Accepted
time: 1347ms
memory: 37348kb
Test #60:
score: 0
Accepted
time: 1289ms
memory: 46064kb
Test #61:
score: 0
Accepted
time: 1672ms
memory: 30620kb
Test #62:
score: 0
Accepted
time: 1378ms
memory: 40108kb
Test #63:
score: 0
Accepted
time: 2117ms
memory: 23516kb
Test #64:
score: 0
Accepted
time: 1211ms
memory: 48732kb
Test #65:
score: 0
Accepted
time: 462ms
memory: 28640kb
Test #66:
score: 0
Accepted
time: 495ms
memory: 36028kb
Test #67:
score: 0
Accepted
time: 565ms
memory: 40396kb
Test #68:
score: 0
Accepted
time: 590ms
memory: 37784kb
Test #69:
score: 0
Accepted
time: 2513ms
memory: 26160kb
Test #70:
score: 0
Accepted
time: 2498ms
memory: 27020kb
Test #71:
score: 0
Accepted
time: 2509ms
memory: 23136kb
Test #72:
score: 0
Accepted
time: 2555ms
memory: 21968kb
Extra Test:
score: 0
Extra Test Passed