首页 > ACM题库 > HDU-杭电 > HDU 4153-Grey Area-高精度-[解题报告]HOJ
2015
04-16

HDU 4153-Grey Area-高精度-[解题报告]HOJ

Grey Area

问题描述 :

Dr. Grey is a data analyst, who visualizes various aspects of data received from all over the world everyday. He is extremely good at sophisticated visualization tools, but yet his favorite is a simple self-made histogram generator.
ZZY’s Dilemma

Figure 1: A histogram

Figure 1 is an example of histogram automatically produced by his histogram generator. A histogram is a visual display of frequencies of value occurrences as bars. In this example, values in the interval 0�C9 occur five times, those in the interval 10�C19 occur three times, and 20�C29 and 30�C39 once each. Dr. Grey’s histogram generator is a simple tool. First, the height of the histogram is fixed, that is, the height of the highest bar is always the same and those of the others are automatically adjusted proportionately. Second, the widths of bars are also fixed. It can only produce a histogram of uniform intervals, that is, each interval of a histogram should have the same width (10 in the above example). Finally, the bar for each interval is painted in a grey color, where the colors of the leftmost and the rightmost intervals are black and white, respectively, and the darkness of bars monotonically decreases at the same rate from left to right. For instance, in Figure 1, the darkness levels of the four bars are 1, 2/3, 1/3, and 0, respectively. In this problem, you are requested to estimate ink consumption when printing a histogram on paper. The amount of ink necessary to draw a bar is proportional to both its area and darkness.

输入:

The input consists of multiple datasets, each of which contains integers and specifies a value table and intervals for the histogram generator, in the following format:
nw
v1
v2
.
.
.
vn
n is the total number of value occurrences for the histogram, and each of the n lines following the first line contains a single value. Note that the same value may possibly occur multiple times. w is the interval width. A value v is in the first (i.e. leftmost) interval if 0 ≤ v<w, the second one if w ≤ v< 2w, and so on. Note that the interval from 0 (inclusive) to w (exclusive) should be regarded as the leftmost even if no values occur in this interval. The last (i.e. rightmost) interval is the one that includes the largest value in the dataset.
You may assume the following:
1 ≤ n ≤ 100
10 ≤ w ≤ 50
0 ≤ vi ≤ 100
for 1 ≤ i ≤ n
You can also assume that the maximum value is no less than w. This means that the histogram has more than one interval. The end of the input is indicated by a line containing two zeros.

输出:

The input consists of multiple datasets, each of which contains integers and specifies a value table and intervals for the histogram generator, in the following format:
nw
v1
v2
.
.
.
vn
n is the total number of value occurrences for the histogram, and each of the n lines following the first line contains a single value. Note that the same value may possibly occur multiple times. w is the interval width. A value v is in the first (i.e. leftmost) interval if 0 ≤ v<w, the second one if w ≤ v< 2w, and so on. Note that the interval from 0 (inclusive) to w (exclusive) should be regarded as the leftmost even if no values occur in this interval. The last (i.e. rightmost) interval is the one that includes the largest value in the dataset.
You may assume the following:
1 ≤ n ≤ 100
10 ≤ w ≤ 50
0 ≤ vi ≤ 100
for 1 ≤ i ≤ n
You can also assume that the maximum value is no less than w. This means that the histogram has more than one interval. The end of the input is indicated by a line containing two zeros.

样例输入:

3 50
100
0
100
3 50
100
100
50
10 10
1
2
3
4
5
16
17
18
29
30
0 0

样例输出:

0.510000
0.260000
1.476667

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4153

这题是昨天杭电热身赛的题目,当时切了两道水题就跑了,晚上继续搞这题,英文不好理解,要不是有最后一组测试数据估计现在还在纠结。题目可以算是猜出的结果,再代入到第一根第二组测试数据对比,结果正确,貌似就算推对了Grey Area。也拿第三组测试数据来看。。。

Grey AreaGrey Area

1×1+2/3+3/5+1/3×1/5+0×1/5+0.01可以看成为:3/3×5/5+2/3+3/5+1/3×1/5+0/3×1/5+0.01。

这样看起来更有规律,在两个分数乘积的中,一个分母是3,另外一个是5。先说为什么是5,从左边的图片可以看出在0-9范围的值最大,就是5。10-19中为3,而3又占5中的3/5。20-29中为1,1占5中的1/5,30-39为1,也占1/5。所以以为什么以5分母的分数就可以求出来了。再说3。。。3/3,2/3,1/3,0,/3正好分别对应0-9,10-19,20-29,30-39。3是由测试数据里面的最大数30除以图像的间距0-9这10个数等于3,因此确定了3是分母,分子再逐级递减。最后在最后的结果上面加上0.01,保留6位小数,就是最终的结果了。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,m,i,t,max,mode,a[1001],count[1001];
    double ans;
    while(~scanf("%d %d",&n,&m)&&(n,m))
    {
        memset(a,0,sizeof(a));
        memset(count,0,sizeof(count));
        mode=-1;ans=0.01;
        for(i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        max=a[n-1]/m;
        for(i=0;i<=n-1;i++)
        {
            t=a[i]/m;
            count[t]++;
        }
        for(i=0;i<=max;i++)
        {
            if(count[i]>mode)
            {
                mode=count[i];
            }
        }
        for(i=max;i>=0;i--)
        {
            ans=ans+(double)(i)/max*count[max-i]/mode;
        }
        printf("%.6lf\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/a_eagle/article/details/7271858


  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.