답안 #141233

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
141233 2019-08-07T07:17:58 Z DrumpfTheGodEmperor Rope (JOI17_rope) C++14
100 / 100
2483 ms 225468 KB
#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
typedef pair<int, int> ii;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 1e6 + 5;
int n, m, c[N], ans[N], cnt[N], cntRMQ[N][20], logy[N], totalRemovals[N], appSize[N];
vector<ii> v;
vector<int> app[N], samePair[N];
int getMax(int l, int r) {
	if (l > r) return 0;
	int tmp = logy[r - l + 1];
	return max(cntRMQ[l][tmp], cntRMQ[r - (1 << tmp) + 1][tmp]);
}
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	fw (i, 0, n) cin >> c[i];
	if (m == 1) {
		cout << "0";
		return 0;
	}
	//Pre - color the rope then fold. Folding can be done if segments of the same color, except the first
	//and last one are all of even length.
	memset(ans, 63, sizeof ans);
	memset(cnt, 0, sizeof cnt);
	fw (i, 0, n) cnt[c[i]]++;
	//RMQ initialization
	fw (i, 0, N) logy[i] = log2(i);
	fw (i, 1, m + 1) cntRMQ[i][0] = cnt[i];
//	fw (i, 1, m + 1) cout << cnt[i] << " "; cout << "\n";
	fw (j, 1, 20) fw (i, 1, m + 1) if (i + (1 << (j - 1)) < n) {
		cntRMQ[i][j] = max(cntRMQ[i][j - 1], cntRMQ[i + (1 << (j - 1))][j - 1]);
	}
	fw (i, 0, 2) {
		bool lastExcluded = 0;
		if (i == 0 && (n & 1)) lastExcluded = 1;
		if (i == 1 && !(n & 1)) lastExcluded = 1;
		//Fix the first segment one as either odd or even.
		fw (color, 1, m + 1) app[color].clear(), samePair[color].clear();
		v.clear();
		for (int j = i; j < n; j += 2) {
			if (j + 1 >= n) break;
			v.pb(ii(c[j], c[j + 1]));
		}
		fw (j, 1, m + 1) app[j].reserve(cnt[j]);
		fw (j, 0, v.size()) {
			app[v[j].fi].pb(j);
			if (v[j].se != v[j].fi) app[v[j].se].pb(j);
		}
		fw (color, 1, m + 1) {
			samePair[color].reserve(1 + app[color].size());
			samePair[color].pb(color);
			fa (pairID, app[color]) {
				//Add color to other color's samePair vector.
				if (v[pairID].fi != color) samePair[v[pairID].fi].pb(color);
				if (v[pairID].se != color) samePair[v[pairID].se].pb(color);
			}
		}
		fw (color, 1, m + 1) {
//			samePair[color].resize(unique(samePair[color].begin(), samePair[color].end()) - samePair[color].begin());
			int totalCost = 0, cntExcluded = 0;
			//Find element with max cnt excluding all the pairs that color j appears in
			if (i == 1 && c[0] == color) {
				totalRemovals[c[0]]++;
				cntExcluded++;
			}
			if (lastExcluded && c[n - 1] == color) {
				totalRemovals[c[n - 1]]++;
				cntExcluded++;
			}
			fa (pairID, app[color]) {
				int cost = 0;
				if (v[pairID].fi != color || v[pairID].se != color) cost = 1;
				totalCost += cost;
				
				totalRemovals[v[pairID].fi]++;
				totalRemovals[v[pairID].se]++;
				
				cntExcluded += 2;
			}
			//Calculating number that appears the most
			int mxAppearances = 0;
			fw (j, 0, samePair[color].size() + 1) {
				if (j < samePair[color].size()) {
					int curColor = samePair[color][j];
//					cout << "curColor = " << curColor << " totalRemovals = " << totalRemovals[curColor] << "\n";
					if (j == 0 || curColor != samePair[color][j - 1]) {
						mxAppearances = max(mxAppearances, cnt[curColor] - totalRemovals[curColor]);
						totalRemovals[curColor] = 0; //Reset total removals.
					}
				}
				int prvColor = j ? samePair[color][j - 1] : 0;
				int curColor = j < samePair[color].size() ? samePair[color][j] : m + 1;
				prvColor++;
				curColor--;
				mxAppearances = max(mxAppearances, getMax(prvColor, curColor));
			}
			//cntIncluded counts the number of cords included in group 2.
			int cntIncluded = n - cntExcluded;
			totalCost += cntIncluded - mxAppearances;
//			cout << "mxApperances = " << mxAppearances << " cntIncluded = " << cntIncluded << "\n";
			ans[color] = min(ans[color], totalCost);
//			cout << "i = " << i << " ans[" << color << "] = " << totalCost << "\n";
		}
	}
	fw (color, 1, m + 1) cout << ans[color] << "\n";
	return 0;
}

