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

猜你喜欢

转载自www.cnblogs.com/uid001/p/10623739.html