Keyboard Purchase CodeForces - 1238E (状压)

时间:2022-09-17 00:13:45

大意: 给定串$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&lpar;状压DP 求图中环的个数&rpar;

    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 &lpar;状压&rpar;

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

  3. Clear The Matrix CodeForces - 903F &lpar;状压&rpar;

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

  4. Pollywog CodeForces - 917C &lpar;状压&rpar;

    链接 大意: 一共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 &lpar;状压&rpar;

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

随机推荐

  1. 从零开始学 ios 的一些建议 摘自http&colon;&sol;&sol;www&period;cocoachina&period;com&sol;ios&sol;20150826&sol;13151&period;html

    我是一只小菜鸟,今天在cocoaChina 看到一篇关于初学者,也就是零基础的童鞋的一些建议,感觉写的好好. 我觉得,学习真的是很累,但是,你要记得一句话,,世界上最最可怕的两个字是认真.共勉! 事情 ...

  2. iOS 串行网络请求。。。待研究

    nsurlsession 和 nsurlconnection 能实现吗? 手动实现的关键点在哪里? 我这里说的串行网络请求,指的是第一个网络请求不返回数据,第二个网络请求就不能开始. AFNetwor ...

  3. Appium for Mac 环境准备篇

    之前写过一篇Appium for windows的文章,因为是09年的T400,启动Android模拟器的时候死机三次,那就公司申请台Macbook air吧,15寸的Macbook Pro实在太重了 ...

  4. pointer

    https://en.wikipedia.org/wiki/Pointer_(computer_programming) In computer science, a pointer is a pro ...

  5. Qemu文档

    http://wiki.qemu.org/Manual http://qemu.weilnetz.de/qemu-doc.html http://www.linuxcertif.com/man/1/q ...

  6. 【UVA 10816】 Travel in Desert (最小瓶颈树&plus;最短路)

    [题意] 有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的. InputInput consists of ...

  7. c&num;第五次作业---正文提取

    1.正文文本 1.正文文本 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFC ...

  8. &lt&semi;META http-equiv&equals;Content-Type content&equals;&quot&semi;text&sol;html&semi; charset&equals;gb2312&quot&semi;&gt&semi;

    META,网页Html语言里Head区重要标签之一 HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有 用的信息,以帮助正确和精确地显示网页内容.常用的HTTP- EQUIV类型有: ...

  9. sublime 安装package control

    import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d15146761 ...

  10. SQLServer常用分页方式

    mysql的分页是基于limit关键字,oracle的分页是基于rownum行号,SQLserver的分页在下面进行研究,是基于SQLServer2012进行的测试. 0.原来的SQL的所有数据 下面 ...