POJ 2425 随机算法 解题报告

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;  
}  

让我看到你们的双手

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值