QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#612649 | #9440. Train Seats | ucup-team3890# | WA | 2ms | 11844kb | C++14 | 2.5kb | 2024-10-05 12:26:51 | 2024-10-05 12:26:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+99;
int n,m,a[N],ans;
struct node{
int sum,len;
int l,r;
bool operator <(const node&A) const{
return sum*A.len<A.sum*len;
}
bool operator !=(const node&A) const{
return sum!=A.sum||len!=A.len||l!=A.l||r!=A.r;
}
node operator +(const node&A){
return (node){sum+A.sum,len+A.len,min(l,A.l),max(r,A.r)};
}
};
node sta1[N],sta2[N];
int cnt1,cnt2;
int seq[N],cnt;
void solve(){
int re=n*(m+1);
for(int i=1;i<=n;i++)
re-=seq[i]*(n-i);
ans=max(ans,re);
return;
}
priority_queue<node> que;
node nd[N];
int f[N];
int find(int x){ if(f[x]==x) return x; return f[x]=find(f[x]); }
void merge(int x,int y){//
f[x]=y;
nd[y]=nd[y]+nd[x];
que.push(nd[y]);
return;
}
void merge1(){
for(int i=1;i<=n+1;i++)
f[i]=i,nd[i]=(node){-a[i],1,i,i};
for(int i=1;i<=n+1;i++)
que.push(nd[i]);
node now;
int x,y;
while(!que.empty()){
now=que.top(); que.pop();
if(nd[find(now.l)]!=now) continue;
if(now.l==1) continue;
x=find(now.l); y=find(now.l-1);
if(nd[x]<nd[y]) continue;//-
merge(x,y);
}
for(int i=n+1;i>=1;i--)
if(f[i]==i)
sta1[++cnt1]=nd[i],sta1[cnt1].sum=-sta1[cnt1].sum;
return;
}
void merge2(){
for(int i=1;i<=n+1;i++)
f[i]=i,nd[i]=(node){-a[i],1,i,i};
for(int i=1;i<=n+1;i++)
que.push(nd[i]);
node now;
int x,y;
while(!que.empty()){
now=que.top(); que.pop();
if(nd[find(now.r)]!=now) continue;
if(now.r==n+1) continue;
x=find(now.r); y=find(now.r+1);
if(nd[x]<nd[y]) continue;//-
merge(x,y);
}
for(int i=1;i<=n+1;i++)
if(f[i]==i)
sta2[++cnt2]=nd[i],sta2[cnt2].sum=-sta2[cnt2].sum;
return;
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1); a[n+1]=m+1;
for(int i=n+1;i>=1;i--) a[i]-=a[i-1];
node now;
merge1();
merge2();
// cout<<cnt1<<endl;
// for(int i=cnt1;i>=1;i--)
// cout<<sta1[i].l<<" "<<sta1[i].r<<endl;
// cout<<cnt2<<endl;
// for(int i=cnt2;i>=1;i--)
// cout<<sta2[i].l<<" "<<sta2[i].r<<endl;
int l=1,r=n+1;
while(cnt1&&cnt2){
if(sta1[cnt1].r>=sta2[cnt2].l) break;
if(sta1[cnt1]<sta2[cnt2]){
while(l<=sta1[cnt1].r)
seq[++cnt]=a[l++];
cnt1--;
}
else{
while(r>=sta2[cnt2].l)
seq[++cnt]=a[r--];
cnt2--;
}
}
// cout<<l<<" "<<r<<endl;
for(int i=l;i<=r;i++)
seq[cnt+i-l+1]=a[i];
solve();
for(int i=r;i>=l;i--)
seq[cnt+r-i+1]=a[i];
solve();
cout<<ans;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11836kb
input:
3 10 3 7 10
output:
28
result:
ok "28"
Test #2:
score: 0
Accepted
time: 0ms
memory: 11844kb
input:
5 20 3 10 11 14 17
output:
73
result:
ok "73"
Test #3:
score: 0
Accepted
time: 2ms
memory: 9788kb
input:
10 1000000000 136909656 243332691 643531997 505919307 43553384 657638276 57213246 179732866 357373203 182482400
output:
7649951260
result:
ok "7649951260"
Test #4:
score: 0
Accepted
time: 2ms
memory: 11824kb
input:
1 172024959 118390965
output:
172024960
result:
ok "172024960"
Test #5:
score: 0
Accepted
time: 0ms
memory: 9740kb
input:
2 920065252 24963998 580538879
output:
1815166508
result:
ok "1815166508"
Test #6:
score: 0
Accepted
time: 0ms
memory: 9800kb
input:
3 172078788 90217996 89200357 170380183
output:
432676968
result:
ok "432676968"
Test #7:
score: 0
Accepted
time: 1ms
memory: 9792kb
input:
4 440425711 125318960 91140311 293637977 102491554
output:
1442752023
result:
ok "1442752023"
Test #8:
score: 0
Accepted
time: 1ms
memory: 9868kb
input:
5 322827483 22471802 47973794 3707067 27640905 273307033
output:
1512343852
result:
ok "1512343852"
Test #9:
score: -100
Wrong Answer
time: 1ms
memory: 9784kb
input:
72 630313504 112329946 338670434 45608233 444381955 513206139 543955969 420916952 485920423 598657953 568525637 92887514 375155749 230002387 302266710 539300386 433464422 380969627 445990156 239073197 278937451 50602251 494375406 139348725 11780176 601670777 68418714 591190755 96719555 612628609 778...
output:
24315752154
result:
wrong answer 1st words differ - expected: '25015466409', found: '24315752154'