ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题
Information Entropy
Time Limit: 2 Seconds Memory Limit: 131072 KB Special Judge
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
3
3 bit
25 25 50
7 nat
1 2 4 8 16 32 37
10 dit
10 10 10 10 10 10 10 10 10 10
Sample Output
1.500000000000
1.480810832465
1.000000000000
牡丹江赛区的第二道签到题。难度不大,基本A题通过后全部人I题也開始非常快通过,题目看起来非常难理解,可是一旦看懂事实上发现这事实上就是一道英文题。题目就是给一堆序列。然后给n个数a1,a2...an。然后对每一个数字求出(ai/sum)*log(ai/sum)的总和,当bit是,log以2为底,当nat时,log以e为底,当dit时。log以10为底,之后计算证明当ai=0时,对结果无影响,所以能够直接忽略,然后直接带入计算就可以AC此题~~~详细AC代码例如以下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[105];
int main()
{
// freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
int n;
string s;
scanf("%d",&n);
cin>>s;
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
double res=0;
if(s=="bit")
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log2(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
else if(s=="nat")
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log10(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
printf("%.12f\n",res);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题的更多相关文章
- ZOJ3819 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江司A称号 Average Score 注册标题
Average Score Time Limit: 2 Seconds Memory Limit: 131072 KB Bob is a freshman in Marjar Univers ...
- 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...
- [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...
- 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)-L Bit Sequence
题意 给你两个数l,m,大小为m的数组a,求[0,l]之间满足以下条件的数x的个数: 对于任何i输入[0,m-1],f(x+i)%2=a[i]:f(k):代表k在二进制下1的个数 m的范围<=1 ...
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem
2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem 题意: 给定一个长度为\(n\)的序列,有两种操作: 1:单点修改. 2:查询区间\([L,R]\)范围内所有子 ...
- ICPC 2018 亚洲横滨赛 C Emergency Evacuation(暴力,贪心)
ICPC 2018 亚洲横滨赛 C Emergency Evacuation 题目大意 你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值 Solution 题目咋说就咋做 直接模拟 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
随机推荐
- web开发性能优化---用户体验篇
怎样从技术角度怎样增强用户体验.都是非常多平台都在做的事情,依据个人实际经验碰到几种体验做下总结. 1.降低页面刷新白屏 适当使用ajax技术.改善刷新白屏现象. 2.信息提醒,邮件.站内信.短信在购 ...
- oracle 之 内存—鞭辟近里(三)
oracle 之 内存—鞭辟近里(三) 今天是2013-07-08,今天晚上突然接到一个电话,我的外甥问我的qq是多少,我感觉很吃惊,他长大了.在他现在这个年龄就开始接触网络,我难免有少许担心,希望他 ...
- ExtJs 设置GridPanel表格文本垂直居中
业务场景,需要实现最终效果图如下: GridPanel代码如下配置: { xtype : 'grid', id : 'grid_jglb', frame : true, region : 'cente ...
- 再读TCP/IP网络7层协议
随着工作的深入,每次读这7层协议,每次都有不同的理解. 分层名 分层号 ...
- 快速入门github的方法
Sometimes you just need a little help. ps:官方的帮助,原汁原味的帮助!强烈推荐 https://help.github.com/ Pro Git Book C ...
- 西南民大oj(矩阵快速幂)
我的名字不可能那么难记 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 16 测试通过 : ...
- BGP的状态机制
Idle 状态:即空闲状态,不接受任何BGP的连接,等待Start事件的产生,如果有start事件产生,若有start事件产生,系统开启ConnectRetry定时器,向邻居发起TCP连接,并将状态变 ...
- AppWidget应用(二)---PendingIntent 之 getActivity
通过AppWidget应用(一)的介绍,我们已经知道如何创建一个在主界面上显示一个appWidget窗口,但这并不是我们的目的,我们需要做到程序与用户之间进行交互:下面来介绍下如何通过appWidge ...
- WPF界面设计技巧(2)—自定义漂亮的按钮样式
原文:WPF界面设计技巧(2)-自定义漂亮的按钮样式 上次做了个很酷的不规则窗体,这次我们来弄点好看的按钮出来,此次将采用纯代码来设计按钮样式,不需要 Microsoft Expression Des ...
- Java命令参数说明
Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令 JAVA_HOME"bin"java –option 来启动,-option为虚 ...