zoj 3827 Information Entropy(2014牡丹江区域赛I题)
来源:互联网 发布:曾经最火的网络歌曲 编辑:程序博客网 时间:2024/06/05 20:47
Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy.
Entropy is the average amount of information contained in each message received. Here, a message stands for an event, or a sample or a character drawn from a distribution or a data stream. Entropy thus characterizes our uncertainty about our source of information. The source is also characterized by the probability distribution of the samples drawn from it. The idea here is that the less likely an event is, the more information it provides when it occurs.
Generally, "entropy" stands for "disorder" or uncertainty. The entropy we talk about here was introduced by Claude E. Shannon in his 1948 paper "A Mathematical Theory of Communication". We also call it Shannon entropy or information entropy to distinguish from other occurrences of the term, which appears in various parts of physics in different forms.
Named after Boltzmann's H-theorem, Shannon defined the entropy Η (Greek letter Η, η) of a discrete random variable X with possible values {x1, x2, ..., xn} and probability mass functionP(X) as:
Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as
Where b is the base of the logarithm used. Common values of b are 2, Euler's number e, and 10. The unit of entropy is bit for b = 2, nat for b = e, and dit (or digit) for b = 10 respectively.
In the case of P(xi) = 0 for some i, the value of the corresponding summand 0 logb(0) is taken to be a well-known limit:
Your task is to calculate the entropy of a finite sample with N values.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 100) and a string S. The string S is one of "bit", "nat" or "dit", indicating the unit of entropy.
In the next line, there are N non-negative integers P1, P2, .., PN. Pi means the probability of the i-th value in percentage and the sum of Pi will be 100.
Output
For each test case, output the entropy in the corresponding unit.
Any solution with a relative or absolute error of at most 10-8 will be accepted.
Sample Input
33 bit25 25 507 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10
Sample Output
1.5000000000001.4808108324651.000000000000
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N = 111;int a[N];int main(){ int T; scanf("%d", &T); while (T--) { int n; char s[5]; scanf("%d", &n); scanf("%s", s); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); double ans = 0; if (s[0] == 'b') { for (int i = 1; i <= n; ++i) { if (!a[i]) continue; ans += 1.0 * a[i] * log2((double) a[i] * 1.0 / 100.0); } } else if (s[0] == 'n') { for (int i = 1; i <= n; ++i) { if (!a[i]) continue; ans += 1.0 * a[i] * log((double) a[i] * 1.0 / 100.0); } } else if (s[0] == 'd') { for (int i = 1; i <= n; ++i) { if (!a[i]) continue; ans += 1.0 * a[i] * log10((double) a[i] * 1.0 / 100.0); } } ans /= 100; printf("%.12f\n", -ans); } return 0;}
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
- ZOJ3827 ACM-ICPC 2014 亚洲区域赛牡丹江现场赛I题 Information Entropy 水题
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
- zoj 3822 Domination(2014牡丹江区域赛D题)
- zoj 3827(牡丹江现场赛I题)
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- ZOJ 3819 Average Score (2014牡丹江区域赛)
- zoj 3827 Information Entropy(水题)
- ZOJ 3827 Information Entropy 水
- zoj 3827 Information Entropy 【水题】
- zoj 3829 Known Notation(2014年牡丹江区域赛k题)
- ZOJ 3817 Chinese Knot(牡丹江网络赛I题)
- 2014牡丹江现场赛A题D题I题(水,概率Dp,水)ZOJ 3819,3822,3827
- ZOJ 3805 Machine
- sdut 3-6 静态数据成员与静态成员函数
- wamp 虚拟目录的设置
- Mac OS使用技巧之八:Dock栏使用技巧
- Task和Back Stack(一)
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- 黑马程序员------foundation_NSArray,NSMutableArray
- Mac OS使用技巧之九:Mission Control和DIY自己的Dashboard
- zoj3822 Domination 牡丹江现场赛D题 概率DP
- 大学规划
- Mac OS使用技巧之十一:隐藏launchpad中图标的方法
- 小米4的相机在几个国产主流机(2014年10月间)不是最好的【视频评测】推荐
- string strtok的用法
- Ubuntu Linux: Uninstall / Remove Any Installed Software