luogu#P6915. [ICPC2015 WF] Weather Report

[ICPC2015 WF] Weather Report

题目描述

You have been hired by the Association for Climatological Measurement, a scientific organization interested in tracking global weather trends over a long period of time. Of course, this is no easy task. They have deployed many small devices around the world, designed to take periodic measurements of the local weather conditions. These are cheap devices with somewhat restricted capabilities. Every day they observe which of the four standard kinds of weather occurred: Sunny, Cloudy, Rainy, or Frogs. After every nn of these observations have been made, the results are reported to the main server for analysis. However, the massive number of devices has caused the available communication bandwidth to be overloaded. The Association needs your help to come up with a method of compressing these reports into fewer bits.

For a particular device’s location, you may assume that the weather each day is an independent random event, and you are given the predicted probabilities of the four possible weather types. Each of the 4n4^ n possible weather reports for a device must be encoded as a unique sequence of bits, such that no sequence is a prefix of any other sequence (an important property, or else the server would not know when each sequence ends). The goal is to use an encoding that minimizes the expected number of transmitted bits.

输入格式

The first line of input contains an integer 1n201 \le n \le 20, the number of observations that go into each report. The second line contains four positive floating-point numbers, psunnyp_{\text {sunny}}, pcloudyp_{\text {cloudy}}, prainyp_{\text {rainy}}, and pfrogsp_{\text {frogs}}, representing the respective weather probabilities. These probabilities have at most 6 digits after the decimal point and sum to 1.

输出格式

Display the minimum expected number of bits in the encoding of a report, with an absolute or relative error of at most 10410^{-4}.

题目大意

给定4种天气情况出现的概率,你需要将n天的所有可能的 4n4^{n} 种情况已某种方式编码为01串,使得:

  • 编码长度的期望最短
  • 任何一个编码不是另一个编码的前缀

求期望长度。

2
0.9 0.049999 0.05 0.000001

1.457510

20
0.25 0.25 0.25 0.25

40.000000

提示

Time limit: 1000 ms, Memory limit: 1048576 kB.

International Collegiate Programming Contest (ACM-ICPC) World Finals 2015