ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)

2021-04-06 02:04:40  阅读:546  来源: 互联网

标签:ac 位置 反悔 predict 45 ICPC 修改 程序设计 texttt


A-AC



时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld


题目描述

Crystal's fortune predict system is successfully developed!

The predict system is a distributed system consists of \(N\) computers. When it receives a predict request, each computer will generate one lowercase letter as its output. The final fortune predict result is determined by the concentration of all \(N\) outputs.

Tired of getting bad predictions like awful: \(\texttt{awful: millions of bugs}\), Ben decides to hack into the predict system and modify the predict result. He has already got the access permission of every computer in the predict system, so he can modify their output to any letter arbitrarily.

As Ben is going to take part in ICPC Asia Regional Kunming Site 2077, he wants predictions like \(\texttt{suitable for writing codes}\) or \(\texttt{will get accepted for every problem}\). He has found that the more times the substring \(\texttt{ac}\) occurs in the concentration of all \(N\) outputs, the luckier he will get in the contest. But as the contest is coming soon, he only has time to modify at most \(K\) outputs of the computers in the predict system.

As Ben is busy hacking into the system, could you tell him how to get the most \(\texttt{ac}\) substrings after his modification?


输入描述

The first line contains two integers \(N\) and \(K\) \((1 \leq N \leq 5\times 10^5, 0 \leq K \leq N)\).

The second line contains a string of length \(N\), denoting the origin prediction. It is guaranteed that the string consists of lowercase English letters.


输出描述

Output two lines. The first line contains a single integer, denotes the maximum number of \(\texttt{ac}\) substring Bob can get, after his modification. The second line contains the final modified predict string. If there are multiple ways that results in the maximum number of \(\texttt{ac}\) substring, print any.


示例1

输入

9 2
arakbacca

输出

3
acacbacca

题目大意

给定一个长度为\(N\)的字符串,可以修改\(K\)个字符。让字符串中的\(\texttt{ac}\)尽可能地多。输出修改后,最多的\(\texttt{ac}\)数量和 一种修改后的字符串。


题解

Problem A-AC

难度评价

Medium-hard

题解

考虑转化问题:对于每个\(k \leq \frac{n}{2}\),如果我们能求出使得原串有\(k\)个ac至少需要改变多少字符,那么就可以求解原题了。对每个\(i \in [1,n)\),我们计算\(c_i\)表示将原串第\(i\)位改成a,第\(i+1\)位改成c的代价,那么相当于是求解选择\(k\)个\(c_i\),使得相邻两个\(c_i\)不同时被选的方案中,\(\sum c_i\)的最小值。这是一个经典的问题,可以运用堆维护可反悔的贪心来解决。构造方案根据选出的位置还原即可。时间复杂度\(O(n\;log\;n)\)


思路

思路来自 三个顶俩 的代码

1.构造c[i]代表第\(i-1\)位改为a、第\(i\)位改为c所需要修改的字符数量;

2.构造小根堆,以c[i] 为第一关键字,位置\(i\)为第二关键字;

3.l[i],r[i]为当前位置相邻字符的下标,L[i],R[i]为当前实际修改的区间,即可设置边界为r[0]=1,l[n]=n-1

4.对于每次操作,取出堆顶,检查其是否已经被修改影响过(见7.)(used[now]),直到找到一个没有被修改过的位置,修改它,使 修改字符数cnt\(+\)c[i],使 ac数量ans\(+1\),直到cnt>k或者ans>n/2(修改字符数超过规定 或 所有字符都尽可能变成了 ac 的形式);

5.之后便是往小根堆中更新反悔信息;

6.used[i]为当前位置是否被修改影响过(见7.),显然对于4.的操作,每次都会使i,l[i],r[i]受到影响;然后利用差分数组的性质,设vis[i]代表 是否已经把字符串修改为 第\(i-1\)位为a、第\(i\)位为c的状态;因为当前需要更新的为反悔信息,则设tmp为新的操作的假位置,为了使其在小根堆的位置更靠后(因为反悔的代价通常很大,不如直接修改没有修改过的地方);

