首页 > ACM题库 > HDU-杭电 > HDU 2908-Grey Area[解题报告]HOJ
2014
02-21

HDU 2908-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.
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�9 occur five times, those in the interval 10�19 occur three times, and 20�29 and 30�39 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.

n w
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.

n w
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.51
0.26
1.476667

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int n, w;
int data[1000];
int interval [1000];

void solve()
{
 int k = 0;
 double sum = 0.01;

 sort(data + 1, data + n + 1);
 while(!(data[n] >= k*w && data[n] <= (k+1)*w-1))
 {
 k++;
 }
 memset(interval, 0, sizeof(interval));
 for(int i = 1; i <= n; i++)
 {
 int kk = 0;
 while(kk <= k)
 {
 if(data[i] >= kk*w && data[i] <= (kk+1)*w-1)
 {
 interval[kk] ++;
 break;
 }
 else
 {
 kk++;
 }
 }
 }
 int max = 0;
 for(int i = 0; i <= k; i++)
 {
 if(max < interval[i])
 {
 max = interval[i];
 }
 }
 for(int i = 0; i <= k; i++)
 {
 sum += ((double)interval[i]) / ((double)max) * (k-i) / k;
 }

 printf("%.6lf\n", sum);
}

int main()
{
 while(1)
 {
 scanf("%d%d", &n, &w);
 if(n == 0 && w == 0)
 {
 break;
 }
 else
 {
 for(int i = 1; i <= n; i++)
 {
 scanf("%d", &data[i]);
 }
 }
 solve();
 }

 return 0;
}

  1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  2. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。