#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e16;
const int mod = 1e9 + 7;
struct Mint
{
int val;
Mint(int x = 0)
{
val = x % mod;
}
Mint(long long x)
{
val = x % mod;
}
Mint operator+(Mint oth)
{
return val + oth.val;
}
Mint operator-(Mint oth)
{
return val - oth.val + mod;
}
Mint operator*(Mint oth)
{
return 1ll * val * oth.val;
}
};
Mint powmod(int a, int b)
{
if (b == 0)
{
return 1;
}
if (b % 2 == 1)
{
return powmod(a, b - 1) * a;
}
Mint P = powmod(a, b / 2);
return P * P;
}
struct aint
{
vector<ll> a;
void resize(int n)
{
a = vector<ll>(4 * n, inf);
}
void update(int node, int left, int right, int pos, ll val)
{
if (left == right)
{
a[node] = val;
return;
}
int mid = (left + right) / 2;
if (pos <= mid)
{
update(2 * node, left, mid, pos, val);
}
else
{
update(2 * node + 1, mid + 1, right, pos, val);
}
a[node] = min(a[2 * node], a[2 * node + 1]);
}
ll query(int node, int left, int right, int st, int dr)
{
if (right < st || left > dr)
{
return inf;
}
if (st <= left && dr >= right)
{
return a[node];
}
int mid = (left + right) / 2;
return min(query(2 * node, left, mid, st, dr), query(2 * node + 1, mid + 1, right, st, dr));
}
};
int solve(int n, int k, int *S)
{
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i)
{
a[i] = S[i - 1];
}
Mint ans = 0;
vector<ll> dp(n + 1, inf);
vector<int> s;
aint tree;
tree.resize(n);
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
while (!s.empty() && a[s.back()] < a[i])
{
tree.update(1, 1, n, s.back(), inf);
s.pop_back();
}
if (s.empty())
{
tree.update(1, 1, n, i, a[i]);
}
else
{
tree.update(1, 1, n, i, a[i] + dp[s.back()]);
}
s.push_back(i);
int st = 0, dr = (int)s.size() - 1;
int rep = -1;
while (st <= dr)
{
int mid = (st + dr) / 2;
if (s[mid] >= i - k + 1)
{
rep = s[mid];
dr = mid - 1;
}
else
{
st = mid + 1;
}
}
assert(rep != -1);
if (rep + 1 <= i)
{
dp[i] = tree.query(1, 1, n, rep + 1, i);
}
dp[i] = min(dp[i], a[rep] + dp[max(0, i - k)]);
ans = ans + powmod(23, n - i) * dp[i];
}
return ans.val;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
564 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
564 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
210 ms |
20064 KB |
Output is correct |
16 |
Correct |
216 ms |
24204 KB |
Output is correct |
17 |
Correct |
223 ms |
24404 KB |
Output is correct |
18 |
Correct |
213 ms |
24104 KB |
Output is correct |
19 |
Correct |
205 ms |
23892 KB |
Output is correct |
20 |
Correct |
206 ms |
24156 KB |
Output is correct |
21 |
Correct |
223 ms |
24192 KB |
Output is correct |
22 |
Correct |
221 ms |
24336 KB |
Output is correct |
23 |
Correct |
223 ms |
24336 KB |
Output is correct |
24 |
Correct |
216 ms |
24144 KB |
Output is correct |
25 |
Correct |
216 ms |
24400 KB |
Output is correct |
26 |
Correct |
211 ms |
24400 KB |
Output is correct |
27 |
Correct |
211 ms |
24144 KB |
Output is correct |
28 |
Correct |
217 ms |
24292 KB |
Output is correct |
29 |
Correct |
212 ms |
24152 KB |
Output is correct |
30 |
Correct |
212 ms |
24152 KB |
Output is correct |
31 |
Correct |
215 ms |
24408 KB |
Output is correct |
32 |
Correct |
226 ms |
24224 KB |
Output is correct |
33 |
Correct |
213 ms |
24208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
20064 KB |
Output is correct |
2 |
Correct |
216 ms |
24204 KB |
Output is correct |
3 |
Correct |
223 ms |
24404 KB |
Output is correct |
4 |
Correct |
213 ms |
24104 KB |
Output is correct |
5 |
Correct |
205 ms |
23892 KB |
Output is correct |
6 |
Correct |
206 ms |
24156 KB |
Output is correct |
7 |
Correct |
223 ms |
24192 KB |
Output is correct |
8 |
Correct |
221 ms |
24336 KB |
Output is correct |
9 |
Correct |
223 ms |
24336 KB |
Output is correct |
10 |
Correct |
216 ms |
24144 KB |
Output is correct |
11 |
Correct |
216 ms |
24400 KB |
Output is correct |
12 |
Correct |
211 ms |
24400 KB |
Output is correct |
13 |
Correct |
211 ms |
24144 KB |
Output is correct |
14 |
Correct |
217 ms |
24292 KB |
Output is correct |
15 |
Correct |
212 ms |
24152 KB |
Output is correct |
16 |
Correct |
212 ms |
24152 KB |
Output is correct |
17 |
Correct |
215 ms |
24408 KB |
Output is correct |
18 |
Correct |
226 ms |
24224 KB |
Output is correct |
19 |
Correct |
213 ms |
24208 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
564 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Execution timed out |
1018 ms |
142012 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |