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\n",ans);
        for(int i=1; i<=n; i++)
        {
            for(int j=1;j<=c[i].num;j++)
            {
                printf("%d ",c[i].b[j]);
            }
            printf("\n");
        }
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章