#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int a[1111111];
int ans[1111111];
int n,m;
vi adj[1111111];
map<int,int> ma;
int sc[1111111];
void del(int x)
{
ma[x]--;
if(!ma[x]) ma.erase(x);
}
void add(int x)
{
ma[x]++;
}
void change(int id, int y)
{
del(sc[id]);
sc[id]=y;
add(sc[id]);
}
int getmx()
{
if(ma.empty()) return -int(1e9);
return (*prev(ma.end())).fi;
}
int sum[1111111];
int ct[1111111];
void solve(vector<int> &vec, vector<int> &extra)
{
assert(int(vec.size())%2==0);
vector<ii> pairs;
memset(ct,0,sizeof(ct));
for(int i=0;i<vec.size();i+=2)
{
if(vec[i]!=vec[i+1]) pairs.pb(mp(vec[i],vec[i+1]));
else ct[vec[i]]++;
}
for(int i=0;i<m;i++) adj[i].clear();
for(ii x:pairs)
{
adj[x.fi].pb(x.se); adj[x.se].pb(x.fi);
}
ma.clear();
for(int i=0;i<m;i++)
{
int res=2*ct[i];
for(int x:extra)
{
if(x==i) res++;
}
res+=adj[i].size();
sc[i]=res; add(res);
}
for(int i=0;i<m;i++)
{
del(sc[i]); int deg=adj[i].size();
int t=0;
for(int v:extra)
{
if(v==i) t++;
}
vi V;
for(int v:adj[i])
{
sum[v]++;
if(sum[v]==1) V.pb(v);
}
vi ori;
for(int v:V)
{
ori.pb(sc[v]);
change(v, sc[v] - sum[v]);
}
ans[i] = min(ans[i], n - (2*ct[i] + getmx() + deg + t));
int ptr=0;
for(int v:V)
{
change(v,ori[ptr++]);
}
for(int v:adj[i])
{
sum[v]--;
}
add(sc[i]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++) ans[i]=int(1e9);
vector<int> cnt(m,0);
for(int i=0;i<n;i++)
{
cin>>a[i]; a[i]--; cnt[a[i]]++;
}
for(int i=0;i<m;i++) ans[i]=n-cnt[i];
if(n%2==0)
{
vi tmp; vi emp;
for(int i=0;i<n;i++) tmp.pb(a[i]);
solve(tmp,emp);
vi extra;
extra.pb(a[0]); extra.pb(a[n-1]);
vi serious;
for(int i=1;i<n-1;i++) serious.pb(a[i]);
solve(serious,extra);
}
else
{
for(int z=0;z<2;z++)
{
vi extra;
extra.pb(a[n-1]);
vi serious;
for(int i=0;i<n-1;i++) serious.pb(a[i]);
solve(serious,extra);
reverse(a,a+n);
}
}
for(int i=0;i<m;i++)
{
cout<<ans[i]<<'\n';
}
}
Compilation message
rope.cpp: In function 'void solve(std::vector<int>&, std::vector<int>&)':
rope.cpp:57:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<vec.size();i+=2)
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
30712 KB |
Output is correct |
2 |
Correct |
30 ms |
30820 KB |
Output is correct |
3 |
Correct |
30 ms |
30884 KB |
Output is correct |
4 |
Correct |
32 ms |
30936 KB |
Output is correct |
5 |
Correct |
31 ms |
31056 KB |
Output is correct |
6 |
Correct |
30 ms |
31056 KB |
Output is correct |
7 |
Correct |
31 ms |
31056 KB |
Output is correct |
8 |
Correct |
32 ms |
31056 KB |
Output is correct |
9 |
Correct |
30 ms |
31056 KB |
Output is correct |
10 |
Correct |
33 ms |
31056 KB |
Output is correct |
11 |
Correct |
32 ms |
31056 KB |
Output is correct |
12 |
Correct |
29 ms |
31056 KB |
Output is correct |
13 |
Correct |
31 ms |
31056 KB |
Output is correct |
14 |
Correct |
29 ms |
31056 KB |
Output is correct |
15 |
Correct |
29 ms |
31056 KB |
Output is correct |
16 |
Correct |
36 ms |
31056 KB |
Output is correct |
17 |
Correct |
30 ms |
31056 KB |
Output is correct |
18 |
Correct |
32 ms |
31056 KB |
Output is correct |
19 |
Correct |
35 ms |
31056 KB |
Output is correct |
20 |
Correct |
29 ms |
31056 KB |
Output is correct |
21 |
Correct |
34 ms |
31056 KB |
Output is correct |
22 |
Correct |
35 ms |
31056 KB |
Output is correct |
23 |
Correct |
34 ms |
31056 KB |
Output is correct |
24 |
Correct |
33 ms |
31084 KB |
Output is correct |
25 |
Correct |
35 ms |
31084 KB |
Output is correct |
26 |
Correct |
34 ms |
31084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
30712 KB |
Output is correct |
2 |
Correct |
30 ms |
30820 KB |
Output is correct |
3 |
Correct |
30 ms |
30884 KB |
Output is correct |
4 |
Correct |
32 ms |
30936 KB |
Output is correct |
5 |
Correct |
31 ms |
31056 KB |
Output is correct |
6 |
Correct |
30 ms |
31056 KB |
Output is correct |
7 |
Correct |
31 ms |
31056 KB |
Output is correct |
8 |
Correct |
32 ms |
31056 KB |
Output is correct |
9 |
Correct |
30 ms |
31056 KB |
Output is correct |
10 |
Correct |
33 ms |
31056 KB |
Output is correct |
11 |
Correct |
32 ms |
31056 KB |
Output is correct |
12 |
Correct |
29 ms |
31056 KB |
Output is correct |
13 |
Correct |
31 ms |
31056 KB |
Output is correct |
14 |
Correct |
29 ms |
31056 KB |
Output is correct |
15 |
Correct |
29 ms |
31056 KB |
Output is correct |
16 |
Correct |
36 ms |
31056 KB |
Output is correct |
17 |
Correct |
30 ms |
31056 KB |
Output is correct |
18 |
Correct |
32 ms |
31056 KB |
Output is correct |
19 |
Correct |
35 ms |
31056 KB |
Output is correct |
20 |
Correct |
29 ms |
31056 KB |
Output is correct |
21 |
Correct |
34 ms |
31056 KB |
Output is correct |
22 |
Correct |
35 ms |
31056 KB |
Output is correct |
23 |
Correct |
34 ms |
31056 KB |
Output is correct |
24 |
Correct |
33 ms |
31084 KB |
Output is correct |
25 |
Correct |
35 ms |
31084 KB |
Output is correct |
26 |
Correct |
34 ms |
31084 KB |
Output is correct |
27 |
Correct |
54 ms |
33672 KB |
Output is correct |
28 |
Correct |
43 ms |
33672 KB |
Output is correct |
29 |
Correct |
54 ms |
33672 KB |
Output is correct |
30 |
Correct |
41 ms |
33672 KB |
Output is correct |
31 |
Correct |
43 ms |
33672 KB |
Output is correct |
32 |
Correct |
50 ms |
33672 KB |
Output is correct |
33 |
Correct |
47 ms |
33672 KB |
Output is correct |
34 |
Correct |
48 ms |
33672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
30712 KB |
Output is correct |
2 |
Correct |
30 ms |
30820 KB |
Output is correct |
3 |
Correct |
30 ms |
30884 KB |
Output is correct |
4 |
Correct |
32 ms |
30936 KB |
Output is correct |
5 |
Correct |
31 ms |
31056 KB |
Output is correct |
6 |
Correct |
30 ms |
31056 KB |
Output is correct |
7 |
Correct |
31 ms |
31056 KB |
Output is correct |
8 |
Correct |
32 ms |
31056 KB |
Output is correct |
9 |
Correct |
30 ms |
31056 KB |
Output is correct |
10 |
Correct |
33 ms |
31056 KB |
Output is correct |
11 |
Correct |
32 ms |
31056 KB |
Output is correct |
12 |
Correct |
29 ms |
31056 KB |
Output is correct |
13 |
Correct |
31 ms |
31056 KB |
Output is correct |
14 |
Correct |
29 ms |
31056 KB |
Output is correct |
15 |
Correct |
29 ms |
31056 KB |
Output is correct |
16 |
Correct |
36 ms |
31056 KB |
Output is correct |
17 |
Correct |
30 ms |
31056 KB |
Output is correct |
18 |
Correct |
32 ms |
31056 KB |
Output is correct |
19 |
Correct |
35 ms |
31056 KB |
Output is correct |
20 |
Correct |
29 ms |
31056 KB |
Output is correct |
21 |
Correct |
34 ms |
31056 KB |
Output is correct |
22 |
Correct |
35 ms |
31056 KB |
Output is correct |
23 |
Correct |
34 ms |
31056 KB |
Output is correct |
24 |
Correct |
33 ms |
31084 KB |
Output is correct |
25 |
Correct |
35 ms |
31084 KB |
Output is correct |
26 |
Correct |
34 ms |
31084 KB |
Output is correct |
27 |
Correct |
54 ms |
33672 KB |
Output is correct |
28 |
Correct |
43 ms |
33672 KB |
Output is correct |
29 |
Correct |
54 ms |
33672 KB |
Output is correct |
30 |
Correct |
41 ms |
33672 KB |
Output is correct |
31 |
Correct |
43 ms |
33672 KB |
Output is correct |
32 |
Correct |
50 ms |
33672 KB |
Output is correct |
33 |
Correct |
47 ms |
33672 KB |
Output is correct |
34 |
Correct |
48 ms |
33672 KB |
Output is correct |
35 |
Correct |
103 ms |
33672 KB |
Output is correct |
36 |
Correct |
88 ms |
34064 KB |
Output is correct |
37 |
Correct |
94 ms |
34064 KB |
Output is correct |
38 |
Correct |
81 ms |
34548 KB |
Output is correct |
39 |
Correct |
93 ms |
34952 KB |
Output is correct |
40 |
Correct |
65 ms |
35732 KB |
Output is correct |
41 |
Correct |
59 ms |
36156 KB |
Output is correct |
42 |
Correct |
53 ms |
36508 KB |
Output is correct |
43 |
Correct |
66 ms |
36888 KB |
Output is correct |
44 |
Correct |
58 ms |
37232 KB |
Output is correct |
45 |
Correct |
62 ms |
37652 KB |
Output is correct |
46 |
Correct |
73 ms |
37996 KB |
Output is correct |
47 |
Correct |
56 ms |
38364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
30712 KB |
Output is correct |
2 |
Correct |
30 ms |
30820 KB |
Output is correct |
3 |
Correct |
30 ms |
30884 KB |
Output is correct |
4 |
Correct |
32 ms |
30936 KB |
Output is correct |
5 |
Correct |
31 ms |
31056 KB |
Output is correct |
6 |
Correct |
30 ms |
31056 KB |
Output is correct |
7 |
Correct |
31 ms |
31056 KB |
Output is correct |
8 |
Correct |
32 ms |
31056 KB |
Output is correct |
9 |
Correct |
30 ms |
31056 KB |
Output is correct |
10 |
Correct |
33 ms |
31056 KB |
Output is correct |
11 |
Correct |
32 ms |
31056 KB |
Output is correct |
12 |
Correct |
29 ms |
31056 KB |
Output is correct |
13 |
Correct |
31 ms |
31056 KB |
Output is correct |
14 |
Correct |
29 ms |
31056 KB |
Output is correct |
15 |
Correct |
29 ms |
31056 KB |
Output is correct |
16 |
Correct |
36 ms |
31056 KB |
Output is correct |
17 |
Correct |
30 ms |
31056 KB |
Output is correct |
18 |
Correct |
32 ms |
31056 KB |
Output is correct |
19 |
Correct |
35 ms |
31056 KB |
Output is correct |
20 |
Correct |
29 ms |
31056 KB |
Output is correct |
21 |
Correct |
34 ms |
31056 KB |
Output is correct |
22 |
Correct |
35 ms |
31056 KB |
Output is correct |
23 |
Correct |
34 ms |
31056 KB |
Output is correct |
24 |
Correct |
33 ms |
31084 KB |
Output is correct |
25 |
Correct |
35 ms |
31084 KB |
Output is correct |
26 |
Correct |
34 ms |
31084 KB |
Output is correct |
27 |
Correct |
54 ms |
33672 KB |
Output is correct |
28 |
Correct |
43 ms |
33672 KB |
Output is correct |
29 |
Correct |
54 ms |
33672 KB |
Output is correct |
30 |
Correct |
41 ms |
33672 KB |
Output is correct |
31 |
Correct |
43 ms |
33672 KB |
Output is correct |
32 |
Correct |
50 ms |
33672 KB |
Output is correct |
33 |
Correct |
47 ms |
33672 KB |
Output is correct |
34 |
Correct |
48 ms |
33672 KB |
Output is correct |
35 |
Correct |
103 ms |
33672 KB |
Output is correct |
36 |
Correct |
88 ms |
34064 KB |
Output is correct |
37 |
Correct |
94 ms |
34064 KB |
Output is correct |
38 |
Correct |
81 ms |
34548 KB |
Output is correct |
39 |
Correct |
93 ms |
34952 KB |
Output is correct |
40 |
Correct |
65 ms |
35732 KB |
Output is correct |
41 |
Correct |
59 ms |
36156 KB |
Output is correct |
42 |
Correct |
53 ms |
36508 KB |
Output is correct |
43 |
Correct |
66 ms |
36888 KB |
Output is correct |
44 |
Correct |
58 ms |
37232 KB |
Output is correct |
45 |
Correct |
62 ms |
37652 KB |
Output is correct |
46 |
Correct |
73 ms |
37996 KB |
Output is correct |
47 |
Correct |
56 ms |
38364 KB |
Output is correct |
48 |
Correct |
632 ms |
66624 KB |
Output is correct |
49 |
Correct |
642 ms |
71116 KB |
Output is correct |
50 |
Correct |
641 ms |
71704 KB |
Output is correct |
51 |
Correct |
690 ms |
75860 KB |
Output is correct |
52 |
Correct |
586 ms |
78572 KB |
Output is correct |
53 |
Correct |
379 ms |
87328 KB |
Output is correct |
54 |
Correct |
282 ms |
90844 KB |
Output is correct |
55 |
Correct |
267 ms |
95240 KB |
Output is correct |
56 |
Correct |
284 ms |
99480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
30712 KB |
Output is correct |
2 |
Correct |
30 ms |
30820 KB |
Output is correct |
3 |
Correct |
30 ms |
30884 KB |
Output is correct |
4 |
Correct |
32 ms |
30936 KB |
Output is correct |
5 |
Correct |
31 ms |
31056 KB |
Output is correct |
6 |
Correct |
30 ms |
31056 KB |
Output is correct |
7 |
Correct |
31 ms |
31056 KB |
Output is correct |
8 |
Correct |
32 ms |
31056 KB |
Output is correct |
9 |
Correct |
30 ms |
31056 KB |
Output is correct |
10 |
Correct |
33 ms |
31056 KB |
Output is correct |
11 |
Correct |
32 ms |
31056 KB |
Output is correct |
12 |
Correct |
29 ms |
31056 KB |
Output is correct |
13 |
Correct |
31 ms |
31056 KB |
Output is correct |
14 |
Correct |
29 ms |
31056 KB |
Output is correct |
15 |
Correct |
29 ms |
31056 KB |
Output is correct |
16 |
Correct |
36 ms |
31056 KB |
Output is correct |
17 |
Correct |
30 ms |
31056 KB |
Output is correct |
18 |
Correct |
32 ms |
31056 KB |
Output is correct |
19 |
Correct |
35 ms |
31056 KB |
Output is correct |
20 |
Correct |
29 ms |
31056 KB |
Output is correct |
21 |
Correct |
34 ms |
31056 KB |
Output is correct |
22 |
Correct |
35 ms |
31056 KB |
Output is correct |
23 |
Correct |
34 ms |
31056 KB |
Output is correct |
24 |
Correct |
33 ms |
31084 KB |
Output is correct |
25 |
Correct |
35 ms |
31084 KB |
Output is correct |
26 |
Correct |
34 ms |
31084 KB |
Output is correct |
27 |
Correct |
54 ms |
33672 KB |
Output is correct |
28 |
Correct |
43 ms |
33672 KB |
Output is correct |
29 |
Correct |
54 ms |
33672 KB |
Output is correct |
30 |
Correct |
41 ms |
33672 KB |
Output is correct |
31 |
Correct |
43 ms |
33672 KB |
Output is correct |
32 |
Correct |
50 ms |
33672 KB |
Output is correct |
33 |
Correct |
47 ms |
33672 KB |
Output is correct |
34 |
Correct |
48 ms |
33672 KB |
Output is correct |
35 |
Correct |
103 ms |
33672 KB |
Output is correct |
36 |
Correct |
88 ms |
34064 KB |
Output is correct |
37 |
Correct |
94 ms |
34064 KB |
Output is correct |
38 |
Correct |
81 ms |
34548 KB |
Output is correct |
39 |
Correct |
93 ms |
34952 KB |
Output is correct |
40 |
Correct |
65 ms |
35732 KB |
Output is correct |
41 |
Correct |
59 ms |
36156 KB |
Output is correct |
42 |
Correct |
53 ms |
36508 KB |
Output is correct |
43 |
Correct |
66 ms |
36888 KB |
Output is correct |
44 |
Correct |
58 ms |
37232 KB |
Output is correct |
45 |
Correct |
62 ms |
37652 KB |
Output is correct |
46 |
Correct |
73 ms |
37996 KB |
Output is correct |
47 |
Correct |
56 ms |
38364 KB |
Output is correct |
48 |
Correct |
632 ms |
66624 KB |
Output is correct |
49 |
Correct |
642 ms |
71116 KB |
Output is correct |
50 |
Correct |
641 ms |
71704 KB |
Output is correct |
51 |
Correct |
690 ms |
75860 KB |
Output is correct |
52 |
Correct |
586 ms |
78572 KB |
Output is correct |
53 |
Correct |
379 ms |
87328 KB |
Output is correct |
54 |
Correct |
282 ms |
90844 KB |
Output is correct |
55 |
Correct |
267 ms |
95240 KB |
Output is correct |
56 |
Correct |
284 ms |
99480 KB |
Output is correct |
57 |
Correct |
2302 ms |
148772 KB |
Output is correct |
58 |
Correct |
1893 ms |
148772 KB |
Output is correct |
59 |
Correct |
2019 ms |
148772 KB |
Output is correct |
60 |
Correct |
2056 ms |
154684 KB |
Output is correct |
61 |
Correct |
2288 ms |
161388 KB |
Output is correct |
62 |
Correct |
922 ms |
161388 KB |
Output is correct |
63 |
Correct |
1412 ms |
164112 KB |
Output is correct |
64 |
Correct |
1020 ms |
167352 KB |
Output is correct |
65 |
Correct |
610 ms |
167352 KB |
Output is correct |