码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-07 00:45:52      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:rds   ems   cte   des   ade   space   within   ted   idea   

【二进制枚举+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 ?rst 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 ?rst 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\n",ans);
    return 0;
}

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

标签:rds   ems   cte   des   ade   space   within   ted   idea   

原文地址:https://www.cnblogs.com/smallocean/p/9749053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!