【洛谷P2652】同花顺

题面

分析

今天考了这道题qwq,题解参考了老师的solution。

\(30pts\):暴搜解决

\(60pts\):去重+贪心

下面是满分做法:

首先,肯定是要排序的。我们按照花色排序,相同花色的按照数字排序。然后就是去重,由于重复的牌无论如何也会被替换掉,所以我们先将其去重,并记录下此时的牌数。

考虑枚举每种花色中,以每一张牌作为结尾时可能的序列,取最大值,最后用排序前的总牌数减去这个值即可,显然地,如果当前牌的值为\(a\)\(last\)的值为\(b\),则需满足\(a-b+1>m\)

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;

template <typename T> inline void read(T &x) {
    T f = 1; x = 0; char c;
    for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    x *= f;
}

struct card {
    int a, b;
    bool operator < (const card &x)const{return a < x.a || (a == x.a && b < x.b);}
    bool operator == (const card &x)const{return a == x.a && b == x.b;}
}cd[100003];

int n, last, ans;

int main() {
//  File("card");
    read(n);
    for (int i = 1; i <= n; ++i) read(cd[i].a), read(cd[i].b);
    sort(cd + 1, cd + 1 + n);
    int m = n;
    n = unique(cd + 1, cd + 1 + n) - (cd + 1);
    for (int i = 1; i <= n; ++i) {
        if (i == 1 || cd[i].a != cd[i-1].a) last = i;
        while(cd[i].b - cd[last].b + 1 > m) last++;
        ans = max(ans, i - last + 1);
    }
    printf("%d",m - ans);
    return 0;
}

转载于:https://www.cnblogs.com/hlw1/p/11303356.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值