链接:

https://codeforces.com/contest/1241/problem/D

题意:

You are given a sequence a1,a2,…,an, consisting of integers.

You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.

For example, if a=[2,1,3,1,1,3,2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):

[1,1,1,2,3,3,2] if you move all 1-elements to the beginning;

[2,3,3,2,1,1,1] if you move all 1-elements to the end;

[2,2,1,3,1,1,3] if you move all 2-elements to the beginning;

[1,3,1,1,3,2,2] if you move all 2-elements to the end;

[3,3,2,1,1,1,2] if you move all 3-elements to the beginning;

[2,1,1,1,2,3,3] if you move all 3-elements to the end;

You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition ai−1≤ai is satisfied.

Note that you have to answer q independent queries.

思路:

记录每个值的左端点和右端点,

然后找出满足范围不相交的最长的连续值.

比赛想了半天硬是没想到能记录两个点..

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5+10; set<int> St;
int l[MAXN], r[MAXN];
int n; int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
St.clear();
int v;
cin >> n;
for (int i = 1;i <= n;i++)
l[i] = n, r[i] = 1;
for (int i = 1;i <= n;i++)
{
cin >> v;
l[v] = min(i, l[v]);
r[v] = max(i, r[v]);
St.insert(v);
}
int cnt = 0, rp = -1, ans = 0;
for (auto x: St)
{
if (l[x] > rp)
cnt++;
else
cnt = 1;
rp = r[x];
ans = max(cnt, ans);
}
cout << St.size()-ans << endl;
} return 0;
}

最新文章

  1. css学习笔记 5
  2. 用HTML和CSS实现WWDC 2015上的动画效果
  3. maven自建仓库 Return code : 405
  4. paper 96:计算机视觉-机器学习近年部分综述
  5. STL--list
  6. C51-keil编译常见错误和警告处理53
  7. bulk insert data into database with table type .net
  8. 【C# in depth 第三版】温故而知新(2)
  9. HDU 5909 Tree Cutting
  10. js取数组最大值的四种方式
  11. mssql sqlserver 将字段null(空值)值替换为指定值的三种方法分享
  12. WinterAndSnowmen
  13. 研究slatstack时踩过的坑,注意点及解决方案
  14. python assert使用说明
  15. C#实现的协同过滤算法
  16. Jersey 2.x 分支 Java SE 兼容性
  17. 【转】matplotlib制图——图例legend
  18. 4539: [Hnoi2016]树
  19. 高并发第二弹:并发概念及内存模型(JMM)
  20. 洛谷【P1854】花店橱窗布置

热门文章

  1. sleep(0) 的作用
  2. Object类入门这一篇就够了!
  3. Python解Leetcode: 539. Minimum Time Difference
  4. N分成不同的数相乘使答案最大
  5. Beautifulsoup模块基础用法详解
  6. c c++各种类型的取值范围
  7. T100——汇总错误消息显示
  8. JVM描述符标识字符含义
  9. dotnet跨平台 - 使用Nginx+Docker Compose运行.NETCore项目
  10. [NOIP10.6模拟赛]1.merchant题解--思维+二分