RGB Balls(AGC037B)(贪心思想)

7 篇文章 0 订阅
7 篇文章 0 订阅

前言

代码实现题,题目还看错了。。。

题目

3 n 3n 3n 个球排成一排,有 3 3 3 种颜色,每种颜色分别有 n n n 个,有 n n n 个人,每个人取 3 3 3 个不同颜色的球,定义每个人取球代价为所取最远两个球的距离,问代价和最小时取球方案数?

思路

其实做过类似的,但是题目看错了…
类似于扫描统计答案的方法
扫到某一个位置我们记录 R , G , B , R G , R B , G B R,G,B,RG,RB,GB R,G,B,RG,RB,GB 的个数(比如 R G , G R RG,GR RG,GR 等价),由于代价和最小,我们将每个人可以看作由左侧开始,右侧结束的收费站对答案每一步算贡献,对于一个位置而言,如果它可以由 R G , R B , G B RG,RB,GB RG,RB,GB 变为 R G B RGB RGB 那肯定结束,因为不会增加新的收费站,还会减少一个;如果能由 R , G , B R,G,B R,G,B 变为 R G , R B , G B RG,RB,GB RG,RB,GB 那肯定变,因为不会增加新的收费站
然后就很简单了,主要是实现困难…
注意最后要乘 n ! n! n! 因为每个人不一样…

代码

#include<set>
#include<map>
#include<cmath>
#include<deque>
#include<stack>
#include<ctime>
#include<queue>
#include<vector>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
int read(){
	int f=1,x=0;char c=getchar();
	while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}
	while('0'<=c&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f*x;
}
#define MAXN 300000
#define INF 0x3f3f3f3f
#define Mod 998244353
void Add(int &x,int &y){if(x)x--,y++;return ;}
int cnt[10];
char S[MAXN+5];
int main(){
	int n=read();
	scanf("%s",S+1);
	LL ans=1;
	for(int i=1;i<=3*n;i++){
		int opt=(S[i]=='R'?1:S[i]=='G'?2:3);
		if(cnt[7-opt]) ans=ans*cnt[7-opt]%Mod,cnt[7-opt]--;//a b c ab ac bc
		else if(opt==1&&(cnt[2]+cnt[3])) ans=ans*(cnt[2]+cnt[3])%Mod,Add(cnt[2],cnt[4]),Add(cnt[3],cnt[5]);
		else if(opt==2&&(cnt[1]+cnt[3])) ans=ans*(cnt[1]+cnt[3])%Mod,Add(cnt[1],cnt[4]),Add(cnt[3],cnt[6]);
		else if(opt==3&&(cnt[1]+cnt[2])) ans=ans*(cnt[1]+cnt[2])%Mod,Add(cnt[1],cnt[5]),Add(cnt[2],cnt[6]);
		else cnt[opt]++;
	}
	for(int i=1;i<=n;i++)
		ans=ans*i%Mod;
	printf("%lld\n",ans);
	return 0;
}

思考

要仔细读题,要多想想怎么算,多刷题

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值