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 ...
随机推荐
- 构造 Bubble Cup 8 - Finals D. Tablecity
题目传送门 题意:在1000*2的格子里,在每个小时能派出两个警察在两个地点搜查小偷,求在2015小时内能抓住小偷的方案. 分析:首先每次扫过一列即i1 i2从左往右扫,这样会漏掉小偷正好从间隙穿过的 ...
- 题解报告:hdu 1969 Pie(二分)
Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...
- Elipse 无法启动问题(转)
来源: <http://www.cnblogs.com/coding-way/archive/2012/10/17/2727481.html> 当选择完workspace之后,eclips ...
- jmeter(十八)属性和变量
一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...
- Linux下cpu过高问题排查
原文地址:https://blog.csdn.net/chenjunan888/article/details/80447800 在服务器报cpu过高时,可使用以下命令,快速导出堆栈信息,以方便查看具 ...
- ubuntu安装mysql多实例
想要尝试mysql的读写分离,在云上安装完mysql之后突然想到一个问题:我本机是没有公网IP的. 开始尝试在唯一一台云服务器上安装多个mysql实例. 主要步骤: 1.新建MySQL目录 (1):新 ...
- canvas基础绘制-绚丽倒计时
效果图: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- jsapi4加载的首个图层的范围被默认作为地图范围,且不能修改的解决
在map加载的第一个图层的图层范围(fullExtent),会被默认设置为map的全图范围,且不能更改,从一般地图控件角度来说,应该有fullExtent属性,作为地图的全图范围,但很遗憾jsapi4 ...
- 009全志R16平台tinav3.0下编译不过的问题
009全志R16平台tinav3.0下编译不过的问题 2018/11/13 11:39 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r16平台tinav3. ...
- Xilinx HLS
Xilinx 的高层次综合(High Level Synthesis, HLS)技术是将C/C++/SystemC软件语言转换成Verilog或VHDL硬件描述语言的技术.现已应用在SDAccel,S ...