답안 #709457

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709457 2023-03-13T15:58:36 Z myrcella Rope (JOI17_rope) C++17
100 / 100
2362 ms 97368 KB
//by szh
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}

const int maxn = 1000010;

int n,m;
int c[maxn];
int cnt[maxn];
int ans[maxn];
int tmp[maxn],tmpp[maxn];

vector <int> v[maxn];

int tree[maxn*4];

void update(int c,int cl,int cr,int pos,int val) {
	if (cl==cr) tree[c]+=val;
	else {
		int mid=cl+cr>>1;
		if (pos<=mid) update(c<<1,cl,mid,pos,val);
		else update(c<<1|1,mid+1,cr,pos,val);
		tree[c] = max(tree[c<<1],tree[c<<1|1]);
	}
}

int query(int c,int cl,int cr,int l,int r) {
	if (l>r) return 0;
	if (l<=cl and cr<=r) return tree[c];
	int mid=cl+cr>>1;
	int ret = 0;
	if (l<=mid) ret = query(c<<1,cl,mid,l,r);
	if (r>mid) ret = max(ret,query(c<<1|1,mid+1,cr,l,r));
	return ret;
}

void solve(int id) {
	memset(tmp,0,sizeof(tmp));
	memset(tmpp,0,sizeof(tmpp));
	memset(tree,0,sizeof(tree));
	if (id==1) tmpp[c[0]]++;
	for (int i=id;i<n;i+=2) {
		if (i+1<n) tmpp[c[i]]+=2;
		else tmpp[c[i]]++;
		if (i+1<n and c[i]!=c[i+1]) {
			tmp[c[i]]++;
			tmp[c[i+1]]++;
			tmpp[c[i+1]]+=2;
			v[c[i]].pb(c[i+1]);
			v[c[i+1]].pb(c[i]);
		}
	}
	rep(i,1,m+1) update(1,1,m,i,cnt[i]);
	rep(i,1,m+1) {
		for (auto it:v[i]) update(1,1,m,it,-1);
		ans[i] = min(ans[i],n-cnt[i]);
		ans[i] = min(ans[i],(n-tmpp[i])-max(query(1,1,m,1,i-1),query(1,1,m,i+1,m))+tmp[i]);
	//	cout<<i<<" "<<query(1,1,m,i+1,m)<<endl;
		while (!v[i].empty()) update(1,1,m,v[i].back(),1),v[i].pop_back();
	}
}

int main() {
//	freopen("input.txt","r",stdin);	
	std::ios::sync_with_stdio(false);cin.tie(0);
	cin>>n>>m;
	rep(i,1,m+1) ans[i] = n;
	rep(i,0,n) {
		cin>>c[i];
		cnt[c[i]]++;
	}
	solve(0);
	solve(1);
	rep(i,0,m) cout<<ans[i+1]<<"\n";
	return 0;
}

Compilation message

rope.cpp: In function 'void update(int, int, int, int, int)':
rope.cpp:39:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   39 |   int mid=cl+cr>>1;
      |           ~~^~~
rope.cpp: In function 'int query(int, int, int, int, int)':
rope.cpp:49:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |  int mid=cl+cr>>1;
      |          ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 23 ms 47292 KB Output is correct
