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 variableX with possible values
{x1, x2, ..., xn} and probability mass functionP(X) as:



rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">
rev=2.5.3" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">
rev=2.5.3" alt="" style="height:14px; width:15px; margin-right:-0.02em">
rev=2.5.3" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">
rev=2.5.3" alt="" style="height:14px; width:6px; margin-right:0.01em">




rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">
rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">
Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">



rev=2.5.3" alt="" style="height:9px; width:6px; margin-right:0.07em">





rev=2.5.3" alt="" style="height:14px; width:6px; margin-right:0.01em">





rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" width="6" height="21" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">
Where b is the base of the logarithm used. Common values of b are 2, Euler's numbere, 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:



rev=2.5.3" alt="" style="height:13px; width:10px; vertical-align:-4px; margin-right:0.01em">


rev=2.5.3" alt="" style="height:14px; width:9px; margin-right:0.04em">
rev=2.5.3" width="15" height="5" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">
rev=2.5.3" width="6" height="14" alt="" style="height:14px; width:6px; margin-right:0.01em">



rev=2.5.3" alt="" style="height:9px; width:7px; margin-right:0.04em">



rev=2.5.3" alt="" style="height:13px; width:10px; vertical-align:-4px; margin-right:0.01em">
rev=2.5.3" alt="" style="height:1px; width:1px; margin-right:0.24em">
rev=2.5.3" alt="" style="height:9px; width:6px; margin-right:0em">
rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">
rev=2.5.3" alt="" style="height:13px; width:11px; vertical-align:-4px; margin-left:-0.03em; margin-right:0em">
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 stringS. 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 thei-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
题意:给你N个数和一个字符串str。 若str为bit。则计算sigma( - log2a[i])(1 <= i <= N); str为nat时,计算sigma(- loga[i])(1 <= i <= N); str为dit时,计算sigma(- log10a[i])(1 <= i <= N)。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
#define MAXN 1000
#define MAXM 100000
using namespace std;
int main()
{
int t;
int N;
char str[10];
double a[110];
scanf("%d", &t);
while(t--)
{
scanf("%d%s", &N, str);
double sum = 0;
for(int i = 0; i < N; i++)
scanf("%lf", &a[i]), sum += a[i];
double ans = 0;
if(strcmp(str, "bit") == 0)
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log2(a[i] / sum) * (a[i] / sum);
}
}
else if(strcmp(str, "nat") == 0)
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log(a[i] / sum) * (a[i] / sum);
}
}
else
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log10(a[i] / sum) * (a[i] / sum);
}
}
printf("%.12lf\n", ans);
}
return 0;
}
zoj 3827 Information Entropy 【水题】的更多相关文章
- ZOJ 3827 Information Entropy 水题
Information Entropy Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/sh ...
- ZOJ 3827 Information Entropy 水
水 Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Informati ...
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...
- 2014 牡丹江现场赛 i题 (zoj 3827 Information Entropy)
I - Information Entropy Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...
- ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题
Information Entropy Time Limit: 2 Seconds Memory Limit: 131072 KB Special Judge Informatio ...
- [ACM] ZOJ 3819 Average Score (水题)
Average Score Time Limit: 2 Seconds Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...
- ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题
2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
随机推荐
- win2008通过计划任务定时执行bat文件
前段时间在Windows Server 2008安装了一套基于MySQL数据库的软件,处于数据安全的考虑,希望每天能够自动进行数据库备份.我在别人脚本的基础上自己写了一个数据库备份的bat脚本,双击该 ...
- hadoop之深入浅出
分布式文件系统与HDFS lHDFS体系结构与基本概念*** l数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管 ...
- POJ 2104 K-th Number (划分树)
K-th Number Time Limit: 20000MS Memory ...
- 信息批量提取工具bulk-extractor
信息批量提取工具bulk-extractor 在数字取证中,通常需要面对海量的数据,如几百GB甚至TB级别的数据.从这些海量数据中,提取有价值的数据是一个漫长.枯燥.繁琐的过程.Kali Linu ...
- 一个错误使用单例模式的场景及ThreadLocal简析
近来参与一个Java的web办公系统,碰到一个bug,开始猜测是线程池管理的问题,最后发现是单例模式的问题. 即,当同时发起两个事务请求时,当一个事务完成后,另一个事务会抛出session is cl ...
- luogu P1529 回家 Bessie Come Home
题目描述 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶 ...
- C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)
https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...
- SSMS查看表行数以及使用空间 How to show table row count and space used in SSMS - SSMS Tutorials
原文:How to show table row count and space used in SSMS - SSMS Tutorials There's a quick and convenien ...
- Get Started with Subversion using SvnX
A very important part of a development environment is source code control. Subversion is the server- ...
- [置顶]
docker1.12--docker命令行帮助文档
镜像操作 build 使用dockerfile方式创建镜像 使用dockerfile文件docker build dockerflle/,dockerfile文件默认位于dockerflle/Dock ...