zoukankan      html  css  js  c++  java
  • POJ 4014

    题目链接:http://poj.org/problem?id=4014

    Problem D. Dice

    Input file: dice.in

    Output file: dice.out 

    Time limit: 3 seconds 

    Memory limit: 256 megabytes

    Along with other things, Feadagor is fond of playing tabletop role playing games. He has just discovered a new game and he’d like to play it with his friends. Unluckily, he cannot call his friends right now, as the game requires quite an unusual set of dice. The description of the game says that there must be n dice, and i-th die must have ai faces. Each die must be shaped so its faces fall equiprobably. According to the game manual, the numbers from 1 to m, where m =∑n i=1 ai, must be written on the faces, and each number from the interval must be used exactly once. The numbers arrangement must be chosen so that if you throw all the dice simultaneously, then the mathematical expectation E of the total value in such experiment will be maximal. The manual says that only Maiar have enough wisdom to arrange the numbers properly (and therefore your only choice is to buy the dice just for 133 dollars, telepathy is quite expensive now). But maybe there is a simpler way to make the proper arrangement? Input The first line of the input file contains a single integer number n (1 ≤ n ≤ 1000). The following line contains n integer numbers a1, a2 ... an (1≤ ai ≤100). Output The first line of the output file must contain maximal possible expectation E — a floating-point number precise up to 5 digits after the decimal point. The following n lines must contain the numbers arrangement: i-th line must contain ai integer numbers — the numbers to be written on the faces of i-th die. Example
    dice.in dice.out
    2 1 4

    7.50000 5 1 2 3 4

    题目很简单,贪心即可。筛子面数越小,上面的数就要越大。
    POJ上这道题目不用文件读写可过,用了反而不过,真是。。。,还有编译时用G++超时,C++不超。
    代码如下:
    #include<iostream>
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=1005;
    struct date
    {
        int num;
        int po;
    }a[maxn];
    struct out
    {
        int num;
        int b[110];
    }c[maxn];
    bool cmp(date x,date y)
    {
        return x.num<y.num;
    }
    int main()
    {
        //freopen("dice.in","r",stdin);
        //freopen("dice.out","w",stdout);
        int n,sum=0;
        double ans=0;
        while(scanf("%d",&n)!=EOF)
        {
            ans=0;
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&a[i].num);
                sum+=a[i].num;
                a[i].po=i;
                c[i].num=a[i].num;
            }
            sort(a+1,a+n+1,cmp);//用下面的可以优化下一个for循环,读者可以自己试试
            /*int start=1,s=1;
            for(int i=sum;i>=1;i--)
            {
                ans+=(1.0*i/a[s].num);
                c[a[s].po].b[start++]=i;
                if(start>a[s].num)
                {
                    start=1;
                    s++;
                }
            }*/
            for(int j=1; j<=n; j++)
            {
                int num=0;
                int  start=1;
                for(int k=a[j].num; k>=1; k--)
                {
                    c[a[j].po].b[start++]=sum;
                    num+=sum--;
                }
                ans+=(1.0*num)/a[j].num;
            }
            printf("%f
    ",ans);
            for(int i=1; i<=n; i++)
            {
                for(int j=1;j<=c[i].num;j++)
                {
                    printf("%d ",c[i].b[j]);
                }
                printf("
    ");
            }
        }
        return 0;
    }



  • 相关阅读:
    自己写CPU第五级(5)——测试逻辑、实现移动和空指令
    ERROR: The partition with /var/lib/mysql is too full! failed!
    关于精益创业理念随想
    android使用ffmpeg
    如何让格斗游戏的横版过关(2) Cocos2d-x 2.0.4
    java两个音频进入巩固期 玩的同时类似的伴奏
    Java 反射 想
    SharePoint 2013 如何使用Silverlight
    如何才能连接到你的网站访客
    写作---英语中常见的写作错误有哪些
  • 原文地址:https://www.cnblogs.com/Zeroinger/p/5493918.html
Copyright © 2011-2022 走看看