7.如果当前修改区间的相邻位置没有到边界,则显然L[tmp]=L[l[i]],R[tmp]=R[r[i]]c[tmp]=c[l[i]]+c[r[i]]-c[i],即如果反悔,当前位置并不会变为ac,则一定会让左右两侧变为ac,来保证ac的数量得以增加;

举例:xacx反悔后为acac,这里如果对位置2(开始位置为0)进行反悔,则位置1和位置3都将受到影响(因为必须保证每次 从小根堆取出的位置 能进行操作 ,能让ans\(+1\);

8.随后将 相邻的 相邻的 位置 与 当前操作的位置的l[tmp],r[tmp]都进行一下连接,这样,一个反悔操作就可以更新到 小根堆 中了;

并且,让相邻的 相邻的 位置 直接 与 tmp 连接,可以防止后继操作继续更新l[i],r[i]位置,因为已经让l[i],r[i]作为反悔时更新的位置了,不能再单独更新它们了;

9.特别地,如果已经到到了边界,即当然位置没得反悔了(左右相邻的位置有一为边界),则此位置就不能反悔,直接固定,直接将边界延伸到当前范围;

10.在遍历答案时,可以发现,最多ac的数量即为ans,而任意一种字符串,可以根据差分数组vis[i],每个被修改的地方可以设为vis[L[i]]^=1,vis[R[i]+1]^=1,通过差分数组的特点,更新其前缀和,如果更新后仍有vis[i]=1,说明 第\(i-1\)位为a、第\(i\)位为c,强制修改即可;


cpp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define mp make_pair
#define fi first
#define se second
const int N=5e5+5;
using T=pair<int,int>;
priority_queue<T,vector<T>,greater<T>> q;
int n,k,tmp,ans,cnt,l[N<<1],r[N<<1],L[N<<1],R[N<<1],c[N<<1];
bool used[N<<1],vis[N];
string s;
void del(int i){
	l[r[i]]=l[i];
	r[l[i]]=r[i];
}
void orz(int i){
	used[i]=used[l[i]]=used[r[i]]=1;
	vis[L[i]]^=1;
	vis[R[i]+1]^=1;
	if(l[i]!=0 && r[i]!=n){
		tmp++;
		L[tmp]=L[l[i]];
		R[tmp]=R[r[i]];
		c[tmp]=c[l[i]]+c[r[i]]-c[i];
		del(l[i]);
		del(r[i]);
		r[l[i]]=tmp;
		l[r[i]]=tmp;
		l[tmp]=l[i];
		r[tmp]=r[i];
		q.push(mp(c[tmp],tmp));
	} else if(l[i]==0 && r[i]!=n){
		del(r[i]);
		del(i);
	} else if(l[i]!=0 && r[i]==n){
		del(l[i]);
		del(i);
	}
}
int main(){
	scanf("%d %d",&n,&k);
	cin>>s;
	for(int i=1;i<n;++i){
		c[i]=(s[i-1]!='a')+(s[i]!='c');
		l[i]=i-1; r[i]=i+1;
		L[i]=R[i]=i;
		q.push(mp(c[i],i));
	}
	r[0]=1; l[n]=n-1; tmp=n;
	while(ans<n/2){
		T now;
		do{
			now=q.top();
			q.pop();
		}while(used[now.se]);
		cnt+=now.fi;
		if(cnt>k) break;
		++ans;
		orz(now.se);
	}
	for(int i=1;i<n;++i){
		vis[i]^=vis[i-1];
		if(vis[i])
			s[i-1]='a',s[i]='c';
	}
	printf("%d\n",ans);
	cout<<s;
	return 0;
}

标签:ac,位置,反悔,predict,45,ICPC,修改,程序设计,texttt
来源: https://www.cnblogs.com/Potrem/p/2021_ICPC_Kunming.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有