#include "homecoming.h"
#include <iostream>
using namespace std;
using ll = long long;
const int MAXN = 2000000;
ll INF = 10000000000000000LL;
struct DpVector
{
ll a, b;
static const DpVector id;
};
const DpVector DpVector::id = {0, -INF};
struct DpMatrix
{
ll a, b, c, d;
/// f({x, y}) = {max(x+a, y+b), max(x+c, y+d)}
/// ( a b )
/// ( c d )
static const DpMatrix id;
static void mult(const DpMatrix &a, const DpMatrix &b, DpMatrix &c)
{
c.a = max(a.a + b.a, a.b + b.c);
c.b = max(a.a + b.b, a.b + b.d);
c.c = max(a.c + b.a, a.d + b.c);
c.d = max(a.c + b.b, a.d + b.d);
}
static void mult(const DpMatrix &a, const DpVector &b, DpVector &c)
{
c.a = max(a.a + b.a, a.b + b.b);
c.b = max(a.c + b.a, a.d + b.b);
}
};
const DpMatrix DpMatrix::id = {0, -INF, -INF, 0};
int n, k;
int *a, *b;
ll sb[MAXN+2];
DpMatrix pref[MAXN+2], suf[MAXN+2], auxmat;
DpVector auxvec[2];
void calcSb()
{
sb[0] = 0;
for (int i = 0; i < k; i++) {
sb[0] += b[i];
}
for (int i = 1; i < n; i++) {
sb[i] = sb[i-1] - b[i-1] + b[(i+k-1)%n];
}
}
void calcPrefMats()
{
pref[0] = {0, 0, a[0]-sb[0], a[0]-b[k-1]};
for (int i = 1; i < n; i++) {
auxmat.c = a[i] - sb[i];
auxmat.d = a[i] - b[(i+k-1)%n];
DpMatrix::mult(auxmat, pref[i-1], pref[i]);
}
}
void calcSufMats()
{
suf[n-1] = {0, 0, a[n-1]-sb[n-1], a[n-1]-b[(n+k-2)%n]};
for (int i = n-2; i >= 0; i--) {
auxmat.c = a[i] - sb[i];
auxmat.d = a[i] - b[(i+k-1)%n];
DpMatrix::mult(suf[i+1], auxmat, suf[i]);
}
}
long long getSimpleAns()
{
long long ans = 0;
for (int i = 0; i < n; i++) {
ans = ans + a[i] - b[i];
}
return max(ans, 0LL);
}
long long solve(int nn, int kk, int *aa, int *bb)
{
n = nn;
k = kk;
a = aa;
b = bb;
calcSb();
calcPrefMats();
calcSufMats();
long long ans = getSimpleAns();
DpMatrix::mult(pref[n-1], DpVector::id, auxvec[0]);
ans = max(ans, max(auxvec[0].a, auxvec[0].b));
for (int i = 1; i < n; i++) {
DpMatrix::mult(suf[i], DpVector::id, auxvec[0]);
DpMatrix::mult(pref[i-1], auxvec[0], auxvec[1]);
ans = max(ans, max(auxvec[1].a, auxvec[1].b));
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
49020 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
164 ms |
196440 KB |
Output is correct |
4 |
Correct |
2 ms |
2136 KB |
Output is correct |
5 |
Correct |
7 ms |
4148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
43 ms |
49020 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
164 ms |
196440 KB |
Output is correct |
14 |
Correct |
2 ms |
2136 KB |
Output is correct |
15 |
Correct |
7 ms |
4148 KB |
Output is correct |
16 |
Correct |
163 ms |
196432 KB |
Output is correct |
17 |
Correct |
62 ms |
37948 KB |
Output is correct |
18 |
Correct |
103 ms |
46052 KB |
Output is correct |
19 |
Correct |
106 ms |
106016 KB |
Output is correct |
20 |
Correct |
139 ms |
179664 KB |
Output is correct |
21 |
Correct |
99 ms |
103436 KB |
Output is correct |