Submission #20930

#TimeUsernameProblemLanguageResultExecution timeMemory
20930gs14004Rope (JOI17_rope)C++11
100 / 100
1546 ms88796 KiB
#include <bits/stdc++.h>
typedef long long lint;
typedef long double llf;
using namespace std;
typedef pair<int, int> pi;

int n, m, a[1000005];
int ans[1000005], occ[1000005], sum;
vector<int> pnt[1000005];

void get_table(int s, int e){
	sum = 0;
	memset(occ, 0, sizeof(occ));
	for(int i=1; i<=n; i++) pnt[i].clear();
	for(int i=s+1; i<=e; i+=2){
		if(a[i-1] == a[i]){
			sum += 2;
			occ[a[i]] += 2;
			pnt[a[i]].push_back(a[i]);
			pnt[a[i]].push_back(a[i]);
		}
		else{
			int x = a[i-1], y = a[i];
			sum += 2;
			occ[x]++;
			occ[y]++;
			pnt[x].push_back(x);
			pnt[x].push_back(y);
			pnt[y].push_back(x);
			pnt[y].push_back(y);
		}
	}
}

int chk[1000005];

void sweep(){
	vector<pi> v;
	for(int i=1; i<=n; i++) v.push_back(pi(occ[i], i));
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
	for(int i=1; i<=m; i++){
		int cur = -1e9;
		for(auto &j : pnt[i]) chk[j]++;
		for(auto &j : v){
			cur = max(cur, j.first - chk[j.second]);
			if(!chk[j.second]) break;
		}
		for(auto &j : pnt[i]) chk[j]--;
		ans[i] = min(ans[i], sum - occ[i] - cur);
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=1; i<=n; i++){
		scanf("%d",&a[i]);
	}
	memset(ans, 0x3f, sizeof(ans));
	if(n % 2 == 0){
		get_table(1, n);
		sweep();
		get_table(2, n-1);
		sum += 2;
		occ[a[1]]++;
		occ[a[n]]++;
		pnt[a[1]].push_back(a[1]);
		pnt[a[n]].push_back(a[n]);
		sweep();
	}
	else{
		get_table(1, n-1);
		sum++;
		occ[a[n]]++;
		pnt[a[n]].push_back(a[n]);
		sweep();
		get_table(2, n);
		sum++;
		occ[a[1]]++;
		pnt[a[1]].push_back(a[1]);
		sweep();
	}
	for(int i=1; i<=m; i++) printf("%d\n", ans[i]);
}

Compilation message (stderr)

rope.cpp: In function 'int main()':
rope.cpp:55:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
rope.cpp:57:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&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...