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]);
}

原文地址:https://www.cnblogs.com/uid001/p/11644921.html

时间: 2024-08-30 16:06:10

Keyboard Purchase CodeForces - 1238E (状压)的相关文章

codeforces 580d 状压DP

题意:有n种菜,现在选m种菜来吃,如果在吃y的前一道菜是x的话,那么就可以获得额外满意度.每一种菜都有一个满意度. 思路:设dp[i][S]表示为最后一道菜为i,现在的菜吃的状态为S.S中的二进位如果为1表示已经吃了,如果是0则表示没吃,状压DP,答案就出了. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm>

codeforces 482c 状压+概率DP

题意:给出N个不同的串,长度一样,别人随机选一个串,你要询问他那个串某一个位置是什么字符直到能确定那个串才能停止,问询问次数的期望. 题解:50个串20个位置容易想到状压,把字符串长度状压先考虑能否在某一个状态确定哪些字符串能确定哪些不能确定,需要2^m*m次,然后时间上不能再乘以n不然会爆,想想只要我知道到达某一个猜位置状态的概率dp[i],再知道相对应有哪些字符串可以确定和不可以确定,用f[i]来表示,那么对于不能确定的字符串相当于就要再猜一步,那么加上这个状态的概率就行了,不会再需要乘以n

Codeforces 8C 状压DP

题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不能暂时放在地上.问最小的花费是多少?花费是笛卡尔距离的平方. 思路一看n 只有24,应该很容易想到要用状压DP. 那么dp[i]表示i状态并且回到原点的最小花费.那么就暴力枚举拿1个或两个物品放回原点,然后转移就行了.需注意,这个题目中拿物品的顺序对答案无影响,比如先拿1号,再拿2号和先拿2号,再拿1号的

2017-03-18 HDU 5733 计算几何 codeforces 599E 状压dp(待补)

HDU 5733 题意:给出四面体的四个顶点,求出其内切球的球心坐标和半径,如果不存在内切球,输出"O O O O". tags:一堆公式..可以做模板了 我们可以将平面上的四点得到由同一个点出发的三个矢量.这样就可以计算这三个矢量的混合积M,则M/6即为四面体体积V. 题目无解的情况当且仅当四点共面,即混合积为0. 求得四面体体积后,可以根据公式r = 3V/(S1+S2+S3+S4)得到内切球半径, S1~S4为四面体四个面的面积. 当前的问题转化为如何求四面体四个面的面积. 由于

Codeforces - 71E 状压DP

参考官方题解 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rrep(i,j,k) for(register int i=j;i>=k;i--) using namespace std; string s[100]={"H","He","Li","Be","B", &

Hongcow Buys a Deck of Cards CodeForces - 744C (状压)

大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑卡数, 求买完所有卡最少天数. 这题挺巧妙的, 刚开始看花费的范围太大一直在想怎么贪心... 实际上注意到减费最多只有120, 可以按照减费进行dp即可 这题CF大神的最优解写了个模拟退火ORZ #include <iostream> #include <algorithm> #inc

Crisp String CodeForces - 1117F (状压)

#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #define REP(i,a,n) for(int i=a;i<=n;

codeforces 1185G1 状压dp

include include include include include include include include include include include include include include include using namespace std; typedef long long LL; typedef long long ll; typedef pair<LL, LL> pLL; typedef pair<LL, int> pLi; typed

Codeforces 453B Little Pony and Harmony Chest(状压)

题目链接:Codeforces 453B Little Pony and Harmony Chest 题目大意:给定一个序列a, 求一序列b,要求∑|ai?bi|最小.并且b中任意两数的最大公约束为1. 解题思路:因为b中不可能含有相同的因子,所以每个素数只能使用1次.又因为说ai最大为30,所以素数只需要考虑到57即可.因为即使对于30而言,59和1的代价是一样的. 所以有dp[i][j]表示的是到第i个数,使用过的素数j. #include <cstdio> #include <cs