1. 程式人生 > >Hongcow Buys a Deck of Cards CodeForces - 744C (狀壓)

Hongcow Buys a Deck of Cards CodeForces - 744C (狀壓)

oid sca ios define code scanf ring for algo

大意: 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);
}

Hongcow Buys a Deck of Cards CodeForces - 744C (狀壓)