题目链接:点击打开链接

题意:

给定一个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. 李洪强iOS开发之-环信04_消息

    李洪强iOS开发之-环信04_消息 消息:IM 交互实体,在 SDK 中对应的类型是 EMMessage.EMMessage 由 EMMessageBody 组成. 构造消息   构造文字消息 EMT ...

  2. IB_DESIGNABLE的使用

    创建LHQTextField 继承自: UITextField 将我自定义的textField在面板中进行关联 此时,在设置刚来添加的属性的值的时候,就会立马出效果

  3. 使用NSURLSession获取网络数据和下载文件

    使用NSURLSession获取网络数据 使用NSURLSession下载文件

  4. ssh 密钥详解

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  5. yiic 数据库迁移工具

    数据库的结构也同源代码一样随着我们开发的进行而不断的发生着改变.在开发过程中,一般的我们需要像管理我们的源代码一样记录下数据库结构的整个变化过程,以便代码还原到指定版本后,数据库能同步的还原到指定的版 ...

  6. laravel route路由,视图和response和filter

    Laravel充分利用PHP 5.3的特性,使路由变得简单并富于表达性.这使得从构建API到完整的web应用都变得尽可能容易.路由的实现代码在 application/routes.php 文件. 和 ...

  7. 【原创】FPGA开发手记(二) VGA接口

    以下内容均以Xilinx的Nexys3作为开发板 1.VGA接口介绍 首先,先看电路图(3*5为例): 标准VGA一共15个接口,但是实际应用的接口信号只用五个:HSYNC,行同步信号:VSYNC,场 ...

  8. Android开发之通过Intent启动其他App的Service

    在Android5.0以前可以通过隐式Intent方式启动其他App的Service,就跟Activity启动隐式Intent一样的. 但是在5.0以后,只能使用显示的Intent方式启动了. 启动其 ...

  9. NESPER的大体结构 z

    NEsper从内容上分为两块,NEsper的核心NEsper.dll和NEsper.IO.dll. (1)NEsper的核心包包含了EPL语法解析引擎,事件监听机制,事件处理等核心模块. (2)NEs ...

  10. Dim Loop 出现结果不同

    (1)结果是:循环1次 Dim counter,num counter = 0 'num = 9 Do Until num = 10     num = num - 1     counter = c ...