BNUOJ 34982 Beautiful Garden

BNUOJ 34982 Beautiful Garden

题目地址:BNUOJ 34982

题意:

看错题意纠结了好久。。。

在坐标轴上有一些树,现在要重新排列这些树,使得相邻的树之间间距相等。

刚开始瞄了一眼以为是求最短的移动距离...后来发现是求最少去移动的树的数量。

分析:

刚开始想错了,以为任意取两棵树,作为相邻的两棵树就行了,吃了好多个wa后,发现这个有问题,因为考虑里面三棵树为最终序列中的三个,那么就有可能判断不出来。

于是想了新的方法,枚举两棵树后,再枚举中间有几棵树,在两棵树中间找有几棵树不用移动。

具体见代码。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  File:        b.cpp
*  Create Date: 2014-05-29 14:43:59
*  Descripton:
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;

typedef long long ll;

const int N = 44;
const double EPS = 1e-8;

ll t, n, x[N], mmin;
set<int> s;

void deal(int lhs, int rhs) {
	int cnt;
	ll dis = x[rhs] - x[lhs];
	// 如果在同一点就作为间距为0的情况处理
	if (dis == 0) {
		mmin = min(mmin, n - (rhs - lhs + 1));
		return;
	}
	// 枚举lhs和rhs中有k个间距,也可以枚举树
	for (int k = 2; k < n; k++) {
		cnt = 2;
		// 在中间的树中找要不用移动的树
		for (int i = lhs + 1; i < rhs; i++) {
			if (x[i] != x[i - 1] && x[i] > x[lhs] && x[i] < x[rhs] && (x[i] - x[lhs]) * k % dis == 0)
				cnt++;
		}
		mmin = min(mmin, n - cnt);
	}
}

int main()
{
	cin >> t;
	for (int cas = 1; cas <= t; cas++) {
		cin >> n;
		s.clear();
		for (int i = 0; i < n; i++) {
			cin >> x[i];
		}
		if (n <= 2) {
			printf("Case #%d: 0\n", cas);
			continue;
		}
		mmin = N;
		sort (x, x + n);
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				deal(i, j);
			}
		}
		printf("Case #%d: ", cas);
		cout << mmin << endl;
	}
	return 0;
}

BNUOJ 34982 Beautiful Garden

时间: 2025-01-12 18:30:59

BNUOJ 34982 Beautiful Garden的相关文章

bnu 34982 Beautiful Garden(暴力)

题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和中间一点,由于要求定中间一点的位置.所以这一步是o(n3);然后用o(n)的算法确定说须要移动几个来保证序列等差. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&g

bnu 34982 Beautiful Garden

Beautiful Garden There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the X-axis, and the coordinate of ith tree is xi. But in recent days, lxhgww wants to move some of the trees to make them look more beaut

北京邀请赛 B. Beautiful Garden

题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,最近水的要死,看了队友的代码做的 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cmath> using namespace

2014 SummerTrain Beautiful Garden

There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the X-axis, and the coordinate of ith tree is xi. But in recent days, lxhgww wants to move some of the trees to make them look more beautiful. lxhgww will

hdu 5977 Garden of Eden

Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 581    Accepted Submission(s): 164 Problem Description When God made the first man, he put him on a beautiful garden, the Garden

hdu-5977 Garden of Eden(树分治)

题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 210    Accepted Submission(s): 75 Problem Description When God made the first man, he put him on a beautiful garden, the G

2014北京邀请赛(部分题解)

马上要去比赛了. 今天做了一下2014北京邀请赛,出了两道题目,感觉很水啊... 首先H题: H. Happy Reversal Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 34988 Font Size:  +   - Elfness is studying

The best and the worst

原文 Joe Sanders has the most beautiful garden in our town. Nearly everybody enters for "The Nicest Garden Competition" each year, but Joe wins every time. Bill Frith's garden is larger than Joe's. Bill works harder than Joe and grows more flowers

2014 北京邀请赛ABDHJ题解

A. A Matrix 点击打开链接 构造,结论是从第一行开始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; #defi