CF Round #813 (Div. 2) C

题意:

给一串长为n的序列a,全为正整数,可以执行一下操作:选择一个数x,让序列中所有等于这个数的位置变为0。问将此序列变为不下降序列最少需要多少次操作。

思路:

从前往后找降序的位置,将降序位置前面的所有数全部置为0,并记录这些数,为了保证不超时,用一个标记(flag)指明上一次抹除抹到乐哪,下一次从这开始即可。

另外听说有可以直接从后往前扫两遍的做法,但是我不知道怎么证明这种办法的正确性,就不说了。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 1e9 + 9;
const ll INF = 1e18l;
/*
priority_queue<int> big_heep;
priority_queue<int,vector<int>,greater<int> > small_heep;
struct cmp{//if return true,it means that the number in the left was smaller than the right one
		bool  operator ()  (int  a,int  b){
	   }
};
 */
int a[200005];
bool check[200005];
void work() {
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++) {
		cin >> a[i];
		check[a[i]] = false;
	}
	int flag = 1;
	int ans = 0;
	check[0] = true;
	for (int i = 2;i <= n;i++) {
		if (check[a[i]]) a[i] = 0;
		if (a[i] < a[i - 1]) {
			for (int j = flag;j <= i - 1;j++) {
				if (!check[a[j]]) {
					ans++;
					check[a[j]] = true;
					a[j] = 0;
				}
				else a[j] = 0;
			}
			flag = i;
		}
	}
	cout << ans << "\n";
	return;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll t;
	cin >> t;
	while (t--) {

		work();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值