Shut the Box HDU4212

http://acm.hdu.edu.cn/showproblem.php?pid=4212

论文写烦了,水了一道很水的状压DP,状态很稀疏用map搞下,预处理很关键。


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <algorithm>
#include <utility>
#include <list>
#include <fstream>
#include <cctype>
#include <vector>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(300010);
const int MAXM(20010);
const int MAXE(210);
const int MAXK(6);
const int HSIZE(313131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}

struct HASH_MAP{
	int first[HSIZE];
	int state[MAXN], val[MAXN], next[MAXN], size;
	void init(){
		memset(first, -1, sizeof(first));
		size = 0;
	}
	void insert(int ts, int tv){
		int h = ts%HSIZE;
		for(int i = first[h]; ~i; i = next[i])
			if(ts == state[i])
				return;
		state[size] = ts;
		val[size] = tv;
		next[size] = first[h];
		first[h] = size++;
	}
} hm[2], *cur, *last;

vector<PAIR> vec[23][23];

void dfs(int dep, int st, int v, int cnt){
	if(v > 22) return;
	if(v > 0) vec[dep-1][v].push_back(PAIR(st, cnt));
	if(dep == 23) return;
	dfs(dep+1, st, v, cnt);
	dfs(dep+1, st|(1 << dep), v+dep, cnt+1);
}

int main(){
	dfs(1, 0, 0, 0);
	int N, T, n_case(0);
	while(~scanf("%d%d", &N, &T), N+T){
		vector<PAIR> *vp = vec[N];
		cur = hm;
		last = hm+1;
		last->init();
		last->insert(0, 0);
		int ans = 0;
		int temp;
		for(int i = 0; i < T; ++i){
			scanf("%d", &temp);
			cur->init();
			for(vector<PAIR>::iterator it = vp[temp].begin(); it != vp[temp].end(); ++it){
				int sz = last->size;
				for(int j = 0; j < sz; ++j)
					if((last->state[j]&it->first) == 0){
						int tv = last->val[j]+it->second;
						checkmax(ans, tv);
						cur->insert(last->state[j]+it->first, tv);
					}
			}
			swap(cur, last);
		}
		printf("Game %d: %d\n", ++n_case, ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值