题意

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

有序是这样定义的

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

然后 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. Fatal error: Call to a member function read() on a non-object in

    是你的路径出问题了系统 > 系统基本参数 > 站点设置 里面的<站点根网址:和 网页主页链接:>系统 > 系统基本参数 > 核心设置 <DedeCMS安装目录 ...

  2. Mac搭建python环境

    1 安装xcode 2 安装 brew ruby-e"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mast ...

  3. sprinvmvc整合swagger实现实时接口信息展示

    1.pom.xml引入swagger插件 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...

  4. VC++动态链接库(DLL)编程深入浅出(四)

    这是<VC++动态链接库(DLL)编程深入浅出>的第四部分,阅读本文前,请先阅读前三部分:(一).(二).(三). MFC扩展DLL的内涵为MFC的扩展,用户使用MFC扩展DLL就像使用M ...

  5. 报错: Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  6. 【秀优越(xie e)】原来出题也能够这么恶心。

    通过邪恶的数据范围和数据限制居然能够把一道传统题出成题答2333. 诶毕竟内部互測,题目就不往上贴了. 特殊限制 - - - 题目作废.输出M行"Orz  PoPoQQQ" - M ...

  7. 【AngularJS】【03】使用AngularJS进行开发

    看不到PPT的请自行解决DNS污染问题.

  8. webstrom 应用 转(http://blog.csdn.net/zghekuiwu/article/details/54382145)

    WebStorm 是 JetBrains 推出的一款商业的 JavaScript 开发工具 任何一个编辑器都需要保存(ctrl + s),这是所有win平台上编辑类软件的特点,但是webstorm编辑 ...

  9. js:|| 和 && 运算符 特殊用法

    引用:http://www.jb51.net/article/21339.htm 先总结一下: 几乎所有语言中||和&&都遵循“短路”原理,如&&中第一个表达式为假就不 ...

  10. python 和 mysql连接

    python 和 mysql连接 虫师教程:http://www.cnblogs.com/fnng/p/3565912.html 其他教程pymysql:http://www.cnblogs.com/ ...