Sequence Transformation Codeforces Round #686 (Div. 3)

传送门

题目

在这里插入图片描述

输入

5
3
1 1 1
5
1 2 3 4 5
5
1 2 3 2 1
7
1 2 3 1 2 3 1
11
2 2 1 2 3 2 1 2 3 1 2

输出

0
1
1
2
3

题意

给你一个数组,在数组中随便找一个数字x,找的这个x在数组中可能有多个,任何一个x都不能删除。每一次操作可以删除不包含x的任何一个区间段,问最少需要操作几次,可以让数组中只剩数字x。

思路

枚举每一个数的间隔,然后取最小值。

AC代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <list>
#include <set>
#include <cstdlib>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int N = 10010;
const int INF = 0x3f3f3f3f;
const ll  LNF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const double eps = 10e-6;
const double PI = 3.1415926;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
priority_queue<int, vector<int>, less<int> >q;
priority_queue<int, vector<int>, greater<int> >qq;

int a[maxn], b[maxn];
map<int, int>mp, mmp;
int main(){
	IOS
	int t;
	cin >> t;
	while (t--){
		mp.clear(); //mp用来存每个数上次出现的下标
		mmp.clear(); //mmp用来存需要操作几次
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
			if (i - mp[a[i]] > 1){ //i-mp[a[i]]是判断两个相同数是否相邻
				mmp[a[i]]++;	   //如果不相邻,就要加一次操作
				mp[a[i]] = i;      //更新最新出现数字的下标
			}else{
				mp[a[i]] = i;
			}
		}
		if (n == 1){ //如果只有一个数字,那么不需要操作,直接就是0
			cout << 0 << endl;
			continue;
		}
		mmp[a[n]]--; 
		//因为每个数字最后出现在哪是不确定的,如果不在最后一位,那么操作还
		//需要加一次(删除后面的的部分),如果是最后一位当然就不用减了
		//所以这里让最后一位减一,下面判断最小值时让mmp[a[i]]里每个数字加一
		int minn = INF;
		for (int i = 1; i <= n; i++){
			minn = min (minn, mmp[a[i]] + 1); //更新最小操作次数
		}
		cout << minn << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值