3 Correct 23 ms 47188 KB Output is correct
4 Correct 22 ms 47232 KB Output is correct
5 Correct 22 ms 47256 KB Output is correct
6 Correct 25 ms 47304 KB Output is correct
7 Correct 23 ms 47188 KB Output is correct
8 Correct 27 ms 47308 KB Output is correct
9 Correct 23 ms 47316 KB Output is correct
10 Correct 24 ms 47188 KB Output is correct
11 Correct 23 ms 47200 KB Output is correct
12 Correct 22 ms 47188 KB Output is correct
13 Correct 25 ms 47224 KB Output is correct
14 Correct 24 ms 47308 KB Output is correct
15 Correct 24 ms 47272 KB Output is correct
16 Correct 22 ms 47268 KB Output is correct
17 Correct 23 ms 47308 KB Output is correct
18 Correct 21 ms 47316 KB Output is correct
19 Correct 22 ms 47208 KB Output is correct
20 Correct 22 ms 47188 KB Output is correct
21 Correct 21 ms 47192 KB Output is correct
22 Correct 23 ms 47188 KB Output is correct
23 Correct 26 ms 47296 KB Output is correct
24 Correct 23 ms 47312 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 24 ms 47188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 23 ms 47292 KB Output is correct
3 Correct 23 ms 47188 KB Output is correct
4 Correct 22 ms 47232 KB Output is correct
5 Correct 22 ms 47256 KB Output is correct
6 Correct 25 ms 47304 KB Output is correct
7 Correct 23 ms 47188 KB Output is correct
8 Correct 27 ms 47308 KB Output is correct
9 Correct 23 ms 47316 KB Output is correct
10 Correct 24 ms 47188 KB Output is correct
11 Correct 23 ms 47200 KB Output is correct
12 Correct 22 ms 47188 KB Output is correct
13 Correct 25 ms 47224 KB Output is correct
14 Correct 24 ms 47308 KB Output is correct
15 Correct 24 ms 47272 KB Output is correct
16 Correct 22 ms 47268 KB Output is correct
17 Correct 23 ms 47308 KB Output is correct
18 Correct 21 ms 47316 KB Output is correct
19 Correct 22 ms 47208 KB Output is correct
20 Correct 22 ms 47188 KB Output is correct
21 Correct 21 ms 47192 KB Output is correct
22 Correct 23 ms 47188 KB Output is correct
23 Correct 26 ms 47296 KB Output is correct
24 Correct 23 ms 47312 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 24 ms 47188 KB Output is correct
27 Correct 37 ms 48264 KB Output is correct
28 Correct 35 ms 48256 KB Output is correct
29 Correct 38 ms 48276 KB Output is correct
30 Correct 40 ms 48084 KB Output is correct
31 Correct 39 ms 48260 KB Output is correct
32 Correct 34 ms 48140 KB Output is correct
33 Correct 37 ms 48168 KB Output is correct
34 Correct 42 ms 48084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 23 ms 47292 KB Output is correct
3 Correct 23 ms 47188 KB Output is correct
4 Correct 22 ms 47232 KB Output is correct
5 Correct 22 ms 47256 KB Output is correct
6 Correct 25 ms 47304 KB Output is correct
7 Correct 23 ms 47188 KB Output is correct
8 Correct 27 ms 47308 KB Output is correct
9 Correct 23 ms 47316 KB Output is correct
10 Correct 24 ms 47188 KB Output is correct
11 Correct 23 ms 47200 KB Output is correct
12 Correct 22 ms 47188 KB Output is correct
13 Correct 25 ms 47224 KB Output is correct
14 Correct 24 ms 47308 KB Output is correct
15 Correct 24 ms 47272 KB Output is correct
16 Correct 22 ms 47268 KB Output is correct
17 Correct 23 ms 47308 KB Output is correct
18 Correct 21 ms 47316 KB Output is correct
19 Correct 22 ms 47208 KB Output is correct
20 Correct 22 ms 47188 KB Output is correct
21 Correct 21 ms 47192 KB Output is correct
22 Correct 23 ms 47188 KB Output is correct
23 Correct 26 ms 47296 KB Output is correct
24 Correct 23 ms 47312 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 24 ms 47188 KB Output is correct
27 Correct 37 ms 48264 KB Output is correct
28 Correct 35 ms 48256 KB Output is correct
29 Correct 38 ms 48276 KB Output is correct
30 Correct 40 ms 48084 KB Output is correct
31 Correct 39 ms 48260 KB Output is correct
32 Correct 34 ms 48140 KB Output is correct
33 Correct 37 ms 48168 KB Output is correct
34 Correct 42 ms 48084 KB Output is correct
35 Correct 61 ms 48280 KB Output is correct
36 Correct 65 ms 48292 KB Output is correct
37 Correct 62 ms 48384 KB Output is correct
38 Correct 66 ms 48268 KB Output is correct
39 Correct 66 ms 48212 KB Output is correct
40 Correct 54 ms 48328 KB Output is correct
41 Correct 57 ms 48300 KB Output is correct
42 Correct 53 ms 48340 KB Output is correct
43 Correct 52 ms 48316 KB Output is correct
44 Correct 53 ms 48352 KB Output is correct
45 Correct 53 ms 48348 KB Output is correct
46 Correct 51 ms 48340 KB Output is correct
47 Correct 52 ms 48364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 23 ms 47292 KB Output is correct
3 Correct 23 ms 47188 KB Output is correct
4 Correct 22 ms 47232 KB Output is correct
5 Correct 22 ms 47256 KB Output is correct
6 Correct 25 ms 47304 KB Output is correct
7 Correct 23 ms 47188 KB Output is correct
8 Correct 27 ms 47308 KB Output is correct
9 Correct 23 ms 47316 KB Output is correct
10 Correct 24 ms 47188 KB Output is correct
11 Correct 23 ms 47200 KB Output is correct
12 Correct 22 ms 47188 KB Output is correct
13 Correct 25 ms 47224 KB Output is correct
14 Correct 24 ms 47308 KB Output is correct
15 Correct 24 ms 47272 KB Output is correct
16 Correct 22 ms 47268 KB Output is correct
17 Correct 23 ms 47308 KB Output is correct
18 Correct 21 ms 47316 KB Output is correct
19 Correct 22 ms 47208 KB Output is correct
20 Correct 22 ms 47188 KB Output is correct
21 Correct 21 ms 47192 KB Output is correct
22 Correct 23 ms 47188 KB Output is correct
23 Correct 26 ms 47296 KB Output is correct
24 Correct 23 ms 47312 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 24 ms 47188 KB Output is correct
27 Correct 37 ms 48264 KB Output is correct
28 Correct 35 ms 48256 KB Output is correct
29 Correct 38 ms 48276 KB Output is correct
30 Correct 40 ms 48084 KB Output is correct
31 Correct 39 ms 48260 KB Output is correct
32 Correct 34 ms 48140 KB Output is correct
33 Correct 37 ms 48168 KB Output is correct
34 Correct 42 ms 48084 KB Output is correct
35 Correct 61 ms 48280 KB Output is correct
36 Correct 65 ms 48292 KB Output is correct
37 Correct 62 ms 48384 KB Output is correct
38 Correct 66 ms 48268 KB Output is correct
39 Correct 66 ms 48212 KB Output is correct
40 Correct 54 ms 48328 KB Output is correct
41 Correct 57 ms 48300 KB Output is correct
42 Correct 53 ms 48340 KB Output is correct
43 Correct 52 ms 48316 KB Output is correct
44 Correct 53 ms 48352 KB Output is correct
45 Correct 53 ms 48348 KB Output is correct
46 Correct 51 ms 48340 KB Output is correct
47 Correct 52 ms 48364 KB Output is correct
48 Correct 510 ms 57892 KB Output is correct
49 Correct 508 ms 57876 KB Output is correct
50 Correct 523 ms 57916 KB Output is correct
51 Correct 483 ms 57868 KB Output is correct
52 Correct 447 ms 56944 KB Output is correct
53 Correct 510 ms 57100 KB Output is correct
54 Correct 453 ms 56168 KB Output is correct
55 Correct 456 ms 55932 KB Output is correct
56 Correct 451 ms 55596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 23 ms 47292 KB Output is correct
3 Correct 23 ms 47188 KB Output is correct
4 Correct 22 ms 47232 KB Output is correct
5 Correct 22 ms 47256 KB Output is correct
6 Correct 25 ms 47304 KB Output is correct
7 Correct 23 ms 47188 KB Output is correct
8 Correct 27 ms 47308 KB Output is correct
9 Correct 23 ms 47316 KB Output is correct
10 Correct 24 ms 47188 KB Output is correct
11 Correct 23 ms 47200 KB Output is correct
12 Correct 22 ms 47188 KB Output is correct
13 Correct 25 ms 47224 KB Output is correct
14 Correct 24 ms 47308 KB Output is correct
15 Correct 24 ms 47272 KB Output is correct
16 Correct 22 ms 47268 KB Output is correct
17 Correct 23 ms 47308 KB Output is correct
18 Correct 21 ms 47316 KB Output is correct
19 Correct 22 ms 47208 KB Output is correct
20 Correct 22 ms 47188 KB Output is correct
21 Correct 21 ms 47192 KB Output is correct
22 Correct 23 ms 47188 KB Output is correct
23 Correct 26 ms 47296 KB Output is correct
24 Correct 23 ms 47312 KB Output is correct
25 Correct 22 ms 47316 KB Output is correct
26 Correct 24 ms 47188 KB Output is correct
27 Correct 37 ms 48264 KB Output is correct
28 Correct 35 ms 48256 KB Output is correct
29 Correct 38 ms 48276 KB Output is correct
30 Correct 40 ms 48084 KB Output is correct
31 Correct 39 ms 48260 KB Output is correct
32 Correct 34 ms 48140 KB Output is correct
33 Correct 37 ms 48168 KB Output is correct
34 Correct 42 ms 48084 KB Output is correct
35 Correct 61 ms 48280 KB Output is correct
36 Correct 65 ms 48292 KB Output is correct
37 Correct 62 ms 48384 KB Output is correct
38 Correct 66 ms 48268 KB Output is correct
39 Correct 66 ms 48212 KB Output is correct
40 Correct 54 ms 48328 KB Output is correct
41 Correct 57 ms 48300 KB Output is correct
42 Correct 53 ms 48340 KB Output is correct
43 Correct 52 ms 48316 KB Output is correct
44 Correct 53 ms 48352 KB Output is correct
45 Correct 53 ms 48348 KB Output is correct
46 Correct 51 ms 48340 KB Output is correct
47 Correct 52 ms 48364 KB Output is correct
48 Correct 510 ms 57892 KB Output is correct
49 Correct 508 ms 57876 KB Output is correct
50 Correct 523 ms 57916 KB Output is correct
51 Correct 483 ms 57868 KB Output is correct
52 Correct 447 ms 56944 KB Output is correct
53 Correct 510 ms 57100 KB Output is correct
54 Correct 453 ms 56168 KB Output is correct
55 Correct 456 ms 55932 KB Output is correct
56 Correct 451 ms 55596 KB Output is correct
57 Correct 2362 ms 97368 KB Output is correct
58 Correct 1811 ms 85500 KB Output is correct
59 Correct 1983 ms 85424 KB Output is correct
60 Correct 2032 ms 90260 KB Output is correct
61 Correct 2009 ms 90168 KB Output is correct
62 Correct 1074 ms 74600 KB Output is correct
63 Correct 1409 ms 79788 KB Output is correct
64 Correct 1246 ms 76528 KB Output is correct
65 Correct 826 ms 68148 KB Output is correct