codeforces计划——800 - 900分

本文详细探讨了Codeforces中800到900分段的算法问题,包括四色花环的正确解决方案、交替二进制序列的解析、抓糖果的策略、Mex Number的概念以及Ideal Point的讨论。通过实例代码和分析,帮助读者掌握这些算法的关键点。
摘要由CSDN通过智能技术生成

一.四色花环

在这里插入图片描述
        以下代码的三个问题:(1)像333444451这种数字串无法得到最长的相同数字组成的子序列;(2)cnt无法准确计数出重复子序列中相同数的个数,例如4444,cnt会计数出6;(3)最后计算需要的总操作次数公式错误。

#include<iostream>
using namespace std;
int root[5];

int main()
{
   
	int t;
	cin >> t;
	while (t--)
	{
   
		for (int i = 0; i < 4; i++)	cin >> root[i];
		int tag = 0;
		int cnt = 0;
		for (int i = 0; i < 4; i++)
		{
   
			for (int j = i + 1; j < 4; j++)
			{
   
				if (root[i] == root[j])
				{
   
					tag = root[i];
					if (root[i] == tag)	cnt++;
				}
			}
		}
		int remained = 4 - cnt;
		if (remained >= cnt - 1)	cout << "4" << endl;
		else if (remained % 2 == 0)	cout << 2 * cnt - 1 << endl;
		else	cout<<2* cnt << endl;
	}
}

       &nbsp以下代码是正确的——

#include<iostream>
using namespace std;

int main()
{
   
	int t;
	cin >> t;
	while (t--)
	{
   
	//不能用整数数组读入,因为输入数据并不会换行!
		char s[5];
		cin >> s;
		int mx = 0;
		for (int i = 0; i < 4; i++)
		{
   
			int cnt = 1;
			for (int j = i + 1; j < 4; j++)
			{
   
				if (s[i] == s[j])
				{
   
					cnt++;
					//mx有两个作用,一是防止组合数对计数的影响,二是计算出最长的重复子序列
					mx = cnt > mx ? cnt : mx;
				}
			}
		}
		int remained = 4 - mx;
		if (mx == 4)	cout << " -1 " << endl
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值