【HDOJ】4317 Unfair Nim
2016-02-09 23:09
543 查看
基本的状态压缩,想明白怎么dp还是挺简单的。
显然对n个数字进行状态压缩,dp[i][j]表示第i位状态j表示的位向高位产生了进位。
数据发生器。
显然对n个数字进行状态压缩,dp[i][j]表示第i位状态j表示的位向高位产生了进位。
/* 4317 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <algorithm> #include <cstdio> #include <cmath> #include <ctime> #include <cstring> #include <climits> #include <cctype> #include <cassert> #include <functional> #include <iterator> #include <iomanip> using namespace std; //#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int> #define stpii set<pair<int, int> > #define mpii map<int,int> #define vi vector<int> #define pii pair<int,int> #define vpii vector<pair<int,int> > #define rep(i, a, n) for (int i=a;i<n;++i) #define per(i, a, n) for (int i=n-1;i>=a;--i) #define clr clear #define pb push_back #define mp make_pair #define fir first #define sec second #define all(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define lson l, mid, rt<<1 #define rson mid+1, r, rt<<1|1 const int INF = 0x3f3f3f3f; int dp[23][(1<<10)+5]; int Bit[23]; int a[15]; int C[(1<<10)+5]; int n; int getBit(int x) { int ret = 0; while (x) { ret += (x & 1); x >>= 1; } return ret; } void Init() { int mst = 1<<10; rep(i, 0, mst) C[i] = getBit(i); } void solve() { rep(j, 0, 22) { Bit[j] = 0; rep(i, 0, n) { if (a[i] & (1<<j)) Bit[j] |= (1<<i); } } memset(dp, INF, sizeof(dp)); dp[0][0] = 0; int mst = 1<<n; int mask = mst - 1; int ans, tmp; rep(i, 0, 22) { rep(j, 0, mst) { if (dp[i][j] == INF) continue; int ov = j & Bit[i]; int one = Bit[i] ^ j; int zero = mask & ~one; rep(k, 0, mst) { if (ov & ~k) continue; int other_ov = k & ~ov; if (other_ov & zero) continue; tmp = C[other_ov]; int val = one ^ other_ov; if (C[val] & 1) { if (val == mask) continue; ++tmp; } tmp <<= i; dp[i+1][k] = min(dp[i+1][k], tmp+dp[i][j]); } } } ans = dp[22][0]; if (ans == INF) puts("impossible"); else printf("%d\n", ans); } int main() { ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif Init(); while (scanf("%d", &n)!=EOF) { rep(i, 0, n) scanf("%d", &a[i]); solve(); } #ifndef ONLINE_JUDGE printf("time = %d.\n", (int)clock()); #endif return 0; }
数据发生器。
from copy import deepcopy from random import randint, shuffle import shutil import string def GenDataIn(): with open("data.in", "w") as fout: t = 20 bound = 10**6 # fout.write("%d\n" % (t)) for tt in xrange(t): n = randint(1, 10) fout.write("%d\n" % (n)) dataList = [] for i in xrange(n): x = randint(1, bound) dataList.append(x) fout.write(" ".join(map(str, dataList)) + "\n") def MovDataIn(): desFileName = "F:\eclipse_prj\workspace\hdoj\data.in" shutil.copyfile("data.in", desFileName) if __name__ == "__main__": GenDataIn() MovDataIn()
相关文章推荐
- Matrix Chain Multiplication UVA 442(栈+表达式求值)
- Mina airQQ聊天 服务端篇(二)
- Obtaining Disk Space Information获取磁盘空间信息
- jetbrains
- HDU 3452 Bonsai
- Your project contains error(s)
- YTU 2547: Repairing a Road
- Aireplay-ng的6种攻击模式详解
- 深入理解Aireplay-ng各种攻击及其原理
- 例题6-3 UVA 442 Martrix Chain Multiplication矩阵链乘
- AIM Tech Round (Div. 2) B
- AIM Tech Round (Div. 2) A
- HDU-1023 Train Problem II 出栈序列问题
- hdu1789 Doing Homework again(贪心)
- HDU 1022 Train Problem I
- USACO--Barn Pepair
- LightOJ 1028 1028 - Trailing Zeroes (I) (求因子个数)
- Failed to load native-hadoop本地库不一致的解决办法
- NetworkOnMainThreadException
- Vim简明教程【CoolShell】转自http://blog.csdn.net/niushuai666/article/details/7275406 (飘过的小牛)