Sequence Sorting【Codeforces 1241 D】【思维+LIS最长上升子序列】

Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D


  题意:给你有N个数,我们可以把其中的某个值为“val”的数集体移动到队首或者队尾,我们需要知道最少需要移动多少次。

  思路:我们可以去先求的总的元素的种类数为all,那么接下去我们可以考虑的是不需要移动的数的个数,首先,先不看重复,假如不存在重复的时候,(也就是不存在相同元素的时候)是不是就是去求一个最长上升子序列LIS?然后剩下的就是需要移动的。那么回归到这道存在重复的问题上去,我们知道每个数有最早出现的位置和最后出现的位置,那么,假如它最后出现的位置都影响不到下一个比他大的值,是不是就是不需要移动下一个值了。于是,我们就这样可以处理出来不需要移动的数的个数sum。最后的答案就是all - sum.

1
3
3 2 3
ans:1
1
3
1 2 3
ans:0

My Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&( -x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-7
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 3e5 + 7;
int N, l[maxN], r[maxN], all;
inline void init()
{
    all = 0;
    for(int i=1; i<=N; i++)
    {
        l[i] = r[i] = 0;
    }
}
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        init();
        for(int i=1, vv; i<=N; i++)
        {
            scanf("%d", &vv);
            if(!l[vv]) { l[vv] = i; all++; }
            r[vv] = i;
        }
        int det = 0, las = 0, cnt = 0;
        for(int i=1; i<=N; i++)
        {
            if(!l[i]) continue;
            if(las < l[i]) cnt++;
            else cnt = 1;
            las = r[i];
            det = max(det, cnt);
        }
        printf("%d\n", all - det);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值