BNUOJ 34982 Beautiful Garden 2014北京邀请赛B (有意思的枚举题)
2015-09-25 21:44
537 查看
题意: 在坐标轴上有一些树,现在要重新排列这些树,使得相邻的树之间间距相等。 求最少移动的树的数量。
分析: 至少有两棵没有移动 枚举两棵树 然后看中间有多少不用移动的 注意判断重合的情况
代码:
//
// Created by TaoSama on 2015-09-25
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, a
;
int cal(int l, int r) {
int d = a[r] - a[l];
if(d == 0) return n - (r - l + 1); //左右重合
int ret = INF;
for(int k = 2; k < n; ++k) { //枚举k个间距
int cnt = 2;
for(int i = l + 1; i < r; ++i) { //不重合
if(a[i] != a[i - 1] && a[i] != a[r] && (a[i] - a[l]) * k % d == 0)
++cnt;
}
ret = min(ret, n - cnt);
}
return ret;
}
int main() {
#ifdef LOCAL
// freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
int kase = 0;
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", a + i);
printf("Case #%d: ", ++kase);
if(n <= 2) {
puts("0");
continue;
}
int ans = INF;
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; ++i)
for(int j = i + 1; j <= n; ++j)
ans = min(ans, cal(i, j));
printf("%d\n", ans);
}
return 0;
}
分析: 至少有两棵没有移动 枚举两棵树 然后看中间有多少不用移动的 注意判断重合的情况
代码:
//
// Created by TaoSama on 2015-09-25
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, a
;
int cal(int l, int r) {
int d = a[r] - a[l];
if(d == 0) return n - (r - l + 1); //左右重合
int ret = INF;
for(int k = 2; k < n; ++k) { //枚举k个间距
int cnt = 2;
for(int i = l + 1; i < r; ++i) { //不重合
if(a[i] != a[i - 1] && a[i] != a[r] && (a[i] - a[l]) * k % d == 0)
++cnt;
}
ret = min(ret, n - cnt);
}
return ret;
}
int main() {
#ifdef LOCAL
// freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
int kase = 0;
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", a + i);
printf("Case #%d: ", ++kase);
if(n <= 2) {
puts("0");
continue;
}
int ans = INF;
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; ++i)
for(int j = i + 1; j <= n; ++j)
ans = min(ans, cal(i, j));
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 枚举的用法详细总结
- c#入门之枚举和结构体使用详解(控制台接收字符串以相反的方向输出)
- 理解C#中的枚举(简明易懂)
- C#枚举中的位运算权限分配浅谈
- C++基础入门教程(四):枚举和指针
- FileShare枚举的使用小结(文件读写锁)
- Java枚举类用法实例
- 枚举窗口句柄后关闭所有窗口示例
- javascript模拟枚举的简单实例
- 深入剖析JavaScript中的枚举功能
- 枚举和宏的区别详细解析
- 深入理解C#中的枚举
- C#实现获取枚举中元素个数的方法
- java中枚举的详细使用介绍
- java枚举的使用示例
- Java枚举详解及使用实例(涵盖了所有典型用法)
- 一个运用enum类型的Demo
- 枚举隐式派生于System.Enum
- 枚举的隐士转换跟显示转换
- Java 枚举7常见种用法