Jersey Politics
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
【解题报告】
这道题是一道比较有意思的题。考虑到只要s1,s2,s3中的两个的值大于k*500就可以了,首先采用一个贪心的策略,对序列采用降序排序,把前2k个划分给s1,s2。然后再采用一个随机化的方法,每次对s1,s2中的一个元素进行互换,直到找到正确答案。
感觉这种随机题有点靠人品。。。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
int nu,t;
}num[200],num1[100],num2[100],temp;
bool cmp(node a,node b)
{
return a.nu>b.nu;
}
int main()
{
int k,sum1=0,sum2=0,rd1,rd2;
scanf("%d",&k);
for(int i=0;i<3*k;i++)
{
scanf("%d",&num[i].nu);
num[i].t=i;
}
sort(num,num+(3*k),cmp);
for(int i=0;i<2*k;i+=2)
{
num1[i/2]=num[i];
sum1+=num[i].nu;
}
for(int i=1;i<2*k;i+=2)
{
num2[i/2]=num[i];
sum2+=num[i].nu;
}
while(sum1<=500*k||sum2<=500*k)
{
rd1=rand()%k;
rd2=rand()%k;
sum1=sum1-num1[rd1].nu+num2[rd2].nu;
sum2=sum2-num2[rd2].nu+num1[rd1].nu;
temp=num1[rd1];
num1[rd1]=num2[rd2];
num2[rd2]=temp;
}
for(int i=0;i<k;i++)
{
printf("%d\n",num1[i].t+1);
}
for(int i=0;i<k;i++)
{
printf("%d\n",num2[i].t+1);
}
for(int i=2*k;i<3*k;i++)
{
printf("%d\n",num[i].t+1);
}
return 0;
}