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) ...
随机推荐
- easyui源码翻译1.32--Droppable(放置)
前言 使用$.fn.droppable.defaults重写默认值对象.下载该插件翻译源码 源码 /** * jQuery EasyUI 1.3.2 * *翻译:lbq --放置 拉伸 */ (fun ...
- I2S和PCM
http://blog.csdn.net/azloong/article/details/6536855
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut
一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...
- iOS上获得MAC地址
很多时候我们都需要唯一来确定一台设备,苹果设备本来有个UDID号,可以实现这个目的.在iOS5.0以前,还有一个uniqueIdentifier的API用来获得这个number.不过iOS5之后,这个 ...
- 【HDOJ】2828 Lamp
DLX简单题目. /* */ #include <iostream> #include <sstream> #include <string> #include & ...
- BZOJ_2179_FFT快速傅立叶_(FFT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2179 超大整数乘法 分析 FFT模板题. 把数字看成是多项式,x是10.然后用FFT做多项式乘 ...
- iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”
iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...
- [WebKit]浏览器的加载与页面性能优化
非常棒.非常系统的一份资料,值得阅读! 原文来自百度泛用户体验. 作者:nwind 本文将探讨浏览器渲染的loading过程,主要有2个目的: 了解浏览器在loading过程中的实现细节,具体都做了什 ...
- C# word开发
c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中, ...
- 一步步写STM32 OS【二】环境搭建
一.安装IAR for ARM6.5 二.新建工程 1.选择处理器:STM32F407VG,暂不使用FPU 2.必要的路径配置和宏定义 3.使用SWO重定向IO输出 4.使用ST-LINK仿真器 5. ...