UVALive 6088 Approximate Sorting 构造题
题目链接:点击打开链接
题意:
给定一个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 构造题的更多相关文章
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- hdu4671 Backup Plan ——构造题
link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- BZOJ 3097: Hash Killer I【构造题,思维题】
3097: Hash Killer I Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 963 Solved: 36 ...
- CF1110E Magic Stones(构造题)
这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...
- CDOJ 1288 旅游的Final柱 构造题
旅游的Final柱 题目连接: http://acm.uestc.edu.cn/#/problem/show/1288 Description 柱神要去打Final啦~(≧▽≦)/~啦啦啦 柱神来到了 ...
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建) 一.UITabBarController在storyoard中得搭建 1.新建一个项目,把storyb ...
- Android ExpandableListView使用+获取SIM卡状态信息
ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...
- 【Pyhton Network】使用poll()或select()实现非阻塞传输
通常情况下,socket上的I/O会阻塞.即除非操作结束,否则程序不会照常进行.而以下集中情况需要在非阻塞模式下进行:1. 网络接口在等待数据时是活动的,可以做出相应:2. 在不使用线程或进程的情况下 ...
- QQ输入法个人设置
常用 按键 外观 词库 账户 高级
- content:attr()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- js中test,exec和match方法
test test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c";var reg = new RegExp("1.&quo ...
- JS正则表达式验证数字非常全
<script type="text/javascript"> function validate(){ var reg = new RegExp("^[0- ...
- C# 操作 Word 修改word的高级属性中的自定义属性
为 Microsoft Word 创建自动化客户端 启动 Visual Studio .NET. 在文件菜单上,单击新建,然后单击项目.从 Visual C# 项目类型中选择 Windows 应用程序 ...
- (转载)PHP 下 CURL 通过 POST 提交表单失败的原因之一与解决办法
(转载)http://blog.renren.com/share/246611432/7511385884 前几天在学习使用 CURL 时遇到一个问题:在 a.php 中以 POST 方式向 b.ph ...
- By类的使用
举例,页面上有5个table,每个table都有标题栏和内容栏你觉的我是把每个table的标题都放到List里面遍历使用还是现指定table,在获取table的标题栏使用呢明显后面的方便不容易乱麽所 ...