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

【二进制枚举+LCS】Card Hand Sorting的更多相关文章

  1. Card Hand Sorting 二进制枚举暴力

    这个题其实由于只有4种花色的,那么每种花色排列的顺序,也不过是4!种,然后对于每种花色内部到底是升序还是降序,其实也可以直接暴力,一共也就4!*2^4种情况,然后直接进行排序就可以了,但是我们如何计算 ...

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】

    任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...

  4. Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)

    Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her ...

  5. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  6. Good Bye 2015B(模拟或者二进制枚举)

    B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  8. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  9. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

随机推荐

  1. 直播课(1)如何通过数据劫持实现Vue(mvvm)框架

    19.6.28更新: 这篇博客比较完善:将每一部分都分装在单独的js文件中: 剖析Vue原理&实现双向绑定MVVM 半个月前看的直播课,现在才自己敲了一遍,罪过罪过 预览: 思路: 简单实现V ...

  2. 【数据结构】C++语言环形队列的实现

    队列--先进先出 队列的一个缺点--出队后的内存空间浪费了,不能二次利用 环形队列--解决以上缺点的队列,用过的内存空间可以重复利用 github: https://github.com/HITFis ...

  3. 吴裕雄--天生自然 PHP开发学习:PHP编程基础知识

    <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> <?php $txt="Hello world!"; $x=5; $y=10.5; ...

  4. ArchLinux安装KDE桌面

    ArchLinux安装KDE桌面 一.链接网络 1.有线 # dhcpcd 2.无线 # wifi-menu 3.检查 # ping www.baidu.com 二.安装X服务 # pacman -S ...

  5. Git--git log

    参考 https://www.cnblogs.com/bellkosmos/p/5923439.html https://www.cnblogs.com/mkl34367803/p/9219913.h ...

  6. SpringCloud学习之Feign 的使用(五)

     Feign 是一个声明式的伪RPC的REST客户端,它用了基于接口的注解方式,很方便的客户端配置,刚开始使用时还不习惯,感觉是在客户端写服务端的代码,Spring Cloud 给 Feign 添加了 ...

  7. 吴裕雄--天生自然MySQL学习笔记:MySQL 正则表达式

    下表中的正则模式可应用于 REGEXP 操作符中. 实例 查找name字段中以'st'为开头的所有数据: mysql> SELECT name FROM person_tbl WHERE nam ...

  8. SQL基础教程(第2版)第8章 SQL高级处理:8-2 GROUPING运算符

    第8章 SQL高级处理:8-2 GROUPING运算符 ■ GROUPING SETS——取得期望的积木● 只使用GROUP BY子句和聚合函数是无法同时得出小计和合计的.如果想要同时得到,可以使用G ...

  9. hash简单题(hdu4907)

    Task schedule 地址:http://acm.hdu.edu.cn/showproblem.php?pid=4907 Problem Description 有一台机器,并且给你这台机器的工 ...

  10. ZJNU 2353 - UNO

    大模拟,但是题目好像有些地方表述不清 根据UNO在初中曾被别人虐了很久很久的经历 猜测出了原本的题意 本题中的+2虽然有颜色,但是也可以当作原UNO游戏中的+4黑牌 即在某人出了+2后,可以出不同颜色 ...