QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#547410 | #4105. 征途 | lhc0707 | 100 ✓ | 540ms | 53072kb | C++14 | 1.6kb | 2024-09-04 21:27:51 | 2024-09-04 21:27:51 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<cstdio>
#define endl '\n'
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
using namespace std;
const int N=3005,V=300005,INF=1e9;
int n,m,a[N],s[N],f[N][N],cnt,v;
struct Line{
int k,b,id;
Line(){k=0,b=INF,id=0;}
Line(int _k,int _b,int _id){k=_k,b=_b,id=_id;}
int f(int x){return k*x+b;}
}tr[V<<2];
void clear(int p,int pl,int pr)
{
tr[p]=Line(0,INF,0);
if(pl==pr)return;
int mid=(pl+pr)>>1;
if(tr[ls(p)].id)clear(ls(p),pl,mid);
if(tr[rs(p)].id)clear(rs(p),mid+1,pr);
}
void update(int p,int pl,int pr,int L,int R,Line g)
{
if(L<=pl&&pr<=R)
{
if(!tr[p].id){tr[p]=g;return;}
int mid=(pl+pr)>>1;
if(tr[p].f(mid)>g.f(mid))swap(tr[p],g);
if(tr[p].f(pl)>g.f(pl))update(ls(p),pl,mid,L,R,g);
if(tr[p].f(pr)>g.f(pr))update(rs(p),mid+1,pr,L,R,g);
return;
}
int mid=(pl+pr)>>1;
if(L<=mid)update(ls(p),pl,mid,L,R,g);
if(R>mid)update(rs(p),mid+1,pr,L,R,g);
}
int query(int p,int pl,int pr,int pos)
{
if(pl==pr)return tr[p].f(pos);
int mid=(pl+pr)>>1;
if(pos<=mid)return min(tr[p].f(pos),query(ls(p),pl,mid,pos));
else return min(tr[p].f(pos),query(rs(p),mid+1,pr,pos));
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i],v=max(v,s[i]);
memset(f,INF,sizeof(f)); f[0][0]=0;
for(int i=1;i<=n;i++)f[i][1]=s[i]*s[i];
for(int l=2;l<=m;l++)
{
clear(1,1,v);
for(int i=1;i<=n;i++)if(i>=l)
{
Line g=Line(-2*s[i-1],s[i-1]*s[i-1]+f[i-1][l-1],++cnt);
update(1,1,v,1,v,g);
f[i][l]=query(1,1,v,s[i])+s[i]*s[i];
}
}
cout<<f[n][m]*m-s[n]*s[n]<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 52928kb
input:
9 5 440 546 398 3132 3121 2939 3139 1476 3075
output:
13040944
result:
ok single line: '13040944'
Test #2:
score: 10
Accepted
time: 3ms
memory: 52992kb
input:
10 6 1769 223 1182 1486 1251 30 373 164 1891 1657
output:
1108740
result:
ok single line: '1108740'
Test #3:
score: 10
Accepted
time: 5ms
memory: 53064kb
input:
8 3 1752 1914 1597 3051 2197 3279 266 3388
output:
5628350
result:
ok single line: '5628350'
Test #4:
score: 10
Accepted
time: 0ms
memory: 53044kb
input:
92 67 117 37 197 297 148 20 100 201 287 209 239 27 85 83 169 35 108 71 90 89 210 37 89 110 262 170 59 290 110 249 90 181 77 60 4 316 17 196 324 135 269 32 14 260 157 148 207 263 71 207 156 291 270 258 168 33 34 165 130 182 168 229 54 309 124 155 19 205 303 251 11 175 23 136 174 217 4 261 149 310 95 ...
output:
17552120
result:
ok single line: '17552120'
Test #5:
score: 10
Accepted
time: 0ms
memory: 52920kb
input:
76 18 296 195 25 355 334 311 266 394 13 345 169 29 55 114 257 159 253 285 180 368 21 149 11 315 184 209 347 11 194 26 50 92 150 27 80 1 17 178 382 257 259 391 114 173 262 278 136 381 83 234 303 161 358 153 88 167 214 367 146 360 278 355 136 107 115 1 352 333 341 14 83 51 345 8 249 88
output:
2106080
result:
ok single line: '2106080'
Test #6:
score: 10
Accepted
time: 0ms
memory: 53056kb
input:
82 29 309 40 38 259 308 59 200 235 308 327 77 122 238 23 351 156 227 225 158 135 253 253 1 277 25 53 257 171 92 13 10 342 17 103 137 11 64 131 258 60 225 363 63 130 24 290 252 237 80 152 77 150 312 294 163 330 232 351 211 191 29 153 330 56 306 89 261 93 124 62 58 329 291 68 30 3 119 39 221 123 140 75
output:
4050584
result:
ok single line: '4050584'
Test #7:
score: 10
Accepted
time: 540ms
memory: 53072kb
input:
2970 1905 1 6 1 10 3 2 4 6 2 2 6 7 4 9 3 6 4 8 9 7 1 6 7 1 6 9 4 8 9 9 3 4 5 6 5 6 8 3 8 8 5 9 3 6 2 9 7 2 9 10 9 7 7 3 2 6 6 3 9 2 3 8 5 2 3 4 4 1 10 2 9 2 10 7 2 4 2 9 9 4 5 7 4 1 7 2 7 2 9 5 9 8 5 5 10 8 3 4 7 9 5 2 7 10 9 2 5 4 4 9 10 8 2 6 10 1 6 9 8 8 7 8 1 6 8 7 6 6 2 1 8 8 9 3 4 6 8 6 5 4 5 ...
output:
10041174
result:
ok single line: '10041174'
Test #8:
score: 10
Accepted
time: 59ms
memory: 52956kb
input:
2583 171 8 5 6 11 11 2 7 8 10 4 7 7 2 2 3 7 11 4 1 11 7 10 10 1 11 4 8 1 1 8 3 3 1 7 5 2 6 11 4 2 11 11 3 10 4 2 6 8 6 2 6 3 2 3 3 5 5 11 6 5 4 10 1 9 1 1 7 2 5 8 9 9 1 8 6 1 11 11 11 2 3 9 8 3 9 8 10 6 6 7 9 3 1 6 3 5 2 1 8 11 3 3 3 6 8 1 3 9 11 9 10 8 3 3 11 7 1 6 6 3 3 8 6 6 11 10 1 7 3 8 3 1 7 4...
output:
112832
result:
ok single line: '112832'
Test #9:
score: 10
Accepted
time: 45ms
memory: 53016kb
input:
2146 152 6 1 4 6 15 20 15 13 20 20 20 13 11 9 14 15 3 4 16 16 16 3 1 10 16 3 16 12 2 12 14 20 14 4 10 20 2 8 16 4 1 8 12 16 3 9 20 15 9 14 13 9 17 9 16 11 11 14 18 6 5 20 11 3 7 20 5 3 6 20 2 11 11 13 4 9 8 19 11 16 13 4 7 3 15 4 12 13 20 17 13 1 12 20 9 19 13 1 6 15 10 14 16 8 6 11 10 10 2 18 19 6 ...
output:
680431
result:
ok single line: '680431'
Test #10:
score: 10
Accepted
time: 73ms
memory: 52956kb
input:
2978 186 2 15 7 17 8 16 1 5 12 1 6 2 10 10 17 16 15 20 5 16 13 2 13 11 8 3 3 18 9 16 4 2 1 7 8 16 10 17 2 9 16 6 9 19 5 4 15 1 1 11 5 18 13 17 20 1 19 7 16 13 11 5 12 14 6 13 7 9 13 7 7 7 6 1 1 13 17 19 2 16 4 7 6 1 15 18 11 17 15 16 3 1 9 6 13 3 15 7 15 10 17 2 17 9 1 8 10 20 11 7 17 11 4 12 12 1 1...
output:
922056
result:
ok single line: '922056'