zoukankan      html  css  js  c++  java
  • 【二进制枚举+LCS】Card Hand Sorting

    【二进制枚举+LCS】Card Hand Sorting

    题目描述

    When dealt cards in the card game Plump it is a good idea to start by sorting the cards in hand by suit and rank. The different suits should be grouped and the ranks should be sorted within each suit. But the order of the suits does not matter and within each suit, the cards may be sorted in either ascending or descending order on rank. It is allowed for some suits to be sorted in ascending order and others in descending order.
    Sorting is done by moving one card at a time from its current position to a new position in the hand, at the start, end, or in between two adjacent cards. What is the smallest number of moves required to sort a given hand of cards?

    输入

    The first line of input contains an integer n (1 ≤ n ≤ 52), the number of cards in the hand. The second line contains n pairwise distinct space-separated cards, each represented by two characters. The first character of a card represents the rank and is either a digit from 2 to 9 or
    one of the letters T , J , Q , K , and A representing Ten, Jack, Queen, King and Ace, respectively, given here in increasing order. The second character of a card is from the set { s , h , d , c } representing the suits spades ♠, hearts ♥, diamonds ♦, and clubs ♣.

    输出

    Output the minimum number of card moves required to sort the hand as described above.

    样例输入

    7
    9d As 2s Qd 2c Jd 8h

    样例输出

    2

    看一眼,52,嗯 状态压缩 暴力 二进制枚举?然后,怎么换啊,不会。好难。。。
    比赛结束,LCS,嗯,会了
    LCS差点不会写...尴尬
    通过移位符判断位置。

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    map<char,int>mp;
    inline void init()
    {
        for(int i=2;i<=9;++i){
            mp[i+'0']=i;
        }
        mp['T']=10;
        mp['J']=11;
        mp['Q']=12;
        mp['K']=13;
        mp['A']=14;
        mp['s']=0;
        mp['h']=1;
        mp['d']=2;
        mp['c']=3;
    }
    struct node
    {
        int id;
        int val;
        int block;
        int k;
    }s[55];
    int dp[55],n;
    inline bool cmp(node x,node y)
    {
        if(x.k==y.k){
            return x.val<y.val;
        }
        return x.k<y.k;
    }
    inline int lcs(){
       memset(dp,0,sizeof(dp));
       int ans=1;
       dp[0]=1;
       for(int i=1;i<n;++i){
            dp[i]=1;
        for(int j=0;j<i;++j){
            if(s[i].id>s[j].id){
                dp[i]=max(dp[j]+1,dp[i]);
            }
        }
        ans=max(dp[i],ans);
       }
       return ans;
    }
    int main()
    {
        init();
        scanf("%d",&n);
        char str[3];
        for(int i=0;i<n;++i){
            scanf("%s",str);
            s[i].val=mp[str[0]];
            s[i].block=mp[str[1]];
            s[i].id=i;
        }
        int arr[4]={0,1,2,3},ans=1e9;
        do{
            for(int i=0;i<16;++i){
                for(int j=0;j<n;++j){
                    s[j].k=arr[s[j].block];
                    s[j].val=abs(s[j].val)*(((i>>s[j].k)&1)!=1?-1:1);
                }
                sort(s,s+n,cmp);
                ans=min(ans,n-lcs());
            }
        }while(next_permutation(arr,arr+4));//全排列
        printf("%d
    ",ans);
        return 0;
    }
    
    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    JavaScript—— scroolleftoffsetleft 系列的含义以及浏览器兼容问题
    GCD
    Treap
    快速* 模板
    线性筛素数
    珂朵莉树
    One Night
    长整数相加运算(内含减法)。。= =
    骑士周游 非完美版。。= =
    《Windows取证分析》
  • 原文地址:https://www.cnblogs.com/smallocean/p/9749053.html
Copyright © 2011-2022 走看看