zoukankan      html  css  js  c++  java
  • POJ2454:Jersey Politics(贪心+随机化) java程序员

    Jersey Politics
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4231   Accepted: 1022   Special Judge

    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

    MYCode:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<ctime>
    #include<algorithm>
    using namespace std;
    #define MAX 200
    struct node
    {
        int v;
        int id;
    }a[MAX];
    bool cmp(node a,node b)
    {
        return a.v>b.v;
    }
    int main()
    {
        int k;
        while(scanf("%d",&k)!=EOF)
        {
            int i;
            for(i=1;i<=3*k;i++)
            {
                scanf("%d",&a[i].v);
                a[i].id=i;
            }
            sort(a+1,a+3*k+1,cmp);
            int s1=0,s2=0;
            for(i=1;i<=k;i++)
            {
                s1+=a[i].v;
                s2+=a[i+k].v;
            }
            srand((int)(time(0)));
            while(true)
            {
                if(s1>500*k && s2>500*k)
                break;
                int id1=rand()%k+1;
                int id2=rand()%k+1+k;
                if(a[id1].v <a[id2].v&&s1<s2||a[id1].v>a[id2].v&&s1>s2)
                {
                    s1+=a[id2].v-a[id1].v;
                    s2+=a[id1].v-a[id2].v;
                    node tp;
                    tp=a[id2];
                    a[id2]=a[id1];
                    a[id1]=tp;
                }
            }
            for(i=1;i<=k;i++)
            {
                printf("%d\n",a[i].id);
            }
            for(i=k+1;i<=2*k;i++)
            {
                printf("%d\n",a[i].id);
            }
            for(i=2*k+1;i<=3*k;i++)
            {
                printf("%d\n",a[i].id);
            }
        }
    }
    //16MS

    贪心+随机化           

  • 相关阅读:
    【web性能优化】DNS解析与ip
    【web性能优化】雅虎军规
    【web性能优化】优化提纲及图片优化(慕课网笔记)
    【web性能优化】常用缓存方式(慕课网学习笔记)
    【前端】企业微信客户端调试
    【es6】es6使用集锦
    【前端】遇到的各种报错
    【前端】安装wampserver提示丢失MSVCR100.dll的解决方法
    【es6】将2个数组合并为一个数组
    【web】使用ionic搭建移动端项目 icon-radio 标签在ios下全部选中的问题
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215871.html
Copyright © 2011-2022 走看看