zoukankan      html  css  js  c++  java
  • POJ2454 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

    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(即通过足够长的时间搞出答案才停)
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<ctime>
     7 using namespace std;
     8 struct ZYYS
     9 {
    10   int x,id;
    11 }a[201];
    12 int k,sum1,sum2;
    13 bool cmp(ZYYS a,ZYYS b)
    14 {
    15   return a.x>b.x;
    16 }
    17 int main()
    18 {int i,j;
    19   cin>>k;
    20   for (i=1;i<=3*k;i++)
    21     {
    22       scanf("%d",&a[i].x);
    23       a[i].id=i;
    24     }
    25   sort(a+1,a+3*k+1,cmp);
    26   for (i=1;i<=k;i++)
    27     sum1+=a[i].x,sum2+=a[i+k].x;
    28   srand(time(0));
    29   while (!(sum1>500*k&&sum2>500*k))
    30     {
    31       int x1=rand()%k+1,x2=rand()%k+1+k;
    32       sum1=sum1-a[x1].x+a[x2].x;
    33       sum2=sum2-a[x2].x+a[x1].x;
    34       swap(a[x1],a[x2]);
    35     }
    36   for (i=1;i<=3*k;i++)
    37     printf("%d
    ",a[i].id);
    38 }
  • 相关阅读:
    typedef用法小结
    14种排序
    常用google产品
    去重排序
    双向链表
    IDEA上传一个项目到github
    IDEA上传一个项目到github
    Git的安装
    Hibernate 加载策略得总结
    hadoop -- fsck
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/8310167.html
Copyright © 2011-2022 走看看