Submission #163417

#TimeUsernameProblemLanguageResultExecution timeMemory
163417iefnah06Rope (JOI17_rope)C++11
100 / 100
1157 ms80760 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6 + 10;
const int MAXM = MAXN;
int N, M;
int A[MAXN];
vector<int> v[MAXM];

int cost[MAXM];
int ind[MAXM];

int main() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < N; i++) {
		scanf("%d", &A[i]); A[i]--;
		v[A[i]].push_back(i);
		cost[A[i]] -= 1;
	}
	iota(ind, ind + M, 0);
	sort(ind, ind + M, [&](int a, int b) { return cost[a] < cost[b]; });

	for (int c = 0; c < M; c++) {
		int ans = N - int(v[c].size());

		for (int par = 0; par < 2; par++) {
			// at most 2 * |v[c]| incident colors
			int cnt = 0;
			for (int i : v[c]) {
				if (i > 0 && i % 2 != par) {
					cost[A[i - 1]] += 1;
					cnt++;
				}
				if (i < N - 1 && (i + 1) % 2 != par) {
					cost[A[i + 1]] += 1;
					cnt++;
				}
			}

			cnt = min(cnt + 2, M);
			int extra = N;
			for (int i = 0; i < cnt; i++) {
				if (ind[i] == c) continue;
				extra = min(extra, cost[ind[i]]);
			}
			ans = min(ans, N - int(v[c].size()) + extra);

			for (int i : v[c]) {
				if (i > 0 && i % 2 != par) {
					cost[A[i - 1]]--;
				}
				if (i < N - 1 && (i + 1) % 2 != par) {
					cost[A[i + 1]]--;
				}
			}
		}

		printf("%d\n", ans);
	}

	return 0;
}

Compilation message (stderr)

rope.cpp: In function 'int main()':
rope.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~~
rope.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]); A[i]--;
   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...