bnu 34982 Beautiful Garden(暴力)
题目链接:bnu 34982 Beautiful Garden
题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数。
解题思路:算是纯暴力吧。枚举等差的起始和中间一点,由于要求定中间一点的位置。所以这一步是o(n3);然后用o(n)的算法确定说须要移动几个来保证序列等差。
#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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
bnu 34982 Beautiful Garden(暴力)的更多相关文章
- BNUOJ 34982 Beautiful Garden
BNUOJ 34982 Beautiful Garden 题目地址:BNUOJ 34982 题意: 看错题意纠结了好久... 在坐标轴上有一些树,如今要又一次排列这些树,使得相邻的树之间间距相等. ...
- 牛客多校第四场 F Beautiful Garden
链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...
- (第四场)F Beautiful Garden
题目: F Beautiful Garden 题目描述 There's a beautiful garden whose size is n x m in Chiaki's house. The ga ...
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
1057: Beautiful Garden Time Limit: 5 Sec Memory Limit: 128 MB Submit: 25 Solved: 12 [Submit][Statu ...
- Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力
B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...
- 2014 SummerTrain Beautiful Garden
There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the ...
- 牛客网暑期ACM多校训练营(第四场) F Beautiful Garden
链接: https://www.nowcoder.com/acm/contest/142/F 题意: n x m的矩形,选个p x q的矩形去掉,两个矩形中⼼重合,去掉后的矩形上下左右对称 求(p, ...
- 北京邀请赛 B. Beautiful Garden
题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,近期水的要死,看了队友的代码做的 #include &l ...
- HDU5977 Garden of Eden(树的点分治)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...
随机推荐
- Max Sum (hdu 1003 简单DP水过)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 收藏的Android很好用的组件或者框架。
收藏的Android很好用的组件或者框架. android框架 先说两个站点: http://www.androidviews.net/ 非常好的国外开源码站,就是訪问速度有点慢啊 http://w ...
- Java数据结构系列之——栈(2):栈的链式存储结构及其操作
package Stack; import LinkList.SinglyLinkList; public class LinkListStack { private SinglyLinkList&l ...
- oracle分区表运行计划
分区表有非常多优点,以大化小,一小化了,加上并行的使用,在loap中能往往能提高几十倍甚至几百倍的效果. 当然表设计得不好也会适得其反.效果比普通表跟糟糕. 为了更好的使用分区表,这里看一下分区表的运 ...
- Add/Remove listview web part in publish site via powershell
1. Here is the code: Add WebPart in Publish Site Example : AddWebPartPublish http://localhost " ...
- OpenCV Haar AdaBoost源代码改进(比EMCV快6倍)
这几天研究了OpenCV源代码 Haar AdaBoost算法,作了一下改进 1.去掉了全部动态分配内存的操作.对嵌入式系统有一定的速度提升 2.凝视覆盖了大量关键代码 3.降低了代码一半的体积,而且 ...
- poj 2586 Y2K Accounting Bug (贪心)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8678 Accepted: 428 ...
- CentOS 6 安装Oracle11g
原创作品.从 "深蓝blog" 博客,欢迎转载,请务必注明转载如下源.否则追究其版权责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...
- URAL 1934 Black Spot --- 最短的简单修改
右侧是1.维护的同时保持最短路p值至少,我有直接存款(1-p).该概率不满足,为了使这个值极大. #include <iostream> #include <cstdlib> ...
- 开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
原文:[原创]开源Math.NET基础数学类库使用(17)C#计算矩阵条件数 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...