Keyboard Purchase CodeForces - 1238E (状压)
大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小.
状压$dp$, 费用提前计算一下, 预处理$cost_{i,j}$表示与字符$i$相连的状态为$j$时的方案数
总复杂度是$O(n 2^n)$
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+50;
int n, m;
char s[N];
int cnt[20][20],Log[1<<20];
int dp[1<<20], cost[20][1<<20];
void chkmin(int &a, int b) {a>b?a=b:0;} int main() {
printf("%d\n",n);
scanf("%d%d%s",&n,&m,s+1);
REP(i,2,n) {
int a=s[i-1]-'a',b=s[i]-'a';
++cnt[a][b],++cnt[b][a];
}
REP(i,0,m-1) Log[1<<i] = i;
int mx = (1<<m)-1;
REP(i,0,m-1) REP(j,1,mx) cost[i][j]=cost[i][j^j&-j]+cnt[i][Log[j&-j]];
memset(dp,0x3f,sizeof dp);
dp[0] = 0;
REP(i,0,mx) {
int c = 0, s = ~i&mx;
REP(j,0,m-1) if (i>>j&1) c += cost[j][s];
REP(j,0,m-1) if (i>>j&1^1) {
chkmin(dp[i^1<<j],dp[i]+c+cost[j][s^1<<j]-cost[j][i]);
}
}
printf("%d\n", dp[mx]);
}
Keyboard Purchase CodeForces - 1238E (状压)的更多相关文章
- CodeForces 11D(状压DP 求图中环的个数)
		
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...
 - Vladik and cards CodeForces - 743E (状压)
		
大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...
 - Clear The Matrix CodeForces - 903F (状压)
		
大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...
 - Pollywog CodeForces - 917C (状压)
		
链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...
 - Codeforces 678E 状压DP
		
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
 - Codeforces 8C 状压DP
		
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
 - Codeforces 1215E 状压DP
		
题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...
 - codeforces 1185G1 状压dp
		
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...
 - Crisp String CodeForces - 1117F (状压)
		
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...
 
随机推荐
- MongoDB 高级查询_aggregate聚合管道
			
MongoDB 聚合管道(AggregationPipeline) 使用聚合管道可以对集合中的文档进行变换和组合.实际项目应用主要是表关联查询.数据的统计. MongoDB 中使用 db.COLLEC ...
 - Java基础 三目运算符 用if-else对其进行解释
			
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
 - odoo开发笔记 -- 多个子类继承同一个父类方法的执行顺序
			
场景描述: odoo模块化开发的架构理念,科学&高效, 可以让很多业务场景,尽可能松耦合:让开发人员的主要精力,关注在当前的业务逻辑: 所谓「前人栽树,后人乘凉」,模块整体好比一棵大树, 开发 ...
 - Linux MySQL 5.6.43 安装
			
[注意] 1.首先安装在默认目录 /usr/local/mysql,如需更改数据存储目录,进行2.3两步 2.如果需要修改数据目录,将my.nf 中的 datadir=/usr/local/mysql ...
 - Js ascii 16进制 url-encode
			
function fixedEncodeURIComponent (str) { return str.replace(/./g, function(c) { return '%' + c.charC ...
 - [译]如何在红帽系统(RHEL)上源码安装python3?
			
原文来源: https://stackoverflow.com/questions/8087184/installing-python-3-on-rhel 很容易手动安装. 1.下载对应的python ...
 - 【翻译】Flink Table Api & SQL —Streaming 概念 ——动态表
			
本文翻译自官网:Flink Table Api & SQL 动态表 https://ci.apache.org/projects/flink/flink-docs-release-1.9/de ...
 - [LeetCode] 73. Set Matrix Zeroes 矩阵赋零
			
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
 - shell语法学习
			
[原文] 菜鸟笔记shell教程学习. 本篇博客只是记录shell的一些关键语法,主要是做一个记录,有些内容也是copy过来的,并不是一个完整的教程,想完整学习shell的同学可以前往 shell脚本 ...
 - 转载:Python Web开发最难懂的WSGI协议,到底包含哪些内容?
			
原文:PSC推出的第二篇文章-<Python Web开发最难懂的WSGI协议,到底包含哪些内容?>-2017.9.27 我想大部分Python开发者最先接触到的方向是WEB方向(因为总是有 ...