bnu 34981 A Matrix(构造)
来源:互联网 发布:精细建筑模型 知乎 编辑:程序博客网 时间:2024/06/05 20:26
题目链接:bnu 34981 A Matrix
题目大意:假定有一序列,按照题目中给定的算法构造出一张二维表,现在题目给定一张二维表,要求求出序列,要求序列的倒置的字典序最大。
解题思路:构造,对于每一层来说,一定是递增的,根据算法可以得出;并且一个数被换到下一行,一定是因为有序列后面有小于自己的数,所以每一层从最后一个数开始匹配,找到上一层中比自己小的最大数字,假定是该数导致当前数被换到下一行,注意一个数只能让一个数被换到下一行。所以有几种是找不到对应序列,要输出-1.
- 一行中的数不是递增的。
- 当前行的数不能一一对应上一行的数
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 1e5+5;bool flag;int n, m, c, mv, f[N], r[N], ans[N];vector<int> g[N];int getfar(int x) { return x == f[x] ? x : f[x] = getfar(f[x]);}void init () { scanf("%d%d", &n, &m); flag = false; for (int i = 0; i <= n; i++) r[i] = f[i] = i; for (int i = 0; i < m; i++) g[i].clear(); int t; for (int i = 0; i < m; i++) { scanf("%d", &t); int a, pre = 0; for (int j = 0; j < t; j++) { scanf("%d", &a); g[i].push_back(a); if (a < pre) flag = true; pre = a; } }}bool insert (int x, int d) { for (int j = mv-1; j >= 0; j--) { if (g[d][j] < x) { int p = getfar(g[d][j]); f[p] = x; r[p] = x; mv = j; return true; } } return false;}void put(int x) { ans[c--] = x; if (r[x] != x) put(r[x]);}void solve () { for (int i = m-1; i; i--) { int t = g[i].size(); mv = g[i-1].size(); for (int j = t-1; j >= 0; j--) if (!insert(g[i][j], i-1)) { flag = true; return; } } c = n; int t = g[0].size(); for (int i = t-1; i >= 0; i--) put(g[0][i]);}int main () { int cas; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { init (); printf("Case #%d: ", i); solve(); if (flag) { printf("No solution\n"); } else { for (int j = 1; j < n; j++) printf("%d ", ans[j]); printf("%d\n", ans[n]); } } return 0;}
1 0
- bnu 34981 A Matrix(构造)
- 2014 BNU 邀请赛A题(构造问题)
- BNU-Binary Matrix- 贪心
- BNUOJ 34981 A Matrix
- BUN 34981 A Matrix
- 2014北京邀请赛 A:A Matrix 构造+贪心
- BNU 0815 A Army Buddies
- BNU 0814 A. Team building
- BNU 0822 A. Farmer Greedy
- fzu1911 (Construct a Matrix) 矩阵快速幂+构造
- Codeforces 364 A Matrix 题解(矩形构造)
- FZU 1911 Construct a Matrix(矩阵快速幂+构造)
- CodeForces 803A Maximal Binary Matrix-【思维+构造】
- BNU
- BNU 0817 A 0 or 1
- BNU A Simple Tree Problem 线段树
- BNU Problem A Best Matched Pair
- [CodeForces365C]Matrix[构造][数学]
- LINUX总结第8篇:Ubuntu下搭建java开发环境
- UVA 756 - Biorhythms(数论)
- myeclipse导入项目项目名上有个红叉,但代码没有报错
- Qt学习之路(1):前言
- android网上服装购物系统
- bnu 34981 A Matrix(构造)
- 【数字图像处理之(一)】数字图像处理与相关领域概述
- 黑马程序员--循环语句while for
- Qt学习之路(2):Hello, world!
- 5.qemu-kvm 网络配置
- 进度有点慢
- 金山毒霸2002界面
- Qt学习之路(3):Hello, world!(续)
- 在源码中编译自己的 Android project