【二进制枚举+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\n",ans);
return 0;
}
【二进制枚举+LCS】Card Hand Sorting的更多相关文章
- Card Hand Sorting 二进制枚举暴力
这个题其实由于只有4种花色的,那么每种花色排列的顺序,也不过是4!种,然后对于每种花色内部到底是升序还是降序,其实也可以直接暴力,一共也就4!*2^4种情况,然后直接进行排序就可以了,但是我们如何计算 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】
任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...
- 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 ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- Good Bye 2015B(模拟或者二进制枚举)
B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- hdu 3118(二进制枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
随机推荐
- Java8 Optional类使用小结
Optional类的Javadoc描述如下: 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. of: 为非null的值创建一 ...
- web系统能力培养计划
服务器知识掌握如下 01购买linux服务器 客户端工具:https://mobaxterm.mobatek.net/download.html 02linux常用命令 https://www.run ...
- 寒假day26
根据已有数据爬取新数据充实人才库
- SpringBoot+Shiro+DB (二)
之前我们完成了Spring+Shiro的最基本配置搭建,现在我们再增加上DB,毕竟没有哪个系统会将用户.角色.权限等信息硬编码到代码里.DB选用myslq. 数据库准备 脚本如下.依然是两个用户:ad ...
- oracle(8)视图和查询数据库对象方法
视图 view 视图是数据库的对象之一. 视图也叫做虚表,既虚拟表,本质是对应一条select 语句, select语句的结果集赋予一个名字就是视图的名字. 作用: 1.可以简化复杂的查询 2.可以限 ...
- aliyun服务器lamp配置
1.安装Apache:yum install httpd 2.安装php: yum install php 3.安装mysql客户端:yum install mysql 4.安装mysql服务端:yu ...
- winform屏蔽鼠标右键
/// <summary> /// 屏蔽右键 /// </summary> internal class MenuHandler : IContextMenuHandler { ...
- vue项目起步准备
1. 项目环境: node.js运行环境(不一定要最新特性的最新版本,用合适的版本即可) 2.项目放在git上管理(网上云仓库码云) 1.创建仓库:选择语言js 2.本地代码和线上代码通过git做成关 ...
- axios 等待同步请求用法及多请求并发
axios等待同步请求 直接上代码 首先在函数中返回一个Promise对象,在调用函数使用同步函数,调用目标函数使用await等待即可 参考http://www.cnblogs.com/cckui/p ...
- Docker部署zookeeper集群和kafka集群,实现互联
本文介绍在单机上通过docker部署zookeeper集群和kafka集群的可操作方案. 0.准备工作 创建zk目录,在该目录下创建生成zookeeper集群和kafka集群的yml文件,以及用于在该 ...