2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy
Time Limit: 2 Seconds Memory Limit: 65536 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
Author: ZHOU, Yuchen
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
题目链接: problemId=5381">Information Entropy
解题思路:又是一道签到题!!!当时想多了。竟然纠结在积分上,果真是想多了,根本不须要什么技术,就是依照那个数学公式算。又学到了个小方法。表示对数能够用log(m, n)= log(n) / log(m). 还有就是e = exp(1)。
还有就是注意当p = 0时,函数值为0。
AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff #define e exp(1.0) int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int T, n, p;
string s;
scanf("%d",&T);
while(T--)
{
double ans = 0;
scanf("%d",&n);
cin>>s;
if(s == "bit"){
for(int i=0; i<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(2));
}
}
else if(s == "nat"){
for(int i=0; i<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(e));
}
}
else{
for(int i=0; i<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(10));
}
}
printf("%.14lf\n",ans);
}
return 0;
}
2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy的更多相关文章
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score
Average Score Time Limit: 2 Seconds Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结
不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...
- 2014 牡丹江现场赛 i题 (zoj 3827 Information Entropy)
I - Information Entropy Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- 2013ACM/ICPC亚洲区南京站现场赛---Poor Warehouse Keeper(贪心)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4803 Problem Description Jenny is a warehouse keeper. ...
随机推荐
- Mac下Apache服务器的初步搭建
回送地址 127.0.0.1(localhost) ping 这个地址可以检测网卡是否正常 ping 本地地址如果不正常说明网线挂了 // 启动 sudo apachectl -k start ...
- 解决普遍pc端公共底部永远在下面框架
<div style="width: 90%;height: 3000px;margin: 0 auto; background: red;"></div> ...
- 树莓派 Centos7 安装EPEL 7
cat > /etc/yum.repos.d/epel.repo << EOF [epel] name=Epel rebuild for armhfp baseurl=https:/ ...
- AF_NETLINK
读procd时,hotplug相关. 从网上资料来看,大概和硬件输入有关? 资料:http://www.cnblogs.com/sammei/p/4119659.html
- running Fluent on Apocrita Cluster
two files: code.sh, code.jou code.sh #!/bin/bash #$ -cwd #$ -j y #$ -m bea #$ -M k.ai@qmul.ac.uk #$ ...
- python和搜索
# -*- coding: UTF-8 -*- import re # 搜索逻辑 def querylogic(list): query = {} if len(list) > 1 or len ...
- Android渐变GradientDrawable叠加组合环ring
Android渐变GradientDrawable叠加组合环ring 写一个Android环形shape之间的叠加组合形成新图像的例子.代码: <?xml version="1. ...
- 同步OR异步?WebFlux开发真的比Servlet开发要快?顺便再科普下CompletableFuture
在看下文之前,先给大家科普一点基础知识 Runable:线程任务类接口,没有返回值 Callable:与上面的不同就是有返回值 Executor:定义了线程池执行任务的接口,不过只定义了Runable ...
- 约分差束 例题 ZOJ 2770 火烧连营
题目来源:ZOJ Monthly, October 2006, ZOJ2770题目描述:大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了.刘备失败的原因是刘备的错误决策.他把军队分成几十个大营,每 ...
- APP后端处理视频的方案
在当前的app应用中,到处都能看到视频的身影,例如,在社交类的app上,用户可以拍摄属于自己的小视频,并发布到相应得栏目,增加和好友们互动的机会. 后台常见的视频处理有以下几种: · ...