[Codeforces-Gym] (101550C)Card Hand Sorting ---- 状态压缩+思维

题目传送门

题意:

  • 给你一副扑克牌,其中不包括大王和小王,共52张,即4*13
  • 有四种花色分别定义为s,h,d,c
  • 然后扑克牌的数字从2~9 大小定义为依次从小到大,剩下的大小为A>K>Q>J>T>9
  • 现在给你n张扑克牌,让你给他们排好序,注:这种排序不是交换相邻的那种,而是可以选择一张插入到某个位置
  • 排序的要求是:同一类型的扑克牌按照降序或升序排列,不同类型的扑克牌间不做要求。
  • 让你输出最少操作次数

做法:

  • %%超霸的思路
  • 四种扑克牌间全排列是4! = 24
  • 一种扑克牌有升降两种状态,那么四种就是2^4 = 16
  • 即全部可能一共有24*16 = 384种,所以我们可以枚举这些状态,找出最优的
  • 技巧点: 同一种扑克牌升序或降序都用从小到大的数表示(PS:因为该题中升序和降序等价,不如直接把降序处理成升序)
  • 所以最终答案就是求什么嘞?没错!即使求一个最长上升子序列的长度len, n-len = ans(即操作次数)
  • 比如题目给的样例:9d As 2s Qd 2c Jd 8h   ---->  As 2s  Qd Jd 9d 2c 8h
  • 我们看出数字  1   2   2003 2004 2006 3002  1008      -----> LIS = 5,ans(min) = 7 - 5 = 2

AC代码:

#include<bits/stdc++.h>
#define IO          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x)       push_back(x)
#define sz(x)       (int)(x).size()
#define sc(x)       scanf("%d",&x)
#define abs(x)      ((x)<0 ? -(x) : x)
#define all(x)      x.begin(),x.end()
#define mk(x,y)     make_pair(x,y
#define fin         freopen("in.txt","r",stdin)
#define fout        freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e5+5;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
int n;
struct node
{
    int num;
    int type;
}a[60];
int thash[] = {0,1,2,3}; //0--s 1--h 2--d 3--c
int nhash[] = {0,1000,2000,3000};//转换成最长上升子序列模型
int aord[10];
int b[60];
int dp[60];
int LIS()
{
    memset(dp,INF,sizeof(dp));
    for(int i=0;i<n;i++) *lower_bound(dp,dp+n,b[i]) = b[i];
    int len = lower_bound(dp,dp+n,INF) - dp;
    return n-len;
}
void solve()
{
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(thash[i] == a[j].type){
                if(aord[i] == 1) b[j] = nhash[i]+a[j].num;
                else b[j] = nhash[i] + 15-a[j].num;
            }
        }
    }
}
int main()
{
    // fin;
    IO;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        char k,m;
        cin>>k>>m;
        if(k == 'T') a[i].num = 10;
        else if(k == 'J') a[i].num = 11;
        else if(k == 'Q') a[i].num = 12;
        else if(k == 'K') a[i].num = 13;
        else if(k == 'A') a[i].num = 14;
        else a[i].num = k-'0';
        if(m == 's') a[i].type = 0;
        if(m == 'h') a[i].type = 1;
        if(m == 'd') a[i].type = 2;
        if(m == 'c') a[i].type = 3;
    }
    int ans = INF;
    do{
        for(int s=0;s<(1<<4);s++)
        {
            for(int i=0;i<4;i++)
            {
                if(s&(1<<i)) aord[i] = 1; //升序
                else aord[i] = 0; //降序
            }
            solve();
            ans = min(ans,LIS());
        }
    }while(next_permutation(thash,thash+4));
    cout<<ans<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值