题意

给出 一个扑克牌的序列 求排成一个“有序”序列 最少的插入次数

有序是这样定义的

同一个花色的 必须放在一起 同一花色中的牌 必须是 升序 或者是 降序

然后 A 是最大的

思路

我们可以枚举一下

一共有四种花色的 就是 4!

每种升序 有 升序 和 降序 就是 2 ^ 4

4 ! * 2 ^ 4 = 384 可以接受

然后次数 怎么求呢 就是 n - LCS(arr)

因为 这不是 交换 来变得 有序 如果是交换来变得有序 就是 求逆序数

插入的话 就将这个序列 求出其 最长上升子列 这个 子列不动 其他元素 依次 插入就可以

然后讲讲 怎么枚举

首先 枚举 花色 就是

我们 将 四种花色 分别与 0 1 2 3 对应起来

然后用一个 数组 arr[4]= {0, 1, 2, 3} 求这个花色的全排列 这就定义了 花色的 先后次序

arr[i] 表示 数字i 对应的花色的优先级是多少

然后 枚举 升序 降序

我们可以用 0-15 这16 个数字的二进制来表示 升序 还是降序

我们可以重新 给它 定义一个 v

降序 就是将 原值 取负数 然后 按 升序排序 实际上 就是 原值的降序排序

比如 0 这个数字 对应四位的二进制数字 是 0000

所以 这个时候 四种花色 都是 升序的 我们可以用花色的优先级 来表示 对应哪位数字

比如 此时 s 花色 对应的 优先级是 0 那么 就是将 0 右移 0 位 再 & 1 就可以判断它对应位 是 1 还是 0

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 4e5 + 5;
const int MOD = 1e9 + 7; map <char, int> m; int color[4] = { 0, 1, 2, 3 }; void init()
{
m['s'] = 0; // suit
m['h'] = 1;
m['d'] = 2;
m['c'] = 3; for (int i = 2; i <= 9; i++)
{
m[i + '0'] = i;
}
m['T'] = 10; // num
m['J'] = 11;
m['Q'] = 12;
m['K'] = 13;
m['A'] = 14;
} struct node
{
int num, suit;
int v;
int id;
}q[60]; bool comp(node x, node y)
{
if (x.suit == y.suit)
return x.v < y.v;
else
return color[x.suit] < color[y.suit];
} int n; int LCS(node q[])
{
int dp[60];
CLR(dp, 0);
dp[0] = 1;
int ans = 1;
for (int i = 1; i < n; i++)
{
dp[i] = 1;
for (int j = 0; j < i; j++)
{
if (q[i].id > q[j].id)
{
dp[i] = max(dp[i], dp[j] + 1);
}
}
ans = max(dp[i], ans);
}
return ans;
} int main()
{
init();
scanf("%d", &n);
char s[3];
for (int i = 0; i < n; i++)
{
scanf(" %s", &s);
q[i].id = i;
q[i].num = m[s[0]];
q[i].suit = m[s[1]];
}
int ans = INF;
do
{
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < n; j++)
{
q[j].v = q[j].num * (((i >> color[q[j].suit]) & 1) != 1 ? 1 : -1);
}
sort(q, q + n, comp);
ans = min(ans, n - LCS(q));
}
} while (next_permutation(color, color + 4));
cout << ans << endl;
}

Kattis - cardhand Card Hand Sorting 【暴力枚举】的更多相关文章

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

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

  2. 【二进制枚举+LCS】Card Hand Sorting

    [二进制枚举+LCS]Card Hand Sorting 题目描述 When dealt cards in the card game Plump it is a good idea to start ...

  3. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  4. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  5. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  6. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  7. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  8. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  9. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

随机推荐

  1. Linux下防火墙iptables用法规则详及其防火墙配置

    转:http://www.linuxidc.com/Linux/2012-08/67952.htm iptables规则 规则--顾名思义就是规矩和原则,和现实生活中的事情是一样的,国有国法,家有家规 ...

  2. centos下lvs配置

    一.lvs-nat模式 网络配置: lvs-server eth0 :host-only adapter 192.168.56.101 lvs-server eth1 :Internal 192.16 ...

  3. vuex 中关于 mapMutations 的作用

    mapMutations 工具函数会将 store 中的 commit 方法映射到组件的 methods 中.和 mapActions 的功能几乎一样,我们来直接看它的实现: export funct ...

  4. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  5. JDK5新特性之线程同步工具类(三)

    一. Semaphore Semaphore能够控制同一时候訪问资源的线程个数, 比如: 实现一个文件同意的并发訪问数. Semaphore实现的功能就类似厕全部5个坑, 增加有十个人要上厕所, 那么 ...

  6. (一)关于jQuery的网上资源

    jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...

  7. nightwatch 切换窗口

    .switchWindow() Change focus to another window. The window to change focus to may be specified by it ...

  8. CSS3进度条 和 HTML5 Canvas画圆环

    看到一些高大上的进度条插件,然后想自己用CSS写.经过搜索资料之后,终于成功了.为了以后方便拿来用,或者复习.将代码贴出. HTML代码: 只需要两个div,外面的为一个有border的div id为 ...

  9. 【C语言天天练(二)】statickeyword

    引言:                 statickeyword不仅能够修饰变量.并且能够修饰函数.了解它的使用方法,不仅对阅读别人的代码有帮助,也有助于自己写出更加健壮的程序. 使用方法:     ...

  10. 小明同学喜欢体育锻炼,他常常去操场上跑步。跑道是一个圆形,在本题中,我们认为跑道是一个半径为R的圆形,设圆心的坐标原点(0,0)。小明跑步的起点坐标为(R,0),他沿着圆形跑道跑步,而且一直沿着一个方向跑步。回到家后,他查看了自己的计步器,计步器显示他跑步的总路程为L。小明想知道自己结束跑步时的坐标,但是他忘记自己是沿着顺时针方向还是逆时针方向跑的了。他想知道在这两种情况下的答案分别是多少。

    include "stdafx.h" #include<iostream> #include<vector> #include<string> ...