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) ... 
随机推荐
- ANDROID_MARS学习笔记_S02_002_Date\TimePicker
			一.文档用法 1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... 
- ☀【组件】字符串 string
			<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ... 
- Android Activity管理类
			public class AppManager { private static Stack<Activity> activityStack; private static AppMana ... 
- Android Matrix用法
			Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ... 
- 关于offset()的理解
			假如要取得x线的offset().top,在页面上的是x线到页面顶端的距离s1,如何取得x线在窗口上的top呢,这就需要取得窗口顶端到页面顶端的距离s2,由于s1和s2都是有方向的,所以,s1-s2就 ... 
- STL总结之bitset
			STL的bitset是一个对位进行存储和操作的容器,可以轻松对bit位进行访问. bitset的模板声明如下: template<size_t _Bits> class bitset; ... 
- bzoj 2434 [Noi2011]阿狸的打字机(fail树+离线处理+BIT)
			[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题意] 按照一定规则生成n个字符串,回答若干个询问:(x,y),问第x个字符串 ... 
- 《Android View 的事件分发和滑动冲突》 —预习资料
			1. 阅读书籍<Android开发艺术探索>第三章 2. 提前阅读如下技术文章: http://blog.csdn.net/singwhatiwanna/article/details/3 ... 
- Weka 入门1
			本人也是借鉴网上他人资料.主要介绍使用java调用Weka库. 首先介绍weka,Weka的全名是怀卡托智能分析环境,是基于开源环境的机器学习和数据挖掘软件.我们可以去weka官网下载最新的Weka软 ... 
- Tuple元组
			Tuple元组 Tuple 是 Storm 的主要数据结构,并且是 Storm 中使用的最基本单元.数据模型和元组. Tuple 描述 Tuple 就是一个值列表, Tuple 中的值可以是任何类型的 ... 
