HDU 5076 Memory
Memory
This problem will be judged on HDU. Original ID: 5076
64-bit integer IO format: %I64d Java class name: Main
Now he wants to assign a value to each byte of the memory. In this problem, a byte is composed of m bits. Therefore he can only assign 0 to 2m - 1 (inclusive) to a single byte.
Bob has some preferences on which value to be assigned to each byte. For the byte indexed by i, if it is assigned with value j (0 ≤ j < 2m), the preference score for it is wi,j.
In addition, each byte has a threshold value. For two different bytes indexed by a and b, if the following two conditions are satisfied, there will be an additional score (ua xor ub):
1.a and b only have one bit of difference in their binary forms;
2.The assigned value of byte a is not less than its threshold value, or the assigned value of byte b is not less than its threshold value.
The total score of an assignment solution is the sum of the preference scores of all bytes plus the sum of all additional scores.
Bob wants to find an assignment solution with the maximum total score. If there are multiple solutions, you can output any of them.
Input
For each test case, the first line contains two integers, n and m(1 ≤ n, m ≤ 8), as mentioned above. The second line contains 2n integers, and the i-th integer is the threshold value for byte i. The threshold values are between 0 and 2m - 1, inclusively. The third line contains 2n integers, and the i-th integer is ui(0 ≤ ui < 1024). The next 2n lines give all preference scores. Each line contains 2m integers, and the j-th integer of the i-th line is wi,j (-1024 ≤ wi,j < 1024).
Output
Sample Input
1
3 2
0 1 1 3 3 0 3 3
4 8 8 7 0 9 2 9
-9 -8 3 2
-9 -6 4 1
-6 -8 -5 3
3 -1 -4 -1
-6 -5 1 10
-10 7 3 -10
-3 -10 -4 -5
-2 -1 -9 1
Sample Output
2 2 3 0 3 1 0 3
Source
- 对于每个位置拆成两个点,左边源右边汇。
- 如果这个位置的index有奇数个1,左边连小于的w,右边连大于等于的w。
- 如果这个位置的index有偶数个1,左边连大于等于的w,右边连小于的w。
- 每个位置左边往右边连一条inf的弧,代表这两个点不能都不割。
- 对于每组a,b,从奇数的小于连向偶数的小于,ua xor ub。
- 为了避免负流量可以把所有w加一个1024,不影响最后结果。
- 总之建出来是个二分图。
- 最后建完就可以发现其实可以不用拆点,但拆了还是更好理解一些。
最后所有的收益加起来减掉最小割就是最大收益。
据说上面是昂神分析的
#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn*maxn];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs() {
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low) {
if(u == T) return low;
int a,tmp = ;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow &&d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(low,e[i].flow)))) {
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(int ret = ) {
while(bfs()) {
memcpy(cur,head,sizeof cur);
ret += dfs(S,INF);
}
return ret;
}
int U[maxn],Th[maxn],B[maxn],L[maxn],ans[maxn],Bid[maxn],Lid[maxn];
int main() {
int kase,n,m;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
n = (<<n);
m = (<<m);
for(int i = tot = ; i < n; ++i)
scanf("%d",Th + i);
for(int i = ; i < n; ++i)
scanf("%d",U + i);
for(int i = ; i < n; ++i) {
B[i] = L[i] = -;
for(int j = ,w; j < m; ++j) {
scanf("%d",&w);
w += ;
if(j >= Th[i] && B[i] < w) {
B[i] = w;
Bid[i] = j;
} else if(L[i] < w) {
L[i] = w;
Lid[i] = j;
}
}
}
S = n<<;
T = S + ;
memset(head,-,sizeof head);
for(int i = ; i < n; ++i) {
int k = __builtin_popcount(i);
add(i,i + n,INF);
if(k&) {
add(S,i,L[i]);
add(i + n,T,B[i]);
} else {
add(S,i,B[i]);
add(i + n,T,L[i]);
}
for(int j = i + ; j < n; ++j) {
if(__builtin_popcount(i^j) == ) {
if(k&) add(i,j + n,U[i]^U[j]);
else add(j,i + n,U[i]^U[j]);
}
}
}
dinic();
for(int i = ; i < n; ++i) {
if(i) putchar(' ');
if(__builtin_popcount(i)&)
printf("%d",(~d[i])?Lid[i]:Bid[i]);
else printf("%d",(~d[i])?Bid[i]:Lid[i]);
}
putchar('\n');
}
return ;
}
HDU 5076 Memory的更多相关文章
- hdu 2871 Memory Control(伸展树splay tree)
hdu 2871 Memory Control 题意:就是对一个区间的四种操作,NEW x,占据最左边的连续的x个单元,Free x 把x单元所占的连续区间清空 , Get x 把第x次占据的区间输出 ...
- hdu 2871 Memory Control(线段树)
题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放全部空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内 ...
- hdu 2871 Memory Control (区间合并 连续段的起始位置 点所属段的左右端点)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2871 题意: 四种操作: 1.Reset 清空所有内存2.New x 分配一个大小为x的内存块返回,返 ...
- HDU 2871 Memory Control
一共4种操作 其中用线段树 区间合并,来维护连续空的长度,和找出那个位置.其他用vector维护即可 #include<cstring> #include<cstdio> #i ...
- hdu 5076 最小割灵活运用
这意味着更复杂的问题,关键的事实被抽象出来:每个点,能够赋予既有的值(挑两个一.需要选择,设定ai,bi). 寻找所有和最大.有条件:如果两个点同时满足: 1,:二进制只是有一个不同之处. 2:中的 ...
- ●HDU 2871 Memory Control(Splay)
●赘述题目 四种操作: ○Reset:将整个内存序列清空. ○New a:在尽量靠左的位置新建一个长度为a的内存块,并输出改内存块起始位置.(各个内存块即使相邻也不会合并..) ○Free a:将a点 ...
- HDU 2871"Memory Control"(线段树区间和并+set.lower_bound)
传送门 •题意 有 n 个内存单元(编号从1开始): 给出 4 种操作: (1)Reset :表示把所有的内存清空,然后输出 "Reset Now". (2)New x :表示申请 ...
- 【NX二次开发】NX内部函数,libugui.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: bool A ...
- hdu 3007 Buried memory 最远点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...
随机推荐
- 优先队列 POJ 2431 Expedition
题目传送门 题意:一辆卡车要行驶L长度,初始有P油,每行驶一个单位长度消耗一单位油.有n个加油站可以加油,问最少加油几次才能行驶L长度,如果不能输出-1 分析:按照挑战书的解法,每走到一个加油站相当于 ...
- 面试王牌 JAVA并发
Java 并发 JavathreadSocketC#C++ 并发 Table of Contents 1 什么是并发问题. 2多线程死锁问题 2 java中synchronized的用法 3 Java ...
- CoreData修改了数据模型报错 The model used to open the store is incompatible with the one used to create the store
在iOS 6 – Core Data 应用程序的开发过程中, App启动时出现如下异常信息: reason = “The model used to open the store is incompa ...
- servlet生命周期:
Servlet生命周期分为三个阶段: 1,初始化阶段 servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...
- Spark SQL catalyst概述和SQL Parser的具体实现
之前已经对spark core做了较为深入的解读,在如今SQL大行其道的背景下,spark中的SQL不仅在离线batch处理中使用广泛,structured streamming的实现也严重依赖spa ...
- datetime 模块详解
1.import datetime 常用方法: ttimedelta() 括号里默认为days,进行别的单位运算可以加上如hours = 1这样.除了进行减法运算,还可以进行加法运算. >> ...
- currentStyle getComputedStyle兼容
function getStyle(obj,attr){ if(obj.currentStyle) {return obj.currentStyle[attr]} else{ return getCo ...
- jq获取设置选中值
var standard = $('input[name="standard"]:checked').val(); $("input[name='advertByid'] ...
- Linux 从源码编译安装 Nginx
Nginx 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器.Nginx 编译安装比较简单,难点在于配置.下面是 Nignx 0.8.54 编译安装和简 ...
- QTableWidget表头样式
转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...