【二进制枚举+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 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
随机推荐
- 六、CI框架之分配变量
一.在controllers里面添加 $this->load->vars('m_Str1','我是一个字符串变量'); 二.在View中添加相应代码 界面显示效果如下: 不忘初心,如果您认 ...
- 优秀的linux学习网站
从网络上拷贝别人归纳的列表. Linux优秀网站列表 国内 http://www.chinaunix.net/ 国内最火爆的unix/linux论坛 http://www.linuxforum.net ...
- tf.summary可视化参数
1.tf.summary.scalar('accuracy', accuracy) 损失值.准确率随着迭代次数的进行,其指标变化情况:一般在画loss,accuary时会用到这个函数. 2.tenso ...
- ArrayList源码阅读笔记
ArrayList ArrayList继承自AbstractList抽象类,实现了RandomAccess, Cloneable, java.io.Serializable接口,其中RandomAcc ...
- mysql5.6免安装使用
一.去MYSQL官网下载MYSQL免安装版,由于我的系统是64位的,所以就下载了64位的Mysql版本 http://cdn.mysql.com//Downloads/MySQL-5.6/mysql- ...
- Mac电脑如何彻底删除node
之前本来想搭建一个hexo来写博客的,但是最后还是放弃,老老实实就在博客园和CSDN写博文了,这里记录一下怎么在Mac电脑下彻底删除node.js的方法 下面这个方法是我结合了网上好几个方法综合在一起 ...
- Map的6种遍历方法
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 探讨有几种遍历Map的方法其实意义并不大,网上的文章一般讲4种或5种的居多,重要的是知道遍历的内涵,从遍历 ...
- idea拉取git项目并创建为maven项目(新创建github项目)
0 环境 系统环境:win10 编辑器:idea 1 正文 1 clone项目 跟着提示yes 下一步 2 在根节点添加pom.xml(maven) <?xml version="1. ...
- [Python Cookbook]Pandas: How to increase columns for DataFrame?Join/Concat
1. Combine Two Series series1=pd.Series([1,2,3],name='s1') series2=pd.Series([4,5,6],name='s2') df = ...
- 北邮14&18年软院机试【参考】答案
2014 Problem A. 奇偶求和 题目描述: 给定N个数,分别求出这N个数中奇数的和以及偶数的和. 输入格式 第一行为测试数据的组数T(1<=T<=50).请注意,任意两组测试数据 ...