poj_2454 Jersey Politics(贪心+随机)
2017-01-10 17:49
501 查看
Jersey Politics
Description
In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three stalls in the Barn of Representatives. The Jersey Cows currently control the state's redistricting committee. They want to partition the state
into three equally sized voting districts such that the Jersey Cows are guaranteed to win elections in at least two of the districts.
Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage
in at least two of districts.
All supplied input datasets are solvable.
Input
* Line 1: A single integer, K
* Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i's cow census.
Output
* Lines 1..K: K lines that are the city numbers in district one, one per line
* Lines K+1..2K: K lines that are the city numbers in district two, one per line
* Lines 2K+1..3K: K lines that are the city numbers in district three, one per line
Sample Input
Sample Output
Hint
Other solutions might be possible. Note that "2 3" would NOT be a district won by the Jerseys, as they would be exactly half of the cows.
先贪心,对数组排序后,其中一个最小的划分一定是数组前面的k个元素,另外两个划分则在后面的2*k个元素中。
然后先假设两个划分刚好是k+1~2*k和2*k~3*k,这种划分不一定满足题目要求的两个划分各自的元素和都大于500*k,
所以需要对两个划分进行调整,可以用随机算法随机从两个划分中各抽取一个元素进行交换,直到满足题意。
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5596 | Accepted: 1416 | Special Judge |
In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three stalls in the Barn of Representatives. The Jersey Cows currently control the state's redistricting committee. They want to partition the state
into three equally sized voting districts such that the Jersey Cows are guaranteed to win elections in at least two of the districts.
Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage
in at least two of districts.
All supplied input datasets are solvable.
Input
* Line 1: A single integer, K
* Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i's cow census.
Output
* Lines 1..K: K lines that are the city numbers in district one, one per line
* Lines K+1..2K: K lines that are the city numbers in district two, one per line
* Lines 2K+1..3K: K lines that are the city numbers in district three, one per line
Sample Input
2 510 500 500 670 400 310
Sample Output
1 2 3 6 5 4
Hint
Other solutions might be possible. Note that "2 3" would NOT be a district won by the Jerseys, as they would be exactly half of the cows.
先贪心,对数组排序后,其中一个最小的划分一定是数组前面的k个元素,另外两个划分则在后面的2*k个元素中。
然后先假设两个划分刚好是k+1~2*k和2*k~3*k,这种划分不一定满足题目要求的两个划分各自的元素和都大于500*k,
所以需要对两个划分进行调整,可以用随机算法随机从两个划分中各抽取一个元素进行交换,直到满足题意。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <stack> #include <bitset> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #define FOP freopen("data.txt","r",stdin) #define FOP2 freopen("data1.txt","w",stdout) #define inf 0x3f3f3f3f #define maxn 200 #define mod 1000000007 #define PI acos(-1.0) #define LL long long using namespace std; struct Node { int x, pos; }a[maxn]; bool cmp(Node a, Node b) { return a.x < b.x; } int k; Node x1[maxn], x2[maxn]; bool judge() { int s1 = 0, s2 = 0; for(int i = 1; i <= k; i++) s1 += x1[i].x, s2 += x2[i].x; if(s1 > 500*k && s2 > 500*k) return true; else return false; } void solve() { srand((unsigned)time(NULL)); while(1) { if(judge()) break; int i = rand()%k+1; int j = rand()%k+1; swap(x1[i], x2[j]); } } int main() { while(~scanf("%d", &k)) { for(int i = 1; i <= 3*k; i++) scanf("%d", &a[i].x), a[i].pos = i; sort(a+1, a+3*k+1, cmp); for(int i = 1; i <= k; i++) x1[i] = a[i+k], x2[i] = a[i+2*k]; solve(); for(int i = 1; i <= k; i++) printf("%d\n", a[i].pos); for(int i = 1; i <= k; i++) printf("%d\n", x1[i].pos); for(int i = 1; i <= k; i++) printf("%d\n", x2[i].pos); } return 0; }
相关文章推荐
- JNI教程(二)
- Linux strace命令
- nginx使用ssl模块配置HTTPS支持(自签证书)
- tomcat 配置多个虚拟域名访问
- mysql5.7或5.6安装workbench、 connector odbc失败。
- SFTP文件上传和下载
- 69. Sqrt(x)
- hadoop基础配置
- laravel5.2实现区分前后台用户登录
- 关于使用腾讯云播放器的遇到的坑
- linux修改主机名的步骤
- 视频TS文件AES加密处理
- vc中debug版程序正常但release版崩溃的解决方法
- 感知机模型
- view的指定角设置圆弧
- 数据结构_图_定义/分类/顶点与边之间的关系/连通图/存储结构/基本操作
- 适合新人使用的Mockplus原型图设计软件
- [转]Publishing and Running ASP.NET Core Applications with IIS
- elasticsearch负载均衡节点——客户端节点 node.master: false node.data: false 其他配置和master 数据节点一样
- 【Html 学习笔记】第四节——框架