题目链接:点击打开链接

题意:

给定一个n*n的01矩阵

我们跑一下例子==

4

0111

0000

0100

0110

0123

\|____

0|0111

1|0000

2|0100

3|0110

用0-n-1随便构造一个序列:

如:1230

我们计算1230的权值 :

int ans = 0;

对于个位0,我们查找第0行:0111,0前面的数有123, 则ans += a[0][1], ans+=a[0][2], ans+=a[0][3]

对于十位3。我们查找第3行:0110。3前面的数有12, ans+=a[3][1], ans+=a[3][2]

如此得到ans = 6

问:

构造一个这种序列使得ans最小,若有多个这种序列则输出序列字典序最小的。

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int Inf = 1e9;
typedef long long ll;
const int N = 18;
const int S = 1 << N;
int d[S], one[S];
int w[N], n, mx;
int out[N], dep;
map<int, int> id; int dp(int s) {
if (~d[s])
return d[s];
d[s] = Inf;
int i, to, x = s;
while (x>0) {
to = (x&(x-1)) ^ x;
i = id[to];
d[s] = min(d[s], dp(s^(1<<i)) + one[w[i]&s]);
x = x^to;
}
return d[s];
}
void dfs(int dep, int cur, int g) {
if (dep == N) {
one[cur] = g;
} else {
dfs(dep+1, cur*2+1, g+1);
dfs(dep+1, cur*2, g);
}
}
void path(int s) {
if (s != 0) {
for (int i = 0; i < n; ++i)
if (s >> i & 1) {
if (dp(s^(1 << i)) + one[w[i]&s] == dp(s) ) {
out[dep++] = i;
path(s^(1 <<i));
return ;
}
}
}
}
char s[N];
void work() {
memset(w, 0, sizeof w);
for (int i = 0; i < n; ++i) {
scanf("%s", s);
for (int j = 0; j < n; ++j)
if (s[j] == '1' && j != i)
w[j] |= 1 << i;
}
mx = (1 << n) - 1;
memset(d, -1, sizeof d);
d[0] = 0;
int ans = dp(mx);
dep = 0;
path(mx);
for (int i = 0; i < dep; ++i) {
if (i > 0)
putchar(' ');
printf("%d", out[i]);
}
puts("");
printf("%d\n", ans);
}
int main() {
dfs(0, 0, 0);
for (int i = 0; i < N; ++i)
id[1 << i] = i;
while (~scanf("%d", &n)) {
if (0 == n)
break;
work();
}
return 0;
}

UVALive 6088 Approximate Sorting 构造题的更多相关文章

  1. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  2. hdu4671 Backup Plan ——构造题

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...

  3. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  4. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  5. BZOJ 3097: Hash Killer I【构造题,思维题】

    3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 36 ...

  6. CF1110E Magic Stones(构造题)

    这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...

  7. CDOJ 1288 旅游的Final柱 构造题

    旅游的Final柱 题目连接: http://acm.uestc.edu.cn/#/problem/show/1288 Description 柱神要去打Final啦~(≧▽≦)/~啦啦啦 柱神来到了 ...

  8. CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...

  9. HDU 5355 Cake (WA后AC代码,具体解析,构造题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. Web开发-表单验证

    表单验证是Web开发中必不可少的一个环节,用来限制用户输入数据的规范和一致性.那么如何能够简化这一任务,让开发人员通过简单的属性设置就能达到目的呢? FineUI在这一点上也是下足了功夫,比Asp.N ...

  2. Altium designer入门篇-过孔不开窗

    有没有觉得在设计PCB的时候,放的过孔开窗了,在焊接实际PCB板子的时候,会有各种锡尖,拖锡尾巴,严重的网络间短路.此经验简述了使用Altium designer软件,让过孔不开窗的设置办法.初学者可 ...

  3. 在redhat6.4下安装 Oracle® Database 11g Release 2

    OS版本: 安装过程的相关信息: pdksh 安装好后根据需要设置oracle开机自启动http://www.cnblogs.com/softidea/p/3761671.html 设置环境变量NLS ...

  4. C语言动态内存管理

    1-概述 动态存储管理的基本问题是:系统如何按请求分配内存,如何回收内存再利用.提出请求的用户可能是系统的一个作业,也可能是程序中的一个变量. 空闲块 未曾分配的地址连续的内存区称为“空闲块”. 占用 ...

  5. !! python 之半年总结

    http://blog.chinaunix.net/uid-26443921-id-3481357.html 半年前开始系统完整深入的了解学习 python 读书篇: <python 核心编程2 ...

  6. SSH框架中配置Hibernate使用proxool连接池

    一.导入proxool.jar包 案例用的是proxool-0.8.3.jar,一般通过MyEclipse配置的SSH都会包含这个jar,如果没有,就去网上搜下下载导入就好了. 二.新建Proxool ...

  7. OpenCV学习笔记:矩阵的掩码操作

    矩阵的掩码操作很简单.其思想是:根据掩码矩阵(也称作核)重新计算图像中每个像素的值.掩码矩阵中的值表示近邻像素值(包括该像素自身的值)对新像素值有多大影响.从数学观点看,我们用自己设置的权值,对像素邻 ...

  8. poj3368Frequent values(RMQ)

    http://poj.org/problem?id=3368 追完韩剧 想起这题来了 想用线段树搞定来着 结果没想出来..然后想RMQ 想出来了 算是离散吧 把每个数出现的次数以及开始的位置及结束的位 ...

  9. windows权限错误

    1.installer "内部错误 2203 C:\WINDOWS\Installer\354787.ipi,-2147287035” 用户的local\Temp目录没有system权限 2 ...

  10. 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错

    原文网址:https://support.apple.com/kb/PH13808?locale=zh_CN&viewlocale=zh_CN 某些 Mac 电脑将在不活跃一段时间后自动进入睡 ...