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. TensorFlow实现Softmax Regression识别手写数字中"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败”问题

    出现问题: 在使用TensorFlow实现MNIST手写数字识别时,出现"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应 ...

  2. Java 密码学算法

    Java 密码学算法 候捷老师在< 深入浅出MFC 2e(电子版)>中引用林语堂先生的一句话: 只用一样东西,不明白它的道理,实在不高明 只知道How,不知道Why,出了一点小问题时就无能 ...

  3. QTableView

    QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型     QStandardItemModel *student_model = ne ...

  4. vue内置指令详解——小白速会

    指令 (Directives) 是带有 v- 前缀的特殊属性,职责是,当表达式的值改变时,将其产生的连带影响,响应式地作用于 DOM. 内置指令 1.v-bind:响应并更新DOM特性:例如:v-bi ...

  5. javascript实现小鸟飞行轨迹

    javascript实现小鸟飞行轨迹 代码如下:

  6. JAVA_SE基础——46.引用数据类型变量.值交换[独家深入解析]

    需求:定义一个函数交换数组中两个元素的位置. code 1: import java.util.*; class Demo3 { public static void main(String[] ar ...

  7. svn的使用技巧

    就是如果想一个文件在提交的时候不被上传,可以设置忽略这样提交的时候就不会被上传

  8. 大数据学习总结(7)we should...

    大数据场景一.各种标签查询 查询要素:人.事.物.单位 查询范围:A范围.B范围.... 查询结果:pic.name.data from 1.痛点:对所有文本皆有实时查询需求2.难点:传统SQL使用W ...

  9. python Django之文件上传

    python Django之文件上传 使用Django框架进行文件上传共分为俩种方式 一.方式一 通过form表单进行文件上传 #=================================== ...

  10. Mysql 测试题

    一. 表结构和数据 作业要求 /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL ...