题目链接: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. gdb学习(一个)[再版]

    概要 gdb是GNU debugger的缩写,是编程调试工具. 功能 1.启动程序,能够依照用户自己定义的要求随心所欲的执行程序. 2.可让被调试的程序在用户所指定的断点处停住 (断点能够是条件表达式 ...

  2. Java 二次MD5 32位小写加密算法与php页面加密结果相同

    最近做的一个项目需要使用MD5加密算法,需要加密的参数有两个.自己先试了几次,算的结果为php页面的不一样,后来与写php页面的同事沟通后,了解到php页面的算法如下: action = " ...

  3. VMware WorkStation安装时提示The MSI failed

    以前安装过其他版本的VMware workstation卸载不完全造成的 先把所有VMware相关服务关闭,然后打开注册表,搜索所有VMware相关键值,删除掉,然后再安装就可以了 前提是你机器上没有 ...

  4. HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram

    HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram ...

  5. POJ2239_Selecting Courses(二分图最大匹配)

    解题报告 http://blog.csdn.net/juncoder/article/details/38154699 题目传送门 题意: 每天有12节课.一周上7天,一门课在一周有多天上课. 求一周 ...

  6. hdu1506(dp减少重复计算)

    可以算出以第i个值为高度的矩形可以向左延伸left[i],向右延伸right[i]的长度 那么答案便是 (left[i] + right[i] + 1) * a[i] 的最大值 关键left[i] 和 ...

  7. Android Material Design-Creating Lists and Cards(创建列表和卡)-(三)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40430319 翻译自:http://developer.android.com/trainin ...

  8. 染色法判断是否是二分图 hdu2444

    用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...

  9. 线段树(单点更新and成段更新)

    线段树需要的空间. 区间为1-->n 假设是一棵完全二叉树,且树高为i. 完全二叉树性质:第i层最多有2^(i-1)个结点. 那么 2^(i-1) = n;     i = log2(n)  + ...

  10. usb host鼠标不能使用原因

    linux kernel 3.4.5的板子插入USB鼠标,出现例如以下错误LOG: [  191.177508] Plug in USB Port2 [  191.363516] usb 1-1: n ...