cf,,,,

给定整数n,你需要找到一个序列a,使得序列中所有元素相等,并且可以通过不断选择不包含某个出现过的元素x的子序列进行删除操作来达到目标。题目还提供了两个具体的操作实例,分别是关于序列变换和特殊排列的问题。你需要为每个测试用例找到符合条件的序列长度和序列本身。
摘要由CSDN通过智能技术生成
​
#include<iostream>
#include<cstdio>
#include<cstring> 
using namespace std;
const int mxn = 2e5+5;
int cnt[mxn],b[mxn],a[mxn],a2[mxn];
int len,k,n,ans;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(cnt,0,sizeof(cnt));
		len=k=0;  
        
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);  //读入序列a
			if(a[i]!=a[i-1])
			{  //压缩序列a2[]
				k++;
				a2[k]=a[i];
			}
		}
        
		for(int i=1;i<=k;i++)
		{
			if(!cnt[a2[i]])
			{    //特判第一次出现
				if(i>1)  //不在开头,左侧有数组,划分段数++
					cnt[a2[i]]++;   //第一次出现过了 
			
				len++;   //第一次出现则加入最终的答案筛选数组;
				b[len]=a2[i];
			}
			
			if(i<k)  //不在结尾,划分段数++;***如开头:它不在结尾***** 
				cnt[a2[i]]++;        //若非第一次出现,之前第一次出现已经加过来 ,不用b[len] 
		}
		
    
		ans=cnt[b[1]];          //cnt:划分段数  b:筛选数组 
		for(int i=2;i<=len;i++)
		{
			ans=ans<cnt[b[i]] ?ans:cnt[b[i]];
		}
		printf("%d\n",ans);
	}
	return 0;
} 

​

C

C. Sequence Transformation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence a�, initially consisting of n� integers.

You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).

To achieve this, you choose some integer x� that occurs at least once in a�, and then perform the following operation any number of times (possibly zero): choose some segment [l,r][�,�] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains x�. More formally, you choose some contiguous subsequence [al,al+1,…,ar][��,��+1,…,��] such that ai≠x��≠� if l≤i≤r�≤�≤�, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)(�+1)-th is now l�-th, the element that was (r+2)(�+2)-th is now (l+1)(�+1)-th, and so on (i. e. the remaining sequence just collapses).

D. Number into Sequence
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值