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>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#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‘
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;}
//head

const int N = 20;
int n;
char c[N];
int x[N], y[N];
int dp[1<<16][150];
void chkmax(int &a, int b) {a=max(a,b);}
int main() {
	scanf("%d", &n);
	REP(i,0,n-1) scanf(" %c%d%d", c+i, x+i, y+i);
	memset(dp, 0xbc, sizeof dp);
	dp[0][0] = 0;
	int mx = (1<<n)-1;
	REP(i,0,mx-1) {
		int A = 0, B = 0;
		REP(k,0,n-1) if (i>>k&1) {
			if (c[k]==‘R‘) ++A;
			else ++B;
		}
		REP(k,0,n-1) if (!(i>>k&1)) {
			REP(j,0,120) if (dp[i][j]!=0xbcbcbcbc) {
				chkmax(dp[i^1<<k][j+min(x[k], A)],dp[i][j]+min(y[k], B));
			}
		}
	}
	int ans = INF, X = 0, Y = 0;
	REP(i,0,n-1) X+=x[i], Y+=y[i];
	REP(i,0,120) ans = min(ans, max(X-i, Y-dp[mx][i]));
	printf("%d\n", ans+n);
}

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

时间: 2024-08-29 22:39:52

Hongcow Buys a Deck of Cards CodeForces - 744C (状压)的相关文章

Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair&

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>

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 482c 状压+概率DP

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

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", &

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 8C 状压DP

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

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

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> #inc