QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#502863 | #7067. The Great Wall | Decade | WA | 2ms | 24144kb | C++17 | 3.0kb | 2024-08-03 15:19:35 | 2024-08-03 15:19:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
mt19937 rng;
uniform_int_distribution <ull> dist(0,1ull<<63);
//#define mp make_pair
inline void print(__int128 x){//输出模板
if(x<0){
putchar('-');
x=-x;
}
if(x>9) print(x/10);
putchar(x%10+'0');
}
inline int read()
{
int X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag)
return X;
return ~(X - 1);
}
const ll mod = 1e9 + 7,INF = 0x3f3f3f3f;
ll qpow(ll a, ll b)
{
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
const int N = 10010,M = 1000010;
int a[N],f[N][N];
int stmax[N][22], stmin[N][22], mn[N];
int n,tt;
struct node
{
int id,l;
}stk[N];
void init(){
mn[0] = -1;
for (int i = 1; i <= n; i++){
mn[i] = ((i & (i - 1)) == 0) ? mn[i - 1] + 1 : mn[i - 1];
stmax[i][0] = stmin[i][0] = a[i];
}
for (int j = 1; j <= mn[n]; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++){
stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]);
stmin[i][j] = min(stmin[i][j - 1], stmin[i + (1 << (j - 1))][j - 1]);
}
}
inline int rmq_max(int L, int R){
int k = mn[R - L + 1];
return max(stmax[L][k], stmax[R - (1 << k) + 1][k]);
}
inline int rmq_min(int L, int R){
int k = mn[R - L + 1];
return min(stmin[L][k], stmin[R - (1 << k) + 1][k]);
}
int cal(int k,int r,int now)
{
return f[stk[now].id][k]+rmq_max(stk[now].l+1,r)-rmq_min(stk[now].l+1,r);
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
init();
for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) f[i][j] = -1;
f[0][0] = 0;
for(int k=1;k<=n;k++)
{
tt = -1;
for(int i=1;i<=n;i++)
{
if(i>=2)
{
if(f[i-2][k-1]!=-1)
{
while(tt!=-1&&cal(k-1,i,tt)<=f[i-2][k-1]+max(a[i-1],a[i])-min(a[i-1],a[i])) tt--;
stk[++tt] = {i-2,i-2};
}
}
if(tt!=-1) f[i][k] = max(cal(k-1,i,0),f[i][k]);
f[i][k] = max(f[i][k],f[i-1][k-1]);
}
}
for(int i=1;i<=n;i++)
{
cout<<f[n][i]<<'\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin>>t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5704kb
input:
5 1 2 3 4 5
output:
4 3 2 1 0
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 5644kb
input:
5 1 2 1 2 1
output:
1 2 2 1 0
result:
ok 5 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 5688kb
input:
6 1 1 4 5 1 4
output:
4 7 7 7 4 0
result:
ok 6 lines
Test #4:
score: 0
Accepted
time: 1ms
memory: 5644kb
input:
6 1 9 1 9 8 1
output:
8 16 23 16 8 0
result:
ok 6 lines
Test #5:
score: 0
Accepted
time: 1ms
memory: 5688kb
input:
12 1 1 4 5 1 4 1 9 1 9 8 1
output:
8 16 23 27 30 30 30 27 23 16 8 0
result:
ok 12 lines
Test #6:
score: 0
Accepted
time: 0ms
memory: 5640kb
input:
1 79932
output:
0
result:
ok single line: '0'
Test #7:
score: -100
Wrong Answer
time: 2ms
memory: 24144kb
input:
500 2 4 2 9 3 1 9 1 2 9 9 9 2 3 8 6 6 5 6 4 9 9 6 4 4 3 1 3 4 6 9 7 1 8 3 10 1 1 1 1 2 2 8 4 4 1 9 1 3 7 5 10 1 3 2 1 3 4 8 4 2 2 2 3 10 8 8 8 6 1 3 5 10 10 6 7 9 7 3 2 5 5 4 10 2 2 8 6 10 8 8 10 4 1 9 8 1 7 10 10 1 1 4 3 8 7 10 3 3 7 3 3 4 1 1 4 1 7 8 2 8 9 6 4 6 6 7 1 3 9 4 4 4 10 8 5 6 7 8 6 6 5 ...
output:
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 188 196 204 212 221 229 237 246 254 261 267 275 283 291 299 308 316 323 329 335 344 352 359 365 372 379 385 390 396 401 407 415 422 428 435 442 448 453 459 467 473 481 490 498 505 511 518 525 531 536 543 550 556 561 567 573 579 586 ...
result:
wrong answer 21st lines differ - expected: '189', found: '188'