QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#100595 | #2211. IOI Problem Revised | charlie2005 | TL | 5775ms | 57420kb | C++23 | 3.3kb | 2023-04-26 23:11:30 | 2023-05-04 22:38:39 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF push_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
#define lc (x<<1)
#define rc ((x<<1)|1)
#define sz(x) ((int)x.size())
#define pc putchar
using namespace std;
int n,k,L;
int a[400005],s[400005];
P<int,int> dp[400005],dp2[400005];
int f[400005],it[400005],len;
int lst[400005];
bool tp;
int get(int l,int r){
int mid=(l+r)>>1;
RE (mid-l+1)*a[mid]-(s[mid]-s[l-1])+(s[r]-s[mid])-(r-mid)*a[mid];
}
int ad;
void solve(int l1,int r1,int l2,int r2){
if(l1>r1)RE;
int mid=(l1+r1)>>1,tl=l2;
P<int,int> tmi=MP(1e18,1e18);
FOR(i,l2,min(r2,mid-1)){
P<int,int> now=MP(dp[i].F+get(i+1,mid)+ad,dp[i].S+(tp?1:-1));
if(now<dp[mid]){
lst[mid]=i;dp[mid]=now;
}
if(now<tmi){
tmi=now;tl=i;
}
}
solve(l1,mid-1,l2,tl);
solve(mid+1,r1,tl,r2);
}
void solve(int l,int r){
if(l==r)RE;
int mid=(l+r)>>1;
solve(l,mid);
solve(mid+1,r,l,mid);
solve(mid+1,r);
}
void check(int mid){
ad=mid;
FOR(i,1,n)dp[i]=MP(1e18,1e18);
solve(0,n);
}
int b[400005];
int mi=1e18,tl[400005],tr[400005];
V<int> v;
void solve2(int l1,int r1,int l2,int r2){
if(l1>r1)RE;
int mid=(l1+r1)>>1;
FOR(i,l2,r2){
if(it[i]>=it[mid])break;
int now=f[i]+get(it[i]+1,it[mid]);
if(now<f[mid]){
lst[mid]=i;f[mid]=now;
}
}
solve2(l1,mid-1,l2,lst[mid]);
solve2(mid+1,r1,lst[mid],r2);
}
void solve2(V<int> vl,V<int> vr){
if(vl[0]>vr[0])RE;
int md=(vl[0]+vr[0])>>1;
len=0;
rep(i,0,sz(vl)){
tl[i]=len+1;
FOR(j,vl[i],vr[i])it[++len]=j;
tr[i]=len;
}
FOR(i,1,len)f[i]=1e18;
f[md-vl[0]+1]=0;
rep(i,1,sz(vl)){
solve2(tl[i],tr[i],tl[i-1],tr[i-1]);
}
int nowmi=1e18,at=-1;
int s=sz(vl);
FOR(i,vl.back(),vr.back())if(i+1<=md+n){
int now=f[len-vr.back()+i]+get(i+1,md+n);
if(now<nowmi){
at=len-vr.back()+i;nowmi=now;
}
}
assert(at!=-1);
V<int> mid(s);
for(int i=s-1;i>=0;i--){
mid[i]=it[at];
at=lst[at];
}
assert(mid[0]==md);
if(nowmi<mi){
mi=nowmi;
mid.PB(md+n);v.clear();
rep(i,0,sz(mid)-1){
v.PB(a[(mid[i]+1+mid[i+1])>>1]%L);
}
mid.pop_back();
}
// exit(0);
mid[0]--;
solve2(vl,mid);
mid[0]+=2;
solve2(mid,vr);
}
signed main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k>>L;
FOR(i,1,n)cin>>a[i],a[i+n]=a[i]+L;
FOR(i,1,n+n)s[i]=s[i-1]+a[i];
int l=0,r=1e18,mid,ans=0;
while(r>=l){
mid=(l+r)>>1;
check(mid);
if(-dp[n].S>=k)ans=mid,l=mid+1;else r=mid-1;
}
check(ans);
FOR(i,0,n)dp2[i]=dp[i],dp2[i].S=-dp2[i].S;
tp=1;
check(ans);
ad=0;
V<int> num;
int lst=n;
num.PB(n);
int hav=1;
for(int i=n-1;i>=1;i--){
if(dp[lst].F==dp[i].F+get(i+1,lst)+ans&&hav+dp[i].S<=k&&hav+dp2[i].S>=k){
hav++;num.PB(i);lst=i;
}
}
reverse(ALL(num));
int t=0;
for(auto u:num)b[++t]=u;
V<int> v1,v2;
FOR(i,1,t)v1.PB(b[i-1]),v2.PB(b[i]);
solve2(v1,v2);
cout<<mi<<'\n';
sort(ALL(v));
for(auto u:v)cout<<u<<' ';
RE 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 15680kb
Test #2:
score: 0
Accepted
time: 2ms
memory: 13620kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 13660kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 13668kb
Test #5:
score: 0
Accepted
time: 2ms
memory: 9640kb
Test #6:
score: 0
Accepted
time: 2ms
memory: 11616kb
Test #7:
score: 0
Accepted
time: 9ms
memory: 11732kb
Test #8:
score: 0
Accepted
time: 10ms
memory: 11760kb
Test #9:
score: 0
Accepted
time: 43ms
memory: 11868kb
Test #10:
score: 0
Accepted
time: 39ms
memory: 11932kb
Test #11:
score: 0
Accepted
time: 570ms
memory: 13340kb
Test #12:
score: 0
Accepted
time: 540ms
memory: 15548kb
Test #13:
score: 0
Accepted
time: 520ms
memory: 14988kb
Test #14:
score: 0
Accepted
time: 493ms
memory: 15068kb
Test #15:
score: 0
Accepted
time: 496ms
memory: 14248kb
Test #16:
score: 0
Accepted
time: 4626ms
memory: 45040kb
Test #17:
score: 0
Accepted
time: 4821ms
memory: 38132kb
Test #18:
score: 0
Accepted
time: 4683ms
memory: 38644kb
Test #19:
score: 0
Accepted
time: 4822ms
memory: 28140kb
Test #20:
score: 0
Accepted
time: 4713ms
memory: 44172kb
Test #21:
score: 0
Accepted
time: 4688ms
memory: 43704kb
Test #22:
score: 0
Accepted
time: 4654ms
memory: 45800kb
Test #23:
score: 0
Accepted
time: 4812ms
memory: 34140kb
Test #24:
score: 0
Accepted
time: 4694ms
memory: 37920kb
Test #25:
score: 0
Accepted
time: 4618ms
memory: 43204kb
Test #26:
score: 0
Accepted
time: 4779ms
memory: 33036kb
Test #27:
score: 0
Accepted
time: 4733ms
memory: 31468kb
Test #28:
score: 0
Accepted
time: 4688ms
memory: 43080kb
Test #29:
score: 0
Accepted
time: 4777ms
memory: 28612kb
Test #30:
score: 0
Accepted
time: 4756ms
memory: 50428kb
Test #31:
score: 0
Accepted
time: 4964ms
memory: 42604kb
Test #32:
score: 0
Accepted
time: 4643ms
memory: 45596kb
Test #33:
score: 0
Accepted
time: 4640ms
memory: 45784kb
Test #34:
score: 0
Accepted
time: 4632ms
memory: 46656kb
Test #35:
score: 0
Accepted
time: 4684ms
memory: 44280kb
Test #36:
score: 0
Accepted
time: 4561ms
memory: 50736kb
Test #37:
score: 0
Accepted
time: 4692ms
memory: 42516kb
Test #38:
score: 0
Accepted
time: 4658ms
memory: 46768kb
Test #39:
score: 0
Accepted
time: 4759ms
memory: 25084kb
Test #40:
score: 0
Accepted
time: 4615ms
memory: 50996kb
Test #41:
score: 0
Accepted
time: 4753ms
memory: 25340kb
Test #42:
score: 0
Accepted
time: 4599ms
memory: 44992kb
Test #43:
score: 0
Accepted
time: 4771ms
memory: 28492kb
Test #44:
score: 0
Accepted
time: 4643ms
memory: 46140kb
Test #45:
score: 0
Accepted
time: 4645ms
memory: 48864kb
Test #46:
score: 0
Accepted
time: 4624ms
memory: 46188kb
Test #47:
score: 0
Accepted
time: 4683ms
memory: 35260kb
Test #48:
score: 0
Accepted
time: 4759ms
memory: 30188kb
Test #49:
score: 0
Accepted
time: 4763ms
memory: 28652kb
Test #50:
score: 0
Accepted
time: 4708ms
memory: 40084kb
Test #51:
score: 0
Accepted
time: 4675ms
memory: 41212kb
Test #52:
score: 0
Accepted
time: 4564ms
memory: 43940kb
Test #53:
score: 0
Accepted
time: 4742ms
memory: 33188kb
Test #54:
score: 0
Accepted
time: 4742ms
memory: 32056kb
Test #55:
score: 0
Accepted
time: 4670ms
memory: 46464kb
Test #56:
score: 0
Accepted
time: 4798ms
memory: 28624kb
Test #57:
score: 0
Accepted
time: 4719ms
memory: 32016kb
Test #58:
score: 0
Accepted
time: 4768ms
memory: 31100kb
Test #59:
score: 0
Accepted
time: 4649ms
memory: 40560kb
Test #60:
score: 0
Accepted
time: 4644ms
memory: 50164kb
Test #61:
score: 0
Accepted
time: 4767ms
memory: 31596kb
Test #62:
score: 0
Accepted
time: 4672ms
memory: 39760kb
Test #63:
score: 0
Accepted
time: 4756ms
memory: 24204kb
Test #64:
score: 0
Accepted
time: 4635ms
memory: 49120kb
Test #65:
score: 0
Accepted
time: 5775ms
memory: 34520kb
Test #66:
score: 0
Accepted
time: 5689ms
memory: 57420kb
Test #67:
score: 0
Accepted
time: 4914ms
memory: 51904kb
Test #68:
score: 0
Accepted
time: 5048ms
memory: 53456kb
Test #69:
score: 0
Accepted
time: 4784ms
memory: 24796kb
Test #70:
score: 0
Accepted
time: 4711ms
memory: 25092kb
Test #71:
score: 0
Accepted
time: 4747ms
memory: 22576kb
Test #72:
score: 0
Accepted
time: 4805ms
memory: 24256kb
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 1