B. Elimination of a Ring Pinely Round 1 (Div. 1 + Div. 2)

传送门

题目意思: 给你一个为环的序列n,这个序列有一个特殊的地方:就是如果有相邻元素相等他就会立马删除其中一个元素(第一个和最后一个相邻)。 然后你每次可以删除一个元素,问你能删除的最多的操作数是多少。

题目思路:

我们可以发现如果有一个元素只有一个数的话,那么可以通过以这个元素为中心不断删除n次。

首先如果元素种类为2 的话,肯定结果就是n/2+1,

当环中至少含有三种不同的元素 假设是a,b,c(三种),然后我们以任意一个元素为中心

,假设是b然后删除他周围的能删的数,直到不能删了或者有一个数存在一个

如果是不能以b为中心去删了,那么因为至少含有三种元素,那么一定会有一个元素只有一个。、

那么无论如果都会有存在一个元素只存在一个的情况。

所以可以一直删直到n次

所以就是如果元素种类大于等于3就是n,否则为n/2+1;

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
ll s[N];



int main()
{
	int t;
	sc_int(t);
	while(t--)
	{
		map<int,int>q;
		cin>>n;
		int res=0;
		for(int i =1;i<=n;i++)
		{
			cin>>s[i];
			if(!q[s[i]]){
			q[s[i]]=1;
			res++;
			}
		}
		if(res>=3)cout<<n<<endl;
		else cout<<n/2+1<<endl;
	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值