QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#886532 | #2211. IOI Problem Revised | ANIG | AC ✓ | 1619ms | 129616kb | C++14 | 5.1kb | 2025-02-07 08:50:21 | 2025-02-07 14:43:21 |
Judging History
This is the latest submission verdict.
- [2025-02-07 14:35:59]
- hack成功,自动添加数据
- (/hack/1532)
- [2025-02-07 08:50:21]
- Submitted
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=4e5+5,inf=1e18;
struct msg1{
int sm,sl;
friend bool operator<(msg1 a,msg1 b){
if(a.sm==b.sm)return a.sl>b.sl;
return a.sm<b.sm;
}
};
struct msg2{
int sm,sl;
friend bool operator<(msg2 a,msg2 b){
if(a.sm==b.sm)return a.sl<b.sl;
return a.sm<b.sm;
}
};
int n,k,L,ni,p[N],qz[N],nw,mk[N],g1[N],g2[N],dp[N],ds[N],ans,res=inf;
unordered_map<int,int>dy[N];
msg1 f1[N];msg2 f2[N];
int gets(int l,int r){
int mid=l+r>>1;
return qz[r]-qz[mid]-p[mid]*(r-mid)+p[mid]*(mid-l+1)-(qz[mid]-qz[l-1]);
}
msg1 gets1(int l,int r){
return {f1[l-1].sm+gets(l,r)-nw,f1[l-1].sl+1};
}
msg2 gets2(int l,int r){
return {f2[l-1].sm+gets(l,r)-nw,f2[l-1].sl+1};
}
struct node{
int l,r,bh;
};
bool chk(int x){
nw=x;
deque<node>q1,q2;
for(int i=1;i<=n;i++){
while(q1.size()&&gets1(i,q1.back().l)<gets1(q1.back().bh,q1.back().l))q1.pop_back();
if(q1.size()&&gets1(i,q1.back().r)<gets1(q1.back().bh,q1.back().r)){
int l=q1.back().l,r=q1.back().r;
while(l<r){
int mid=l+r+1>>1;
if(gets1(q1.back().bh,mid)<gets1(i,mid))l=mid;
else r=mid-1;
}
q1.back().r=l;
}
if(q1.size()){
if(q1.back().r<n)q1.push_back({q1.back().r+1,n,i});
}else q1.push_back({i,n,i});
while(q1.front().r<i)q1.pop_front();
f1[i]=gets1(q1.front().bh,i);
while(q2.size()&&gets2(i,q2.back().l)<gets2(q2.back().bh,q2.back().l))q2.pop_back();
if(q2.size()&&gets2(i,q2.back().r)<gets2(q2.back().bh,q2.back().r)){
int l=q2.back().l,r=q2.back().r;
while(l<r){
int mid=l+r+1>>1;
if(gets2(q2.back().bh,mid)<gets2(i,mid))l=mid;
else r=mid-1;
}
q2.back().r=l;
}
if(q2.size()){
if(q2.back().r<n)q2.push_back({q2.back().r+1,n,i});
}else q2.push_back({i,n,i});
while(q2.front().r<i)q2.pop_front();
f2[i]=gets2(q2.front().bh,i);
g1[i]=f1[i].sl;g2[i]=f2[i].sl;
}
return g1[n]>=k;
}
vector<pair<int,int> >jl;
void creat(){
int nw=n,nk=k;
while(nw){
mk[nw]=1;
for(int i=nw;i>=1;i--){
if(f1[i-1].sm+gets(i,nw)-ans!=f1[nw].sm)continue;
if(nk-1<g2[i-1]||nk-1>g1[i-1])continue;
jl.push_back({i-1,nw});
nw=i-1;
nk--;
break;
}
}
}
void solve(int l,int r,int a,int b){
if(l>r)return;
int mid=l+r>>1,wz,ans=inf;
for(int i=a;i<=b;i++){
if(ds[i]+gets(i+1,mid)<ans){
ans=ds[i]+gets(i+1,mid);
wz=i;
}
}
dy[ni][mid]=wz;
dp[mid]=ans;
solve(l,mid-1,a,wz);
solve(mid+1,r,wz,b);
}
vector<int>rr;
void solve(int l,int r,vector<pair<int,int> >q){
if(l>r)return;
int mid=l+r>>1;
for(int i=q[0].first;i<=q[0].second;i++)dp[i]=inf;
dp[mid]=0;
// cout<<mid<<endl;
for(int i=1;i<q.size();i++){
ni=i;
for(int j=q[i-1].first;j<=q[i-1].second;j++)ds[j]=dp[j];
solve(q[i].first,q[i].second,q[i-1].first,q[i-1].second);
}
int wz=0,sms=inf;
//for(int i=1;i<=n;i++)cout<<i<<" "<<dp[i]<<" "<<dy[i]<<endl;
for(int i=q.back().first;i<=q.back().second;i++){
if(dp[i]+gets(i+1,mid+n)<sms){
sms=dp[i]+gets(i+1,mid+n);
wz=i;
}
}
vector<int>jl;
int nw=wz,nk=k-1;
// for(auto c:q)cout<<c.first<<" "<<c.second<<endl;
// for(int i=1;i<=n;i++)cout<<i<<" "<<dp[i]<<" "<<dy[i]<<endl;
while(nw!=mid){
jl.push_back(nw);
nw=dy[nk][nw];
nk--;
}
vector<pair<int,int> >q1,q2;
q1.push_back({l,mid-1});
q2.push_back({mid+1,r});
reverse(jl.begin(),jl.end());
for(int i=1;i<q.size();i++){
q1.push_back({q[i].first,jl[i-1]});
q2.push_back({jl[i-1],q[i].second});
}
if(sms<res){
res=sms;
rr.clear();
rr.push_back(p[mid+jl[0]+1>>1]);
for(int i=1;i<jl.size();i++)rr.push_back(p[jl[i-1]+jl[i]+1>>1]);
rr.push_back(p[mid+n+jl.back()+1>>1]);
}
solve(l,mid-1,q1);solve(mid+1,r,q2);
}
signed main(){
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k>>L;
for(int i=1;i<=n;i++)cin>>p[i],p[i+n]=p[i]+L;
for(int i=1;i<=2*n;i++)qz[i]=qz[i-1]+p[i];
if(k==1){
int wz=0;
for(int i=1;i<=n;i++){
if(gets(i,i+n-1)<res){
res=gets(i,i+n-1);
wz=p[2*i+n-1>>1];
}
}
if(wz>=L)wz-=L;
cout<<res<<endl<<wz;
return 0;
}
int l=-n*L,r=n*L;
while(l<r){
int mid=l+r>>1;
if(chk(mid))r=mid;
else l=mid+1;
}
ans=l;
chk(ans);
creat();
reverse(jl.begin(),jl.end());
// for(auto c:jl)cout<<c.first<<" "<<c.second<<endl;
int tmp,wz;
for(int i=0;i<jl.size();i++){
auto c=jl[i];
if(c.second-c.first<=n/k+1){
wz=i;
tmp=c.first;
break;
}
}
// cout<<tmp<<endl;
rotate(p+1,p+tmp+1,p+n+1);
for(int i=n-tmp+1;i<=n;i++)p[i]+=L;
for(int i=1;i<=n;i++)p[i+n]=p[i]+L;
// for(int i=1;i<=n;i++)cout<<p[i]<<" ";
// cout<<endl;
for(int i=1;i<=2*n;i++)qz[i]=qz[i-1]+p[i];
// for(auto c:jl)cout<<c.first<<" "<<c.second<<endl;
for(int i=0;i<jl.size();i++){
auto &c=jl[i];
c.first=(c.first-tmp+n)%n;
c.second=(c.second-tmp+n)%n;
if(i!=wz&&c.second==0)c.second=n;
}
// for(auto c:jl)cout<<c.first<<" "<<c.second<<endl;
sort(jl.begin(),jl.end());
solve(jl[0].first,jl[0].second,jl);
cout<<res<<endl;
for(auto &c:rr)c%=L;
sort(rr.begin(),rr.end());
cerr<<res<<endl;
for(auto c:rr)cout<<c<<" ";
}
这程序好像有点Bug,我给组数据试试?
Details
Test #1:
score: 100
Accepted
time: 2ms
memory: 38488kb
Test #2:
score: 0
Accepted
time: 8ms
memory: 36424kb
Test #3:
score: 0
Accepted
time: 2ms
memory: 36584kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 38508kb
Test #5:
score: 0
Accepted
time: 4ms
memory: 36584kb
Test #6:
score: 0
Accepted
time: 2ms
memory: 36584kb
Test #7:
score: 0
Accepted
time: 9ms
memory: 38684kb
Test #8:
score: 0
Accepted
time: 13ms
memory: 36692kb
Test #9:
score: 0
Accepted
time: 19ms
memory: 39356kb
Test #10:
score: 0
Accepted
time: 22ms
memory: 39272kb
Test #11:
score: 0
Accepted
time: 182ms
memory: 42748kb
Test #12:
score: 0
Accepted
time: 191ms
memory: 48884kb
Test #13:
score: 0
Accepted
time: 187ms
memory: 45568kb
Test #14:
score: 0
Accepted
time: 190ms
memory: 48376kb
Test #15:
score: 0
Accepted
time: 185ms
memory: 47484kb
Test #16:
score: 0
Accepted
time: 1499ms
memory: 121412kb
Test #17:
score: 0
Accepted
time: 1422ms
memory: 94844kb
Test #18:
score: 0
Accepted
time: 1430ms
memory: 96724kb
Test #19:
score: 0
Accepted
time: 1389ms
memory: 69652kb
Test #20:
score: 0
Accepted
time: 1549ms
memory: 123468kb
Test #21:
score: 0
Accepted
time: 1525ms
memory: 118796kb
Test #22:
score: 0
Accepted
time: 1537ms
memory: 123608kb
Test #23:
score: 0
Accepted
time: 1374ms
memory: 87456kb
Test #24:
score: 0
Accepted
time: 1393ms
memory: 98572kb
Test #25:
score: 0
Accepted
time: 1443ms
memory: 99876kb
Test #26:
score: 0
Accepted
time: 1356ms
memory: 93972kb
Test #27:
score: 0
Accepted
time: 1344ms
memory: 92772kb
Test #28:
score: 0
Accepted
time: 1449ms
memory: 104796kb
Test #29:
score: 0
Accepted
time: 1384ms
memory: 78232kb
Test #30:
score: 0
Accepted
time: 1598ms
memory: 128376kb
Test #31:
score: 0
Accepted
time: 1465ms
memory: 117276kb
Test #32:
score: 0
Accepted
time: 1493ms
memory: 119720kb
Test #33:
score: 0
Accepted
time: 1545ms
memory: 123524kb
Test #34:
score: 0
Accepted
time: 1412ms
memory: 119672kb
Test #35:
score: 0
Accepted
time: 1439ms
memory: 120024kb
Test #36:
score: 0
Accepted
time: 1619ms
memory: 124380kb
Test #37:
score: 0
Accepted
time: 1473ms
memory: 115924kb
Test #38:
score: 0
Accepted
time: 1547ms
memory: 122064kb
Test #39:
score: 0
Accepted
time: 1339ms
memory: 67200kb
Test #40:
score: 0
Accepted
time: 1510ms
memory: 129428kb
Test #41:
score: 0
Accepted
time: 1318ms
memory: 72468kb
Test #42:
score: 0
Accepted
time: 1498ms
memory: 118604kb
Test #43:
score: 0
Accepted
time: 1370ms
memory: 75344kb
Test #44:
score: 0
Accepted
time: 1536ms
memory: 125948kb
Test #45:
score: 0
Accepted
time: 1534ms
memory: 129100kb
Test #46:
score: 0
Accepted
time: 1499ms
memory: 120584kb
Test #47:
score: 0
Accepted
time: 1361ms
memory: 93852kb
Test #48:
score: 0
Accepted
time: 1385ms
memory: 82636kb
Test #49:
score: 0
Accepted
time: 1373ms
memory: 72116kb
Test #50:
score: 0
Accepted
time: 1456ms
memory: 99116kb
Test #51:
score: 0
Accepted
time: 1470ms
memory: 115952kb
Test #52:
score: 0
Accepted
time: 1425ms
memory: 119616kb
Test #53:
score: 0
Accepted
time: 1369ms
memory: 84048kb
Test #54:
score: 0
Accepted
time: 1373ms
memory: 80620kb
Test #55:
score: 0
Accepted
time: 1525ms
memory: 122888kb
Test #56:
score: 0
Accepted
time: 1376ms
memory: 77288kb
Test #57:
score: 0
Accepted
time: 1268ms
memory: 76276kb
Test #58:
score: 0
Accepted
time: 1366ms
memory: 76764kb
Test #59:
score: 0
Accepted
time: 1402ms
memory: 99000kb
Test #60:
score: 0
Accepted
time: 1580ms
memory: 129616kb
Test #61:
score: 0
Accepted
time: 1378ms
memory: 83576kb
Test #62:
score: 0
Accepted
time: 1406ms
memory: 98352kb
Test #63:
score: 0
Accepted
time: 1355ms
memory: 66144kb
Test #64:
score: 0
Accepted
time: 1578ms
memory: 126144kb
Test #65:
score: 0
Accepted
time: 666ms
memory: 73536kb
Test #66:
score: 0
Accepted
time: 709ms
memory: 95828kb
Test #67:
score: 0
Accepted
time: 712ms
memory: 95644kb
Test #68:
score: 0
Accepted
time: 694ms
memory: 99508kb
Test #69:
score: 0
Accepted
time: 1387ms
memory: 62716kb
Test #70:
score: 0
Accepted
time: 1379ms
memory: 67876kb
Test #71:
score: 0
Accepted
time: 1364ms
memory: 63464kb
Test #72:
score: 0
Accepted
time: 1408ms
memory: 62728kb
Extra Test:
score: 0
Extra Test Passed