# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
987571 |
2024-05-23T05:14:24 Z |
cig32 |
Fruits (NOI22_fruits) |
C++17 |
|
610 ms |
1048576 KB |
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 4e5 + 10;
int ps[2010][2010];
int freq(int l, int r, int lb, int ub) {
return ps[r][ub] - ps[l-1][ub] - ps[r][lb-1] + ps[l-1][lb-1];
}
void solve(int tc) {
int n;
cin >> n;
int a[n+1], c[n+1];
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=n; i++) cin >> c[i];
int sumai = 0, sumci = 0;
for(int i=1; i<=n; i++) {
sumai += a[i];
sumci += c[i];
}
if(sumai == -n) {
int sum = 0;
for(int i=n; i>=1; i--) {
sum += c[i];
cout << sum << " \n"[i == 1];
}
return;
}
int dp[n+1][n+1];
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
dp[i][j] = -1e18;
ps[i][j] = 0;
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
ps[i][j] = ps[i-1][j] + ps[i][j-1] - ps[i-1][j-1] + (a[i] == j);
}
}
bool chosen[n+1];
for(int i=1; i<=n; i++) chosen[i] = 0;
for(int i=1; i<=n; i++) if(a[i] > 0) chosen[a[i]] = 1;
dp[0][0] = 0;
int wow = 0;
for(int i=1; i<=n; i++) {
int mx = -1e18;
for(int j=0; j<i; j++) mx = max(mx, dp[j][i-1]);
for(int j=i; j<=n; j++) {
if(a[i] > j || freq(i + 1, n, 1, j) > j - i) dp[j][i] = -1e18;
else if(a[i] == j) dp[j][i] = mx + c[j];
else if(chosen[j]) dp[j][i] = max(dp[j][i], dp[j][i-1]);
else if(a[i] < j && a[i] >= 1) dp[j][i] = max(dp[j][i], dp[j][i-1]);
else {
assert(a[i] == -1);
dp[j][i] = mx + c[j];
dp[j][i] = max(dp[j][i], dp[j][i-1]);
}
mx = max(mx, dp[j][i-1]);
}
for(int j=1; j<=n; j++) wow = max(wow, dp[j][i]);
cout << wow << " \n"[i == n];
}
if(sumci == n && n > 2000) {
int p[n+1];
for(int i=1; i<=n; i++) p[i] = 0;
for(int i=1; i<=n; i++) {
if(a[i] > 0) p[a[i]] = 1;
}
int mx = 0;
queue<int> q;
for(int i=1; i<=n; i++) {
if(p[i] == 0) q.push(i);
}
int ans = 0;
for(int i=1; i<=n; i++) {
if(a[i] == -1) {
while(q.size() && q.front() < mx) {
q.pop();
}
if(q.size()) {
mx = q.front();
q.pop();
ans++;
}
}
else {
if(a[i] > mx) {
mx = a[i];
ans++;
}
}
cout << ans << " \n"[i == n];
}
return;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1; //cin>>t;
for(int i=1; i<=t; i++) solve(i);
}
/*
g++ T2443.cpp -std=c++17 -O2 -o T2443
./T2443 < input.txt
g++ gen.cpp -std=c++17 -O2 -o gen
g++ checker.cpp -std=c++17 -O2 -o checker
g++ T2443.cpp -std=c++17 -O2 -o T2443
g++ T2443_brute.cpp -std=c++17 -O2 -o T2443_brute
for((i=1;; ++i)); do
./gen $i > input.txt
./T2443 < input.txt > output.txt
./T2443_brute < input.txt > answer.txt
./checker
echo "Passed test: " $i
done
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
37 ms |
8532 KB |
Output is correct |
5 |
Correct |
77 ms |
16504 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 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 |
2 ms |
4952 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
2 ms |
3160 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 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 |
2 ms |
4952 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
2 ms |
3160 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
88 ms |
63324 KB |
Output is correct |
29 |
Correct |
96 ms |
63316 KB |
Output is correct |
30 |
Correct |
76 ms |
63312 KB |
Output is correct |
31 |
Correct |
21 ms |
20572 KB |
Output is correct |
32 |
Correct |
22 ms |
20568 KB |
Output is correct |
33 |
Correct |
52 ms |
41564 KB |
Output is correct |
34 |
Correct |
67 ms |
49756 KB |
Output is correct |
35 |
Correct |
49 ms |
41748 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
70 ms |
52052 KB |
Output is correct |
39 |
Correct |
68 ms |
45744 KB |
Output is correct |
40 |
Correct |
69 ms |
54204 KB |
Output is correct |
41 |
Correct |
87 ms |
56452 KB |
Output is correct |
42 |
Correct |
84 ms |
57560 KB |
Output is correct |
43 |
Correct |
77 ms |
58016 KB |
Output is correct |
44 |
Correct |
86 ms |
58240 KB |
Output is correct |
45 |
Correct |
97 ms |
63576 KB |
Output is correct |
46 |
Correct |
85 ms |
63312 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
610 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 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 |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
1116 KB |
Output is correct |
14 |
Correct |
37 ms |
8532 KB |
Output is correct |
15 |
Correct |
77 ms |
16504 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
1 ms |
1880 KB |
Output is correct |
19 |
Correct |
2 ms |
3160 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
1368 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
88 ms |
63324 KB |
Output is correct |
35 |
Correct |
96 ms |
63316 KB |
Output is correct |
36 |
Correct |
76 ms |
63312 KB |
Output is correct |
37 |
Correct |
21 ms |
20572 KB |
Output is correct |
38 |
Correct |
22 ms |
20568 KB |
Output is correct |
39 |
Correct |
52 ms |
41564 KB |
Output is correct |
40 |
Correct |
67 ms |
49756 KB |
Output is correct |
41 |
Correct |
49 ms |
41748 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
70 ms |
52052 KB |
Output is correct |
45 |
Correct |
68 ms |
45744 KB |
Output is correct |
46 |
Correct |
69 ms |
54204 KB |
Output is correct |
47 |
Correct |
87 ms |
56452 KB |
Output is correct |
48 |
Correct |
84 ms |
57560 KB |
Output is correct |
49 |
Correct |
77 ms |
58016 KB |
Output is correct |
50 |
Correct |
86 ms |
58240 KB |
Output is correct |
51 |
Correct |
97 ms |
63576 KB |
Output is correct |
52 |
Correct |
85 ms |
63312 KB |
Output is correct |
53 |
Correct |
1 ms |
344 KB |
Output is correct |
54 |
Correct |
1 ms |
344 KB |
Output is correct |
55 |
Runtime error |
610 ms |
1048576 KB |
Execution killed with signal 9 |
56 |
Halted |
0 ms |
0 KB |
- |