【二进制枚举+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 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
随机推荐
- 算法(第4版)Robert Sedgewick 刷题 第一章(1)
/** * @Description 颠倒数组排列顺序 * @author SEELE * @date 2017年8月17日 上午10:56:17 * @action sortArr */ publi ...
- (5)opencv的基础操作和矩阵的掩模操作
不懂的,可以简单,看看这个网址:https://blog.csdn.net/xiongwen_li/article/details/78503491 图片放到了桌面,所以,图片的路径就是桌面了,剩余的 ...
- Bootstrap-模态框 modal.js
参考网址:http://v3.bootcss.com/(能抄不写) 1.大模态框 图片效果图: 代码:(button的属性data-target对应的是具体模态框的class) <!-- Lar ...
- 从零开始入门 K8s | Kubernetes 存储架构及插件使用
本文整理自<CNCF x Alibaba 云原生技术公开课>第 21 讲. 导读:容器存储是 Kubernetes 系统中提供数据持久化的基础组件,是实现有状态服务的重要保证.Kubern ...
- css3 flex布局详解
原文链接: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool https://www.cnblog ...
- 一键分享mob,方法二
2.快速生成项目http://wiki.mob.com/android-sharesdk完整的集成文档/ 修改目标项目名称和项目的包名: 由于直接复制jar包和资源的集成方式比较麻烦,ShareSDK ...
- format 可以用 * 星号
procedure TForm1.FormCreate(Sender: TObject); var s:string; a:integer; b:Single; begin a:=; b:=108.4 ...
- GTX 1080显卡出错
NVRM: RmInitAdapter failed! (0x26:0xffff:1097) NVRM: rm_init_adapter failed for device bearing minor ...
- 前端Json 增加,删除,修改元素(包含json数组处理)
一:基础JSON对象 二:JSON数组数据 以下为增删修改方法: <!DOCTYPE html> <html lang="en"> <head> ...
- normal equation(正规方程)
normal equation(正规方程) 正规方程是通过求解下面的方程来找出使得代价函数最小的参数的: \[ \frac{\partial}{\partial\theta_j}J\left(\the ...