Jersey Politics
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5592   Accepted: 1413   Special Judge

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

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.
 
试题分析:
    我们可以想到把数组排一个序,然后取前2k个,分到S1、S2中去
    设S=(S1,S2)
    那么我们持续随机排序S,最终在满足条件时输出答案
  
    上面的策略固然能解决这个问题,但是随即排序S时间浪费太大
    那么我们要怎样呢?
    我们可以随机交换S1,S2中的元素,然后直到满足条件就可以了
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
struct data{
int num,k;
}a[200],s1[61],s2[61],s3[61];//S1 S2满足条件
bool cmp(data a,data b){
return a.num>b.num;
}
int N;
bool judge(){
int sum=0,sum2=0;
for(int i=1;i<=N/3;i++) sum+=s1[i].num;//S1 sum
for(int i=1;i<=N/3;i++) sum2+=s2[i].num;
if(sum2>500*(N/3)&&sum>500*(N/3)) return true;
return false;
}
void change(){
//srand((unsigned int)time(0));加上这个POJ会给你显示RE
int tmp1=rand()%(N/3)+1;
int tmp2=rand()%(N/3)+1;
swap(s1[tmp1].num,s2[tmp2].num);
swap(s1[tmp1].k,s2[tmp2].k);
}
void print(){
for(int i=1;i<=N/3;i++) cout<<s1[i].k<<endl;
for(int i=1;i<=N/3;i++) cout<<s2[i].k<<endl;
for(int i=1;i<=N/3;i++) cout<<s3[i].k<<endl;
}
int main(){
N=read(),N*=3;
for(int i=1;i<=N;i++) a[i].num=read(),a[i].k=i;
sort(a+1,a+N+1,cmp); for(int i=1;i<=N/3;i++) //划分前k大到S1
s1[i].num=a[i].num,s1[i].k=a[i].k;
for(int i=N/3+1;i<=2*(N/3);i++) //划分前K+1~前2K大到S2
s2[i-N/3].num=a[i].num,s2[i-N/3].k=a[i].k;
for(int i=2*(N/3)+1;i<=N;i++)
s3[i-2*(N/3)].num=a[i].num,s3[i-2*(N/3)].k=a[i].k; while(!judge()) change();
print();
}

  

【数学】Jersey Politics的更多相关文章

  1. POJ2454——Jersey Politics

    POJ2454——Jersey Politics 题目大意: 在泽西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷仓中获得了三个档位. 泽西奶牛目前控制着国家重新分配委员会. 他们想将国家分为三个相当 ...

  2. POJ2454 Jersey Politics

    Description In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three ...

  3. [USACO2005][POJ2454]Jersey Politics(随机化)

    题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...

  4. poj 2454 Jersey Politics 随机化

    随机化算法+贪心! 将3*k排序后分成3分,将第二第三份的和分别加起来,让和与500*k比较,都大于则输出,否则,随机生成2个数,在第二第三份中交换! 代码如下: #include<iostre ...

  5. Jersey Politics

    poj2454:http://poj.org/problem?id=2454 题意:给你3*k个数,然后让你分成三堆,使得至少其中的两堆中的数字之和大于500*k.题解:这道题一开始我并不知道怎么做, ...

  6. poj 2454 Jersey Politics dfs

    这个题目第一步还是比较明显的,先把最小的n个值去掉,剩下的问题就是能不能把数据分成两半,使得每一半和都大于n*500,这个刚开始考虑了下dp的做法,但是复杂度不满足要求. 那么能想到的就是搜索了,实际 ...

  7. POJ.2454.Jersey Politics(随机化算法)

    题目链接 \(Description\) 将长为\(3n\)的序列划分成\(3\)个子序列,要求至少有两个子序列的和都\(\geq 500*n\),输出任一方案.保证有解. \(Solution\) ...

  8. 【POJ】2454.Jersey Politics

    题解 有种迷一样的讽刺效果 每个城市有1000头牛,然后你现在知道对于自己政党每个城市的选票,把城市划分成三个州,保证在至少两个州内获胜 找出前2K大的然后random_shuffle,直到前K个加起 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. 2016huasacm暑假集训训练四 递推_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/B 题意:给定n个三角形,问最多可以把区域化成多少个部分,这是一个一维空间  一定会 ...

  2. python 2.43 升级到2.7

    [root@GW1 bin]# lsb_release -aLSB Version: :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3. ...

  3. 然并卵,腾讯QQ认证空间又再次关闭申请

    昨天发布的腾讯QQ认证空间又开放申请的消息,此消息一放出,大家都去关注认证的事情,而马浩周发现在4月27日下午4-5点,腾讯QQ空间认证又再次关闭页面开放申请的通知,变成了以前停止审核的通知了. 可能 ...

  4. Flink - InstanceManager

    InstanceManager用于管理JobManager申请到的taskManager和slots资源 /** * Simple manager that keeps track of which ...

  5. 一步一步来做WebQQ机器人-(五)(发送消息||完结)

    × 本篇主要是: 发送QQ消息(to:好友,群),以及对小黄鸡抓包利用它的语言库 本文是WebQQ流程的最后一章 最后一章内容不多但我还是啰嗦,可能对大部分人都已知晓的流程方法我也会介绍一下 前面几个 ...

  6. JAVA基础篇NO1--环境变量的配置及命名规则

    标签(空格分隔): java基础 一:计算机概述 计算机:硬件和软件 硬件:控制器 运算器 存储器 输入和输出设备       存储器:外存(硬盘) 内存 软件:系统软件 应用软件   系统软件:wi ...

  7. 浅析“依赖注入(DI)/控制反转(IOC)”的实现思路

    开始学习Spring的时候,对依赖注入(DI)——也叫控制反转(IOC)—— 的理解不是很深刻.随着学习的深入,也逐渐有了自己的认识,在此记录,也希望能帮助其他入门同学更深入地理解Spring.本文不 ...

  8. 超好用的网页栅格化工具: GridGuide

    平面设计中使用栅格化设计是相当重要的,特别是网页和VI设计方面,在设计前都需要来好栅格,但是选择合适栅格和计算无疑是浪费了设计师不少的时间,然而当遇上今天的神器「GridGuide」在线工具,以后再也 ...

  9. MVC 上传 下载

    [上传]带进度条 view  注:添加easyui的js文件 <script type="text/javascript"> function fileSelected ...

  10. [原创]Eclipse Mars 在Ubuntu升级后无法工作的解决方法

    近日将自己的Ubuntu从14.04LTS升级到了16.04LTS,顿时发现Eclipse不能正常工作了,到Ubuntu的官网上转了一圈发现以下解决方案: 症状: [1]Eclipse启动很慢; [2 ...