您的位置:首页 > 大数据 > 人工智能

【HDOJ】4317 Unfair Nim

2016-02-09 23:09 543 查看
基本的状态压缩,想明白怎么dp还是挺简单的。
显然对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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: