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 ...
随机推荐
- 用链表实现栈----《数据结构与算法分析----C语言描述》
一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...
- Conversion to Dalvik format failed: Unable to execute dex
最近莫名奇妙遇到“Conversion to Dalvik format failed: Unable to execute dex”错误,stackoverflow以后得到结果 把项目中classp ...
- Eureka 的 Application Client client的执行演示样例
上篇以一个 demo 演示样例介绍了 Eureka 的 Application Service 客户端角色.今天我们继续了解 Eureka 的 Application Client 客 ...
- 【android】在Eclipse在联想引jar包源代码
(前提是你有jar包源代码!!) .确保Referenced LIbraies下已经有该jar包,否则的话,右击该jar包选build path->add to build path. 二.右键 ...
- Linux应用环境实战10:Bash脚本编程语言中的美学与哲学(转)
阅读目录 一.一切皆是字符串 二.引用和元字符 三.字符串从哪里来.到哪里去 四.再加上一点点的定义,就可以推导出整个Bash脚本语言的语法了 五.输入输出重定向 六.Bash脚本语言的美学:大道至简 ...
- 简单工厂 VS 工厂方法 VS 抽象工厂
说到设计模式.自然少不了简单工厂模式.工厂方法和抽象工厂这三姐妹. 它们之间可谓是各有所长,术业专攻啊!这篇博客来简单的梳理一下三者之间的关系. 那么工厂又是什么意思呢?结合三者的特点,我觉得能够这样 ...
- Deploy 11.2.0.3 RAC+DG on Windows 2008 R2 Step by Step
环境规划: 节点1: tc1 192.168.56.101 内存:2G 节点2: tc2 192.168.56.102 内存:2G 物理备库:tcdg192.168.56.108内存:1.5G 操作系 ...
- zabbix通过脚本发送短信
zabbix通过脚本发送短信 原则 和zabbix电子邮件是一样的,他们是action内部配置,司的api来完毕.当然网上有不少利用139邮箱来发的,这个事实上算调用email的一种,这里复述的是调用 ...
- 开源Math.NET基础数学类库使用(16)C#计算矩阵秩
原文:[原创]开源Math.NET基础数学类库使用(16)C#计算矩阵秩 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...
- Flex强制类型转换错误
1.错误描写叙述 TypeError: Error #1034: 强制转换类型失败:无法将 HoleDetailInnerClass0@c2cccf1 转换为 mx.controls.listClas ...