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



  • 相关阅读:
    jQuery获取当前元素是该父元素的第几个元素&获取父元素的第n个子元素
    获取当前月份的天数
    获取当年的每个月份的天数:
    解决ul下的li换行问题,(父元素div加overflow:scroll没作用的问题)
     获取当年的月份的天数:
    程序员成长道路上必经的几个阶段
    CSS如何让文字垂直居中?
    2015年7个重要的Web设计趋势
    引入css文件时,css link和@import区别
    在html页面引用css文件的方法
  • 原文地址:https://www.cnblogs.com/Zeroinger/p/5493918.html
Copyright © 2011-2022 走看看