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. 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能

    此处采用VS2017+SqlServer数据库 一.创建项目并引用dll: 1.创建一个MVC项目 2.采用Nuget安装EF6.1.3 二.创建Model 在models文件夹中,建立相应的mode ...

  2. Build to win--来自小黄衫

    写在前面 首先非常荣幸.非常侥幸能以微弱的优势得到这次小黄衫,感谢各位老师同学的帮助,也谢谢来自<构建之法>团队的小黄衫赞助! 这次能够获得小黄衫,就像汪老师上课说的那样,其实,是一个积累 ...

  3. 201621123040《Java程序设计》第七周学习总结

    1.本周学习总结 1.1思维导图:Java图形界面总结 2.书面作业 2.1GUI中的事件处理 2.1.1写出事件处理模型中最重要的几个关键词. 关键词:事件 事件源 事件监听器 2.1.2任意编写事 ...

  4. 敏捷开发冲刺--day3

    1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285)  Git链接:https://github.com/WHUSE2017/C-team 2 ...

  5. Linux中Eclipse下搭建Web开发环境

    0. 准备工作 java环境,Linux下基本上都有含开源jdk的库,可直接下载,且不用配置环境变量,当然也可以官网下载后自己配置: Eclipse Neon,注意看清是64位还是32位,下载的应该是 ...

  6. 关于FPGA随笔

    verilog与c

  7. Entity Framework Core Code First

    参考地址:https://docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore/new-db

  8. Css之导航栏学习

    Css: ul { list-style-type:none; margin:; padding:; overflow:hidden; background-color:blue; /*固定在顶部*/ ...

  9. JAVA_SE基础——34.static修饰成员变量

    需求:描述一下学校的学生.  特点:都是中国人.... 测试代码1: class Student{ String name; String country = "中国"; //国籍 ...

  10. python 鸭子类型

    首先Python不支持多态,也不用支持多态,python是一种多态语言,崇尚鸭子类型. 在程序设计中,鸭子类型(英语:duck typing)是动态类型的一种风格.在这种风格中,一个对象有效的语义,不 ...