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.
先贪心的把数组从大到小排序,分3组
最小的一组无论是否合格,都不管
接下来随机交换第1组和第2组的值,直到有解
这应该属于随机算fa♂(即通过足够长的时间搞出答案才停)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<ctime>
using namespace std;
struct ZYYS
{
int x,id;
}a[];
int k,sum1,sum2;
bool cmp(ZYYS a,ZYYS b)
{
return a.x>b.x;
}
int main()
{int i,j;
cin>>k;
for (i=;i<=*k;i++)
{
scanf("%d",&a[i].x);
a[i].id=i;
}
sort(a+,a+*k+,cmp);
for (i=;i<=k;i++)
sum1+=a[i].x,sum2+=a[i+k].x;
srand(time());
while (!(sum1>*k&&sum2>*k))
{
int x1=rand()%k+,x2=rand()%k++k;
sum1=sum1-a[x1].x+a[x2].x;
sum2=sum2-a[x2].x+a[x1].x;
swap(a[x1],a[x2]);
}
for (i=;i<=*k;i++)
printf("%d\n",a[i].id);
}

POJ2454 Jersey Politics的更多相关文章

  1. POJ2454——Jersey Politics

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

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

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

  3. 【数学】Jersey Politics

                                                            Jersey Politics Time Limit: 1000MS   Memory ...

  4. Jersey Politics

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

  5. poj 2454 Jersey Politics 随机化

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

  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. 浅浅地谈一下随机算法【poj2454】【poj3318】

    随机算法我也只是稍微接触了一下,就是想写篇博客自己稍微总结一下 其实随机算法也算是一个玄学吧,运气不好还是会wa.但是我们知道,计算机可以在短时间内计算大量的数据,所以碰到正确答案的概率还是挺大的. ...

随机推荐

  1. C语言第三次博客作业

    一.PTA实验作业 1 1.实验代码 int i,N; char sex; float high; scanf("%d",&N); for(i =1;i <=N;i ...

  2. 展示博客(Beta版本)

    团队:xjbz 1. 团队成员博客,源码仓库地址. coding:https://git.coding.net/z404395979/xjbz.git 钟平辉(071):http://www.cnbl ...

  3. LOW版统计词频

    import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...

  4. 记一次jar包冲突

    题记:永远不要在同一个项目中,引用不同版本的两个jar包,否则,这可能就是一个大坑. 在做网校项目的时候,帮助中心要使用lucene,所以就引入了lucene-5.5.1的包,删掉了原先存在于项目中的 ...

  5. SWFUpload文件上传详解

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...

  6. STM32F4系列单片机上使用CUBE配置MBEDTLS实现pem格式公钥导入

    |版权声明:本文为博主原创文章,未经博主允许不得转载. 最近尝试在STM32F4下用MBEDTLS实现了公钥导入(我使用的是ECC加密),整个过程使用起来比较简单. 首先,STM32F4系列CUBE里 ...

  7. [UWP]针对UWP程序多语言支持的总结,含RTL

    UWP 对 Globalization and localization 的支持非常好,可以非常容易地实现应用程序本地化. 所谓本地化,表现最为直观的就是UI上文字和布局方式了,针对文字,提供不同的语 ...

  8. python 网络爬虫与信息提取 学习笔记day4

    正则表达式简介: 简洁表示一组字符串的特征或者模式,在文本处理中十分常用,主要应用于字符串匹配中 1.  通用的字符串表达框架 2.  简洁表达一组字符串的表达式 3.  针对字符串表达简洁和特征思想 ...

  9. mongodb 索引的基本命令

    mongodb的索引: 在数据量超大的时候,能够极大的增快查询速率,但是会降低更新效率.建立索引: db.集合.ensureIndex({属性:1}) //1代表升序 -1代表降序 db.集合.ens ...

  10. RxJava系列6(从微观角度解读RxJava源码)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...