//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define vi vector<int>
#define vl vector<ll>
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;
const int MAX = 5007, MOD = 1e9 + 7;
ll a[MAX], bit[MAX], dp[MAX][MAX], ans[MAX];
void upd(int x, int v){
while(x < MAX){
bit[x] += v;
x += x & -x;
}
}
int qry(int x){
int res = 0;
while(x){
res += bit[x];
x -= x & -x;
}
return res;
}
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
ans[i] = 2e18;
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= i;j++){
for(int k = n;k >= 1;k--){
if(i < a[k]){
dp[j][i] += qry(n);
upd(n + 1, 1);
}else if(j <= a[k] && a[k] <= i){
int pos = i - a[k] + j + 1;
dp[j][i] += qry(pos - 1);
upd(pos, 1);
}
}
for(int k = 1;k <= n;k++){
if(i < a[k])
upd(n + 1, -1);
else if(j <= a[k] && a[k] <= i){
int pos = i - a[k] + j + 1;
upd(pos, -1);
}
}
}
}
for(int i = 0;i < n;i++){
for(int j = 1;j <= n;j++)
ans[j] = min(ans[j], ans[i] + dp[i + 1][j]);
}
cout << ans[n] << '\n';
}
int main(){
IOS
// CASE
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
100 ms |
1184 KB |
Output is correct |
22 |
Correct |
103 ms |
1236 KB |
Output is correct |
23 |
Correct |
101 ms |
1296 KB |
Output is correct |
24 |
Correct |
105 ms |
1212 KB |
Output is correct |
25 |
Correct |
104 ms |
1264 KB |
Output is correct |
26 |
Correct |
107 ms |
1304 KB |
Output is correct |
27 |
Correct |
104 ms |
1304 KB |
Output is correct |
28 |
Correct |
104 ms |
1256 KB |
Output is correct |
29 |
Correct |
105 ms |
1224 KB |
Output is correct |
30 |
Correct |
105 ms |
1312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
100 ms |
1184 KB |
Output is correct |
22 |
Correct |
103 ms |
1236 KB |
Output is correct |
23 |
Correct |
101 ms |
1296 KB |
Output is correct |
24 |
Correct |
105 ms |
1212 KB |
Output is correct |
25 |
Correct |
104 ms |
1264 KB |
Output is correct |
26 |
Correct |
107 ms |
1304 KB |
Output is correct |
27 |
Correct |
104 ms |
1304 KB |
Output is correct |
28 |
Correct |
104 ms |
1256 KB |
Output is correct |
29 |
Correct |
105 ms |
1224 KB |
Output is correct |
30 |
Correct |
105 ms |
1312 KB |
Output is correct |
31 |
Execution timed out |
5035 ms |
5212 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
100 ms |
1184 KB |
Output is correct |
22 |
Correct |
103 ms |
1236 KB |
Output is correct |
23 |
Correct |
101 ms |
1296 KB |
Output is correct |
24 |
Correct |
105 ms |
1212 KB |
Output is correct |
25 |
Correct |
104 ms |
1264 KB |
Output is correct |
26 |
Correct |
107 ms |
1304 KB |
Output is correct |
27 |
Correct |
104 ms |
1304 KB |
Output is correct |
28 |
Correct |
104 ms |
1256 KB |
Output is correct |
29 |
Correct |
105 ms |
1224 KB |
Output is correct |
30 |
Correct |
105 ms |
1312 KB |
Output is correct |
31 |
Execution timed out |
5035 ms |
5212 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |