题目链接: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(暴力)的更多相关文章

  1. BNUOJ 34982 Beautiful Garden

    BNUOJ 34982 Beautiful Garden 题目地址:BNUOJ 34982 题意:  看错题意纠结了好久... 在坐标轴上有一些树,如今要又一次排列这些树,使得相邻的树之间间距相等.  ...

  2. 牛客多校第四场 F Beautiful Garden

    链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...

  3. (第四场)F Beautiful Garden

    题目: F Beautiful Garden 题目描述 There's a beautiful garden whose size is n x m in Chiaki's house. The ga ...

  4. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  5. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

    B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...

  6. 2014 SummerTrain Beautiful Garden

    There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the ...

  7. 牛客网暑期ACM多校训练营(第四场) F Beautiful Garden

    链接: https://www.nowcoder.com/acm/contest/142/F 题意: n x m的矩形,选个p x q的矩形去掉,两个矩形中⼼重合,去掉后的矩形上下左右对称 求(p, ...

  8. 北京邀请赛 B. Beautiful Garden

    题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,近期水的要死,看了队友的代码做的 #include &l ...

  9. HDU5977 Garden of Eden(树的点分治)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...

随机推荐

  1. 微端 代码project as air 分享

    分享 ^_^ 1. 使用 air , as . 2. 微端下载和更新技术 成功上线棋牌游戏.它可用于传统的游戏开发. 地址: http://download.csdn.net/detail/stone ...

  2. [置顶] ios 时间定时器 NSTimer应用demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...

  3. MyEclipse中“擅自乱改”项目名导致项目报错的处理

    最近几天培训的过程中,经常有同学手一抖,默默的修改了本来配置部署好的项目名,导致项目报错…… 遇到这种事情,我一般会做的处理就是重新新建项目,然后把包和各种文件ctrl+c ctrl+v,遇到项目小还 ...

  4. Java 理论与实践: 处理 InterruptedException(转)

    很多 Java™ 语言方法,例如 Thread.sleep() 和 Object.wait(),都可以抛出InterruptedException.您不能忽略这个异常,因为它是一个检查异常(check ...

  5. oracle数据库恢复与备份

    一.oracle数据库恢复 1.恢复刚才删除的一条数据 delete from emp e where e.empname='SMITH' select * from flashback_transa ...

  6. ArcGIS SDE 10.1 for Postgresql 服务连接配置

    去年写了ArcGIS 10.1 如何连接Postgresql 数据库(http://blog.csdn.net/arcgis_all/article/details/8202709)当时采用的也是Ar ...

  7. Jquery中使用setInterval和setTimeout 容易犯的低级错误

    直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...

  8. 重新想象 Windows 8 Store Apps (8) - 控件之 WebView

    原文:重新想象 Windows 8 Store Apps (8) - 控件之 WebView [源码下载] 重新想象 Windows 8 Store Apps (8) - 控件之 WebView 作者 ...

  9. C# 隐藏 Windows Phone 侦错模式中萤幕右上角的数据条(模拟器、实机可用),截图好方便。

    原文:C# 隐藏 Windows Phone 侦错模式中萤幕右上角的数据条(模拟器.实机可用),截图好方便. 一般我们在开发Windows Phone App时,会使用模拟器或是实体的手机开发,在Vi ...

  10. Hello World! 2010年山东省第一届ACM大学生程序设计竞赛

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...