跳到正文

题解

题解:CF2070C Limited Repainting

e_mei904约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年3月8日
收录
2026年11月6日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
1
题目
CF2070C

版本

  1. v1当前

思路

本题是一道二分加贪心题。我们可以先二分算总惩罚值,再慢慢一遍一遍去找,如果遇到小于等于惩罚值的量,可以跳过它,因为如果它涂错了也没啥关系。

代码实现

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N=300036;
int t,n,k,ar[N];
string s;

bool check (int x)
{
	string s1="";
	for (int i=0; i<n; i++)
	{
		if (ar[i+1]<=x)
			s1+='3';
		else if (s[i]=='R')
			s1+='2';
		else
			s1+='1';
	}
	int l=0,ans=0;
	while (l<n && (s1[l]=='2' || s1[l]=='3'))
		l++;
	for ( ; l<n; )
	{
		while (l<n && (s1[l]=='1' || s1[l]=='3'))
			l++;
		while (l<n && (s1[l]=='2' || s1[l]=='3'))
			l++;
		ans++;
	}
	return ans <= k;
}

signed main ()
{
	cin>>t;
	while (t--)
	{
		cin>>n>>k;
		cin>>s;
		for(int i=1; i<=n; i++)
			cin>>ar[i];
		int l=0,r=1e9,res=0;
		while (l<=r)
		{
			int mid=(l+r)/2;
			if (check(mid))
			{
				res=mid;
				r=mid-1;
			}
			else l=mid+1;
		}
		cout<<res<<endl;
	}
	return 0;
}
原文
存档评论1 条抓取于