【Codeforces】Round #528 (Div. 1) 1086C-F题解(错排&拉格朗日插值)

博客详细解析了Codeforces Round #528 中C到F四道题目,涉及模拟、树状数组、递推式和拉格朗日插值等算法。C题是关于模板的模拟问题,D题用树状数组解决Rock-Paper-Scissors比赛冠军问题,E题通过递推和BIT求解排列方案数,F题使用扫描线和多项式分段来处理森林火灾问题,最后利用拉格朗日插值计算系数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Vasya and Templates

模拟+讨论,细节比较多。

没有写,贴了一份CF上最短的代码

#include "bits/stdc++.h"
using namespace std;
string s,a,b;
int mp[26],seen[26],k,T;
bool solve(int i, int smaller, int greater){
   
	if((smaller&&greater) || i==s.size()) return 1;
	if(mp[s[i]]!=-1){
   
		if(!greater&&mp[s[i]]<a[i] || !smaller&&mp[s[i]]>b[i])
			return false;
		return solve(i+1,smaller|mp[s[i]]<b[i],greater|mp[s[i]]>a[i]);
	} else{
   
		for(int c=(greater?0:a[i]);c<=(smaller?k-1:b[i]);++c) if(!seen[c]){
   
			mp[s[i]] = c, seen[c] = 1;
			if(solve(i+1,smaller|c<b[i],greater|c>a[i])) return 1;
			mp[s[i]] = -1,seen[c] = 0;
		}
		return 0;
	}
}

int main(){
   
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	for(cin>>T;T--;cout<<'\n'){
   
		cin>>k>>s>>a>>b;
		for(auto &c:s) c-='a';
		for(auto &c:a) c-='a';
		for(auto &c:b) c-='a';
		memset(mp,-1,sizeof(mp));
		memset(seen,0,sizeof(seen));
		if(solve(0,0,0)){
   
			cout<<"YES\n";
			for(int i=0;i<k;cout<<char(mp[i++]+'a'))
				for(int j=0;j<k && mp[i]==-1;++j) if(!seen[j])
					mp[i] = j, seen[j] = 1;
		}
		else cout<<"NO";
	}
}

D. Rock-Paper-Scissors Champion

某个人赢的必要条件是它的两侧都满足下面两个条件之一:

  • 不存在能严格赢他的人
  • 存在能严格输给他的人
    树状数组维护一下即可。

代码贴的:

#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long ll;
const int N=2e5+10,M=3;
int n,Q,ans;
char s[N];
set<int>st[M];

int read()
{
   
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)) {
   if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

struct BIT
{
   
	#define lowbit(x) (x&(-x))
	int c[N];
	void update(int x,int v){
   for(;x<N;x+=lowbit(x))c[x]+=v;}
	int query(int x){
   int res=0;for(;x;x-=lowbit(x))res+=c[x];return res;}
}bit[M];

int id(char c){
   if(c=='R')return 0;else if(c=='S')return 1;return 2;}
int calc(int p)
{
   
	int win=(p+1)%M,lose=(p-1+M)%M;
	if(st[p].empty()) return bit[win].query(n);
	if(st[lose].empty()) return 0;
	return bit[win].query(n)-bit[win].query(max(*st[p].rbegin(),*st[lose].rbegin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值