CSU 2020 Card Hand Sorting(暴力枚举+LIS DP)

2020: Card Hand Sorting

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 44     Solved: 19    

Description

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?

Input

There will be several test cases. For the each case, 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

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

Sample Input

4
2h Th 8c Qh
7
9d As 2s Qd 2c Jd 8h
4
2h 3h 9c 8c

Sample Output

1
2
0

Hint

Source

NCPC2016

        大致题意:有n张扑克牌,大小规则有所不同,2是最小的,没有小丑,其他不变。现在按照顺序给出这n张牌,要求你进行排序,使得同花色的排连续的排在一起,然后每个花色内部要求递增或者递减。每次你可以把某一张牌移动到某两张牌中间或者最前和最后面。问最少的移动次数。
        首先,还是回顾LIS相关的应用。显然就是求最小交换次数使得一个序列不上升或者不下降。所有同样也可以考虑用LIS来解决这道题。但考虑到有花色的限制,必须先去解决花色的问题。这里由于数据范围比较的小,可以考虑暴力的枚举最后的排列方式。由于花色只有四种,那么对于花色的排法就有4!=24种,每种花色内部的递增或减有2^4=16种,总共就是24*16=384种最终的排列方式。我们要做的,就是看究竟哪种排列方式需要的移动次数最少。

        对于每种排列方式,固定了花色顺序和每种花色内部的递增减,也就确定了最后的时候每个位置应该放置哪一张牌,也即知道了每张牌最后所在的位置。如果我们把每张牌最后位置按照小到大的顺序排了,那么也就自然的满足了题目的摆放要求。那么可以考虑把这个最后的摆放位置作为每个牌新的权值标号,然后对这个标号求LIS,总的牌数n-LIS即为最后移动的次数。具体见代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#define N 110
using namespace std;

int n,m,len,dp[N],order[5],a[N],ans;
struct card{char id,kind;} c[N];
map<char,int> mp;
bool v[5],d[5];

void init()                                                //对每张牌进行重标号,这个号就是可以对应最后的位置,但是有放大
{
    len=0;
    for(int i=1;i<=n;i++)
        if (d[mp[c[i].kind]]) a[i]=(order[mp[c[i].kind]]-1)*20+mp[c[i].id];
                             else a[i]=order[mp[c[i].kind]]*20-mp[c[i].id];
}

void DP()                                                  //直接求LIS
{
    dp[++len]=a[1];
    for(int i=2;i<=n;i++)
    {
        if (a[i]>=dp[len]) dp[++len]=a[i];
        else
        {
            int j=lower_bound(dp+1,dp+1+len,a[i])-dp;
            dp[j]=a[i];
        }
    }
    ans=min(ans,n-len);
}

void dfs(int dep)                                                         //暴力枚举花色顺序和内部递增减
{
    if (dep>4)
    {
        init(); DP();
        return;
    }
    for(int i=1;i<=4;i++)
    {
        if (v[i]) continue;
        order[i]=dep; v[i]=1; d[i]=1;                                    //d[]表示某张牌是递增还是递减
        dfs(dep+1); d[i]=0;
        dfs(dep+1); v[i]=0;
    }
}

int main()
{
    mp['s']=1;mp['h']=2;mp['d']=3;
    mp['c']=4;mp['T']=10;mp['J']=11;
    mp['Q']=12;mp['K']=13;mp['A']=14;
    for(int i=2;i<=9;i++) mp[char(i+48)]=i;
    while(cin>>n)
    {
        char x;
        getchar();
        ans=0x3f3f3f3f;
        for(int i=1;i<=n;i++)
            scanf("%c%c%c",&c[i].id,&c[i].kind,&x);
        dfs(1); printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CSU飞跃手册2019是一个以服务学生发展为宗旨的学生服务项目,帮助学生实现个人成长和职业发展目标。飞跃手册提供了诸如成功学、职业规划、就业技巧以及个人发展等方面的指导,帮助学生掌握自我认知、提升职场竞争力并构建良好的人脉关系。 飞跃手册2019包括三个模块。第一个模块着重讲解如何探索自我,明确职业目标和规划职业道路。这些关键的步骤帮助学生发现自己的职业特长和兴趣爱好,从而为个人职业发展做出决策和规划。 第二个模块提供了职业发展过程中的必备技能,如招聘面试技巧、简历写作、职场沟通、决策和问题解决能力,以及管理和领导力等。这些技能帮助学生在职场中不断学习和成长,并展示自己的优势。 第三个模块则介绍如何利用社会资源和人脉关系来推动个人职业发展。它包括了网络管理、人脉建立、社交礼仪和社会责任等方面的指导。 总之,CSU飞跃手册2019是一份应对职场挑战和工作生涯的指南, 帮助学生建立自己的职业目标并实现自我发展。它讲解了个人成长、职场技能、人脉力量和社会责任等方面的内容,为学生的成功创造了一个强有力的支撑体系。 ### 回答2: CSU飞跃手册2019是一本由中国海洋大学控制科学与工程学院发行的指导学生爆炸性训练和实习的手册。该手册将学生分为不同职业兴趣组,并提供了具体的介绍、岗位培训以及实习就业方案。 该手册还包括了部分参考书目、学术论文和个人成长方向的建议,在实习期间指导学生更好地发展个人能力和提高自己的竞争力。除此之外,该手册还有一些校内外的实践活动介绍和经验分享。这些活动涵盖了多个不同领域,包括科技、创业、环保和社会公益等。 该手册还提供了一些面试技巧和求职指南,帮助学生更好地应对就业市场的挑战。此外,该手册还鼓励学生通过网络平台和社会实践等多种途径积累人脉资源,增强自己的社交和交际技巧。 总的来说,CSU飞跃手册2019为学生提供了一个全面的指导工具,帮助他们更好地理解职业规划,提高个人素质,扩展职业视野并实现自我价值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值