# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
490591 |
2021-11-28T07:59:51 Z |
fhvirus |
Rope (JOI17_rope) |
C++17 |
|
1290 ms |
84484 KB |
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int N, M;
cin >> N >> M;
vector<int> rope(N);
for (int &i: rope)
cin >> i;
vector<int> cnt(M+1, 0);
vector<int> num(N+1, 0);
int mxv = 0;
num[0] = M;
auto add = [&](int v) {
--num[cnt[v]];
++cnt[v];
++num[cnt[v]];
if(cnt[v] > mxv)
mxv = cnt[v];
};
auto rem = [&](int v) {
--num[cnt[v]];
--cnt[v];
++num[cnt[v]];
if(num[mxv] == 0)
--mxv;
};
for (int &i: rope)
add(i);
vector<int> ans(M+1, N);
for (int k = 0; k < 2; ++k) {
vector<vector<int>> adj(M+1, vector<int>());
if(k == 1) adj[rope[0]].pb(0);
for (int i = k; i < N; i += 2) {
int u = rope[i];
int v = (i + 1 < N ? rope[i + 1] : 0);
adj[u].pb(v);
if (u != v and v != 0)
adj[v].pb(u);
}
for (int i = 1; i <= M; ++i) {
int cur = 0;
for (int &j: adj[i]) {
++cur;
rem(i);
if (j == i)
++cur;
if (j != 0)
rem(j);
}
ans[i] = min(ans[i], N - cur - mxv);
for (int &j: adj[i]) {
add(i);
if (j != 0)
add(j);
}
}
}
for (int i = 1; i <= M; ++i)
cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
0 ms |
208 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
0 ms |
208 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
13 ms |
1868 KB |
Output is correct |
28 |
Correct |
13 ms |
1824 KB |
Output is correct |
29 |
Correct |
14 ms |
1920 KB |
Output is correct |
30 |
Correct |
13 ms |
1900 KB |
Output is correct |
31 |
Correct |
14 ms |
1872 KB |
Output is correct |
32 |
Correct |
13 ms |
1864 KB |
Output is correct |
33 |
Correct |
14 ms |
1920 KB |
Output is correct |
34 |
Correct |
12 ms |
1984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
0 ms |
208 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
13 ms |
1868 KB |
Output is correct |
28 |
Correct |
13 ms |
1824 KB |
Output is correct |
29 |
Correct |
14 ms |
1920 KB |
Output is correct |
30 |
Correct |
13 ms |
1900 KB |
Output is correct |
31 |
Correct |
14 ms |
1872 KB |
Output is correct |
32 |
Correct |
13 ms |
1864 KB |
Output is correct |
33 |
Correct |
14 ms |
1920 KB |
Output is correct |
34 |
Correct |
12 ms |
1984 KB |
Output is correct |
35 |
Correct |
15 ms |
2000 KB |
Output is correct |
36 |
Correct |
15 ms |
1988 KB |
Output is correct |
37 |
Correct |
15 ms |
2068 KB |
Output is correct |
38 |
Correct |
20 ms |
1980 KB |
Output is correct |
39 |
Correct |
16 ms |
1988 KB |
Output is correct |
40 |
Correct |
17 ms |
2108 KB |
Output is correct |
41 |
Correct |
16 ms |
2108 KB |
Output is correct |
42 |
Correct |
15 ms |
2120 KB |
Output is correct |
43 |
Correct |
15 ms |
2124 KB |
Output is correct |
44 |
Correct |
15 ms |
2148 KB |
Output is correct |
45 |
Correct |
14 ms |
2112 KB |
Output is correct |
46 |
Correct |
13 ms |
2124 KB |
Output is correct |
47 |
Correct |
14 ms |
2076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
0 ms |
208 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
13 ms |
1868 KB |
Output is correct |
28 |
Correct |
13 ms |
1824 KB |
Output is correct |
29 |
Correct |
14 ms |
1920 KB |
Output is correct |
30 |
Correct |
13 ms |
1900 KB |
Output is correct |
31 |
Correct |
14 ms |
1872 KB |
Output is correct |
32 |
Correct |
13 ms |
1864 KB |
Output is correct |
33 |
Correct |
14 ms |
1920 KB |
Output is correct |
34 |
Correct |
12 ms |
1984 KB |
Output is correct |
35 |
Correct |
15 ms |
2000 KB |
Output is correct |
36 |
Correct |
15 ms |
1988 KB |
Output is correct |
37 |
Correct |
15 ms |
2068 KB |
Output is correct |
38 |
Correct |
20 ms |
1980 KB |
Output is correct |
39 |
Correct |
16 ms |
1988 KB |
Output is correct |
40 |
Correct |
17 ms |
2108 KB |
Output is correct |
41 |
Correct |
16 ms |
2108 KB |
Output is correct |
42 |
Correct |
15 ms |
2120 KB |
Output is correct |
43 |
Correct |
15 ms |
2124 KB |
Output is correct |
44 |
Correct |
15 ms |
2148 KB |
Output is correct |
45 |
Correct |
14 ms |
2112 KB |
Output is correct |
46 |
Correct |
13 ms |
2124 KB |
Output is correct |
47 |
Correct |
14 ms |
2076 KB |
Output is correct |
48 |
Correct |
167 ms |
19424 KB |
Output is correct |
49 |
Correct |
167 ms |
19408 KB |
Output is correct |
50 |
Correct |
170 ms |
19348 KB |
Output is correct |
51 |
Correct |
178 ms |
18940 KB |
Output is correct |
52 |
Correct |
136 ms |
17664 KB |
Output is correct |
53 |
Correct |
151 ms |
18536 KB |
Output is correct |
54 |
Correct |
160 ms |
17532 KB |
Output is correct |
55 |
Correct |
165 ms |
17640 KB |
Output is correct |
56 |
Correct |
152 ms |
17140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
0 ms |
208 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
13 ms |
1868 KB |
Output is correct |
28 |
Correct |
13 ms |
1824 KB |
Output is correct |
29 |
Correct |
14 ms |
1920 KB |
Output is correct |
30 |
Correct |
13 ms |
1900 KB |
Output is correct |
31 |
Correct |
14 ms |
1872 KB |
Output is correct |
32 |
Correct |
13 ms |
1864 KB |
Output is correct |
33 |
Correct |
14 ms |
1920 KB |
Output is correct |
34 |
Correct |
12 ms |
1984 KB |
Output is correct |
35 |
Correct |
15 ms |
2000 KB |
Output is correct |
36 |
Correct |
15 ms |
1988 KB |
Output is correct |
37 |
Correct |
15 ms |
2068 KB |
Output is correct |
38 |
Correct |
20 ms |
1980 KB |
Output is correct |
39 |
Correct |
16 ms |
1988 KB |
Output is correct |
40 |
Correct |
17 ms |
2108 KB |
Output is correct |
41 |
Correct |
16 ms |
2108 KB |
Output is correct |
42 |
Correct |
15 ms |
2120 KB |
Output is correct |
43 |
Correct |
15 ms |
2124 KB |
Output is correct |
44 |
Correct |
15 ms |
2148 KB |
Output is correct |
45 |
Correct |
14 ms |
2112 KB |
Output is correct |
46 |
Correct |
13 ms |
2124 KB |
Output is correct |
47 |
Correct |
14 ms |
2076 KB |
Output is correct |
48 |
Correct |
167 ms |
19424 KB |
Output is correct |
49 |
Correct |
167 ms |
19408 KB |
Output is correct |
50 |
Correct |
170 ms |
19348 KB |
Output is correct |
51 |
Correct |
178 ms |
18940 KB |
Output is correct |
52 |
Correct |
136 ms |
17664 KB |
Output is correct |
53 |
Correct |
151 ms |
18536 KB |
Output is correct |
54 |
Correct |
160 ms |
17532 KB |
Output is correct |
55 |
Correct |
165 ms |
17640 KB |
Output is correct |
56 |
Correct |
152 ms |
17140 KB |
Output is correct |
57 |
Correct |
1290 ms |
84484 KB |
Output is correct |
58 |
Correct |
946 ms |
56724 KB |
Output is correct |
59 |
Correct |
1026 ms |
56364 KB |
Output is correct |
60 |
Correct |
1050 ms |
63468 KB |
Output is correct |
61 |
Correct |
1043 ms |
63556 KB |
Output is correct |
62 |
Correct |
455 ms |
38568 KB |
Output is correct |
63 |
Correct |
612 ms |
46680 KB |
Output is correct |
64 |
Correct |
501 ms |
41604 KB |
Output is correct |
65 |
Correct |
250 ms |
28816 KB |
Output is correct |