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.
Source |
……随机化专题。。
思路挺正,题目要求把3*k批牛分为3组,每组k批,每组有一个总的牛数量,要求至少两组牛数量都大于 500∗k ,那么一定是从大到小排序后,讲前 2∗k 头牛分为和均大于 500∗k 的两组牛。保证有解。
至于分法……rand=。= ……神rand……不过想想好像没什么不对?%……
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
struct Node
{
int x,id;
bool operator <(const struct Node a)const
{
return x > a.x;
}
};
Node num[233];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1; i <= 3*n; ++i)
{
scanf("%d",&num[i].x);
num[i].id = i;
}
sort(num+1,num+3*n+1);
int s1,s2;
s1 = s2 = 0;
int ned = n*500;
for(int i = 1; i <= n; ++i)
{
s1 += num[i].x;
}
for(int i = n+1; i <= 2*n; ++i)
{
s2 += num[i].x;
}
while(s1 <= ned || s2 <= ned)
{
int x,y;
x = rand()%n+1;
y = rand()%n+1+n;
//printf("%d %d\n",x,y);
s1 = s1-num[x].x+num[y].x;
s2 = s2-num[y].x+num[x].x;
swap(num[x],num[y]);
}
for(int i = 1; i <= 3*n; ++i)
printf("%d\n",num[i].id);
}
return 0;
}