大意: 给定串$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 (状压)的更多相关文章

  1. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  2. Vladik and cards CodeForces - 743E (状压)

    大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...

  3. Clear The Matrix CodeForces - 903F (状压)

    大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...

  4. Pollywog CodeForces - 917C (状压)

    链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...

  5. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  6. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

  7. Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...

  8. codeforces 1185G1 状压dp

    codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...

  9. Crisp String CodeForces - 1117F (状压)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

随机推荐

  1. string拼接时去掉最后一个逗号

     str.replace(str.length() - 1, str.length(), "");

  2. mysql 显示表结构

    例子 mysql> show columns from table1; +------------+------------------+------+-----+---------+----- ...

  3. location 浅解析

    https://www.baidu.com/s?ie=UTF-8&wd=sdasds location.href // 'https://www.baidu.com/s?ie=UTF-8&am ...

  4. devops 运维平台相关知识

    1.https://choerodon.io/zh/community/ (代码 https://github.com/choerodon/choerodon) 猪齿鱼 2.https://www.o ...

  5. mongodb启动命令与端口设置

    一.mongodb安装和配置 1.创建tools目录,用于存放安装包 cd /usr/local mkdir -p tools cd tools 2.下载mongodb包(其它版本请自行下载) wge ...

  6. Spring Boot 面试,一个问题就干趴下了!(下)

    前些天栈长在Java技术栈微信公众号分享一篇文章:Spring Boot 面试,一个问题就干趴下了!,看到大家的留言很精彩,特别是说"约定大于配置"的这两个玩家. 哈哈,上墙的朋友 ...

  7. skywalking安装运行(docker)

    https://github.com/apache/skywalking-docker/tree/master/6/6.5 https://hub.docker.com/r/apache/skywal ...

  8. Vintage、滚动率、迁移率的应用

    python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...

  9. vue---splitpane分割

    使用splitpane可以对窗口进行拆分,这个splitpane组件还是比较好用的, 首先安装: npm install vue-splitpane 引入使用: import splitPane fr ...

  10. IDEA 开发javafx: error: java:package javafx.application does not exist

    1)jdk使用1.8, 1.7中未包含javafx相关内容. 2)确保classpath中加入了javafx包路径. 在“file” --> "project structure&qu ...