[“深圳计算科研院杯“E起来编程暨第三届湖北省赛] D.WA (优先队列)

21 篇文章 0 订阅
5 篇文章 0 订阅
题目链接:D.WA
题解

维护出相邻a之间的长度以及左右下标,按照长度从小到大放入优先队列,显然队首出队的永远是长度最小的,我们就将该区间内的字母全部变为a即可。

注意第一次出现a往前的区间以及最后一次出现a往后的区间没有考虑。所以如果k有剩余就考虑这两个区间,不断变a即可。

代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=5e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int n,k;
string s;
vector<int> p;

struct Rt
{
	int l,r,len;
	Rt(int _l=0,int _r=0,int _len=0)
	{
		l=_l; r=_r; len=_len;
	}
	bool operator > (const Rt &w) const{
		return len>w.len;
	}
};
priority_queue<Rt,vector<Rt>,greater<Rt> > que;

int main()
{
	int n,k,ans=0;
	cin >> n >> k;
	cin >> s;
	int flag=1,pre=0,fir,las;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='a')
		{
			las=i;
			if(flag)
			{
				pre=i;
				fir=i;
				flag=0;
				continue;
			}
			if(i-pre-1==0)
			{
				pre=i;
				continue;
			}
			Rt a;
			a=Rt(pre,i,i-pre-1);
			que.push(a);
			pre=i;
		}
	}
	
	while (!que.empty()) {
		Rt he=que.top();
		for(int i=he.l+1;i<he.r && k>0;i++) { s[i]='a'; k--; }
		if(!k) break;
		que.pop();
	}
	if(k>0)
	{
		for(int i=fir-1;i>=0 && k>0;i--) { s[i]='a'; k--; }
	}
	if(k>0)
	{
		for(int i=las+1;i<n && k>0;i++) { s[i]='a'; k--; }
	}
	for(int i=1;i<n;i++)
	{
		if(s[i]=='a' && s[i-1]=='a') ans++;
	}
	printf("%d\n",ans);
	cout << s << endl;
}

每次遇到这种模拟思维+基础数据结构的题就扔给了队友,这下好了,队友都在看其它题,我挑起了大梁,好久不打遭报应了吧,正如题名那样WA了很多次。。感觉应该很早就出的,这样还能帮队友看看B,结果我在D上耗时太多,哎,我坑了啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值