Compilation message

rope.cpp: In function 'int main()':
rope.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
rope.cpp:61:7:
   fw (j, 0, v.size()) {
       ~~~~~~~~~~~~~~                   
rope.cpp:61:3: note: in expansion of macro 'fw'
   fw (j, 0, v.size()) {
   ^~
rope.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
rope.cpp:98:8:
    fw (j, 0, samePair[color].size() + 1) {
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rope.cpp:98:4: note: in expansion of macro 'fw'
    fw (j, 0, samePair[color].size() + 1) {
    ^~
rope.cpp:99:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (j < samePair[color].size()) {
         ~~^~~~~~~~~~~~~~~~~~~~~~~~
rope.cpp:108:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     int curColor = j < samePair[color].size() ? samePair[color][j] : m + 1;
                    ~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 59080 KB Output is correct
2 Correct 72 ms 59000 KB Output is correct
3 Correct 71 ms 59032 KB Output is correct
4 Correct 71 ms 59100 KB Output is correct
5 Correct 71 ms 59116 KB Output is correct
6 Correct 73 ms 59136 KB Output is correct
7 Correct 71 ms 59040 KB Output is correct
8 Correct 71 ms 59096 KB Output is correct
9 Correct 71 ms 59060 KB Output is correct
10 Correct 72 ms 59100 KB Output is correct
11 Correct 71 ms 59000 KB Output is correct
12 Correct 71 ms 59096 KB Output is correct
13 Correct 71 ms 59000 KB Output is correct
14 Correct 72 ms 59064 KB Output is correct
15 Correct 71 ms 59096 KB Output is correct
16 Correct 71 ms 59004 KB Output is correct
17 Correct 71 ms 59128 KB Output is correct
18 Correct 70 ms 59004 KB Output is correct
19 Correct 71 ms 59000 KB Output is correct
20 Correct 70 ms 59112 KB Output is correct
21 Correct 71 ms 59128 KB Output is correct
22 Correct 46 ms 47268 KB Output is correct
23 Correct 71 ms 59052 KB Output is correct
24 Correct 72 ms 59060 KB Output is correct
25 Correct 74 ms 59020 KB Output is correct
26 Correct 45 ms 47348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 59080 KB Output is correct
2 Correct 72 ms 59000 KB Output is correct
3 Correct 71 ms 59032 KB Output is correct
4 Correct 71 ms 59100 KB Output is correct
5 Correct 71 ms 59116 KB Output is correct
6 Correct 73 ms 59136 KB Output is correct
7 Correct 71 ms 59040 KB Output is correct
8 Correct 71 ms 59096 KB Output is correct
9 Correct 71 ms 59060 KB Output is correct
10 Correct 72 ms 59100 KB Output is correct
11 Correct 71 ms 59000 KB Output is correct
12 Correct 71 ms 59096 KB Output is correct
13 Correct 71 ms 59000 KB Output is correct
14 Correct 72 ms 59064 KB Output is correct
15 Correct 71 ms 59096 KB Output is correct
16 Correct 71 ms 59004 KB Output is correct
17 Correct 71 ms 59128 KB Output is correct
18 Correct 70 ms 59004 KB Output is correct
19 Correct 71 ms 59000 KB Output is correct
20 Correct 70 ms 59112 KB Output is correct
21 Correct 71 ms 59128 KB Output is correct
22 Correct 46 ms 47268 KB Output is correct
23 Correct 71 ms 59052 KB Output is correct
24 Correct 72 ms 59060 KB Output is correct
25 Correct 74 ms 59020 KB Output is correct
26 Correct 45 ms 47348 KB Output is correct
27 Correct 94 ms 60924 KB Output is correct
28 Correct 90 ms 61060 KB Output is correct
29 Correct 90 ms 61012 KB Output is correct
30 Correct 88 ms 60984 KB Output is correct
31 Correct 88 ms 61016 KB Output is correct
32 Correct 88 ms 60896 KB Output is correct
33 Correct 87 ms 60916 KB Output is correct
34 Correct 87 ms 61052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 59080 KB Output is correct
2 Correct 72 ms 59000 KB Output is correct
3 Correct 71 ms 59032 KB Output is correct
4 Correct 71 ms 59100 KB Output is correct
5 Correct 71 ms 59116 KB Output is correct
6 Correct 73 ms 59136 KB Output is correct
7 Correct 71 ms 59040 KB Output is correct
8 Correct 71 ms 59096 KB Output is correct
9 Correct 71 ms 59060 KB Output is correct
10 Correct 72 ms 59100 KB Output is correct
11 Correct 71 ms 59000 KB Output is correct
12 Correct 71 ms 59096 KB Output is correct
13 Correct 71 ms 59000 KB Output is correct
14 Correct 72 ms 59064 KB Output is correct
15 Correct 71 ms 59096 KB Output is correct
16 Correct 71 ms 59004 KB Output is correct
17 Correct 71 ms 59128 KB Output is correct
18 Correct 70 ms 59004 KB Output is correct
19 Correct 71 ms 59000 KB Output is correct
20 Correct 70 ms 59112 KB Output is correct
21 Correct 71 ms 59128 KB Output is correct
22 Correct 46 ms 47268 KB Output is correct
23 Correct 71 ms 59052 KB Output is correct
24 Correct 72 ms 59060 KB Output is correct
25 Correct 74 ms 59020 KB Output is correct
26 Correct 45 ms 47348 KB Output is correct
27 Correct 94 ms 60924 KB Output is correct
28 Correct 90 ms 61060 KB Output is correct
29 Correct 90 ms 61012 KB Output is correct
30 Correct 88 ms 60984 KB Output is correct
31 Correct 88 ms 61016 KB Output is correct
32 Correct 88 ms 60896 KB Output is correct
33 Correct 87 ms 60916 KB Output is correct
34 Correct 87 ms 61052 KB Output is correct
35 Correct 98 ms 61236 KB Output is correct
36 Correct 97 ms 61184 KB Output is correct
37 Correct 97 ms 61144 KB Output is correct
38 Correct 99 ms 61172 KB Output is correct
39 Correct 101 ms 61236 KB Output is correct
40 Correct 94 ms 61232 KB Output is correct
41 Correct 94 ms 61184 KB Output is correct
42 Correct 94 ms 61220 KB Output is correct
43 Correct 94 ms 61096 KB Output is correct
44 Correct 94 ms 61228 KB Output is correct
45 Correct 106 ms 61260 KB Output is correct
46 Correct 93 ms 61212 KB Output is correct
47 Correct 93 ms 61164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 59080 KB Output is correct
2 Correct 72 ms 59000 KB Output is correct
3 Correct 71 ms 59032 KB Output is correct
4 Correct 71 ms 59100 KB Output is correct
5 Correct 71 ms 59116 KB Output is correct
6 Correct 73 ms 59136 KB Output is correct
7 Correct 71 ms 59040 KB Output is correct
8 Correct 71 ms 59096 KB Output is correct
9 Correct 71 ms 59060 KB Output is correct
10 Correct 72 ms 59100 KB Output is correct
11 Correct 71 ms 59000 KB Output is correct
12 Correct 71 ms 59096 KB Output is correct
13 Correct 71 ms 59000 KB Output is correct
14 Correct 72 ms 59064 KB Output is correct
15 Correct 71 ms 59096 KB Output is correct
16 Correct 71 ms 59004 KB Output is correct
17 Correct 71 ms 59128 KB Output is correct
18 Correct 70 ms 59004 KB Output is correct
19 Correct 71 ms 59000 KB Output is correct
20 Correct 70 ms 59112 KB Output is correct
21 Correct 71 ms 59128 KB Output is correct
22 Correct 46 ms 47268 KB Output is correct
23 Correct 71 ms 59052 KB Output is correct
24 Correct 72 ms 59060 KB Output is correct
25 Correct 74 ms 59020 KB Output is correct
26 Correct 45 ms 47348 KB Output is correct
27 Correct 94 ms 60924 KB Output is correct
28 Correct 90 ms 61060 KB Output is correct
29 Correct 90 ms 61012 KB Output is correct
30 Correct 88 ms 60984 KB Output is correct
31 Correct 88 ms 61016 KB Output is correct
32 Correct 88 ms 60896 KB Output is correct
33 Correct 87 ms 60916 KB Output is correct
34 Correct 87 ms 61052 KB Output is correct
35 Correct 98 ms 61236 KB Output is correct
36 Correct 97 ms 61184 KB Output is correct
37 Correct 97 ms 61144 KB Output is correct
38 Correct 99 ms 61172 KB Output is correct
39 Correct 101 ms 61236 KB Output is correct
40 Correct 94 ms 61232 KB Output is correct
41 Correct 94 ms 61184 KB Output is correct
42 Correct 94 ms 61220 KB Output is correct
43 Correct 94 ms 61096 KB Output is correct
44 Correct 94 ms 61228 KB Output is correct
45 Correct 106 ms 61260 KB Output is correct
46 Correct 93 ms 61212 KB Output is correct
47 Correct 93 ms 61164 KB Output is correct
48 Correct 485 ms 80788 KB Output is correct
49 Correct 529 ms 80808 KB Output is correct
50 Correct 516 ms 80876 KB Output is correct
51 Correct 470 ms 80220 KB Output is correct
52 Correct 470 ms 79288 KB Output is correct
53 Correct 423 ms 80268 KB Output is correct
54 Correct 424 ms 80096 KB Output is correct
55 Correct 403 ms 80060 KB Output is correct
56 Correct 393 ms 79980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 59080 KB Output is correct
2 Correct 72 ms 59000 KB Output is correct
3 Correct 71 ms 59032 KB Output is correct
4 Correct 71 ms 59100 KB Output is correct
5 Correct 71 ms 59116 KB Output is correct
6 Correct 73 ms 59136 KB Output is correct
7 Correct 71 ms 59040 KB Output is correct
8 Correct 71 ms 59096 KB Output is correct
9 Correct 71 ms 59060 KB Output is correct
10 Correct 72 ms 59100 KB Output is correct
11 Correct 71 ms 59000 KB Output is correct
12 Correct 71 ms 59096 KB Output is correct
13 Correct 71 ms 59000 KB Output is correct
14 Correct 72 ms 59064 KB Output is correct
15 Correct 71 ms 59096 KB Output is correct
16 Correct 71 ms 59004 KB Output is correct
17 Correct 71 ms 59128 KB Output is correct
18 Correct 70 ms 59004 KB Output is correct
19 Correct 71 ms 59000 KB Output is correct
20 Correct 70 ms 59112 KB Output is correct
21 Correct 71 ms 59128 KB Output is correct
22 Correct 46 ms 47268 KB Output is correct
23 Correct 71 ms 59052 KB Output is correct
24 Correct 72 ms 59060 KB Output is correct
25 Correct 74 ms 59020 KB Output is correct
26 Correct 45 ms 47348 KB Output is correct
27 Correct 94 ms 60924 KB Output is correct
28 Correct 90 ms 61060 KB Output is correct
29 Correct 90 ms 61012 KB Output is correct
30 Correct 88 ms 60984 KB Output is correct
31 Correct 88 ms 61016 KB Output is correct
32 Correct 88 ms 60896 KB Output is correct
33 Correct 87 ms 60916 KB Output is correct
34 Correct 87 ms 61052 KB Output is correct
35 Correct 98 ms 61236 KB Output is correct
36 Correct 97 ms 61184 KB Output is correct
37 Correct 97 ms 61144 KB Output is correct
38 Correct 99 ms 61172 KB Output is correct
39 Correct 101 ms 61236 KB Output is correct
40 Correct 94 ms 61232 KB Output is correct
41 Correct 94 ms 61184 KB Output is correct
42 Correct 94 ms 61220 KB Output is correct
43 Correct 94 ms 61096 KB Output is correct
44 Correct 94 ms 61228 KB Output is correct
45 Correct 106 ms 61260 KB Output is correct
46 Correct 93 ms 61212 KB Output is correct
47 Correct 93 ms 61164 KB Output is correct
48 Correct 485 ms 80788 KB Output is correct
49 Correct 529 ms 80808 KB Output is correct
50 Correct 516 ms 80876 KB Output is correct
51 Correct 470 ms 80220 KB Output is correct
52 Correct 470 ms 79288 KB Output is correct
53 Correct 423 ms 80268 KB Output is correct
54 Correct 424 ms 80096 KB Output is correct
55 Correct 403 ms 80060 KB Output is correct
56 Correct 393 ms 79980 KB Output is correct
57 Correct 2483 ms 225468 KB Output is correct
58 Correct 2021 ms 164868 KB Output is correct
59 Correct 2102 ms 164368 KB Output is correct
60 Correct 2181 ms 179900 KB Output is correct
61 Correct 2175 ms 179868 KB Output is correct
62 Correct 1062 ms 123848 KB Output is correct
63 Correct 1450 ms 140812 KB Output is correct
64 Correct 1216 ms 130256 KB Output is correct
65 Correct 859 ms 103448 KB Output is correct