The 2014 ACM-ICPC Asia Mudanjiang Regional
继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了
【C】-_-///
【E】-_-///
【F】树结构填数
【G】-_-///
【H】模拟
【J】-_-///
【K】贪心,构造后缀表达式
【A】 ZOJ 3819
Average Score
Time Limit: 2 Seconds Memory Limit: 65536 KB
Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis.
After a mid-term exam, Bob was anxious about his grade. He went to the professor asking about the result of the exam. The professor said:
"Too bad! You made me so disappointed."
"Hummm... I am giving lessons to two classes. If you were in the other class, the average scores of both classes will increase."
Now, you are given the scores of all students in the two classes, except for the Bob's. Please calculate the possible range of Bob's score. All scores shall be integers within [0, 100].
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 two integers N (2 <= N <= 50) and M (1 <= M <= 50) indicating the number of students in Bob's class and the number of students in the other class respectively.
The next line contains N - 1 integers A1, A2, .., AN-1 representing the scores of other students in Bob's class.
The last line contains M integers B1, B2, .., BM representing the scores of students in the other class.
Output
For each test case, output two integers representing the minimal possible score and the maximal possible score of Bob.
It is guaranteed that the solution always exists.
Sample Input
2
4 3
5 5 5
4 4 3
6 5
5 5 4 5 3
1 3 2 2 1
Sample Output
4 4
2 4
【分析】
签到,写个方程稍微推一下即可得到区间
【B】 ZOJ 3820
Building Fire Stations
Time Limit: 5 Seconds Memory Limit: 131072 KB Special Judge
Marjar University is a beautiful and peaceful place. There are N buildings and N - 1 bidirectional roads in the campus. These buildings are connected by roads in such a way that there is exactly one path between any two buildings. By coincidence, the length of each road is 1 unit.
To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup two fire stations in two different buildings so that firefighters are able to arrive at the scene of the fire as soon as possible whenever fires occur. That means the longest distance between a building and its nearest fire station should be as short as possible.
As a clever and diligent student in Marjar University, you are asked to write a program to complete the plan. Please find out two proper buildings to setup the fire stations.
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 (2 <= N <= 200000).
For the next N - 1 lines, each line contains two integers Xi and Yi. That means there is a road connecting building Xi and building Yi (indexes are 1-based).
Output
For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next two integers are the indexes of the two buildings selected to build the fire stations.
If there are multiple solutions, any one will be acceptable.
Sample Input
2
4
1 2
1 3
1 4
5
1 2
2 3
3 4
4 5
Sample Output
1 1 2
1 2 4
【题意】
题目给出一棵树作为一个城镇的地图,现在要在地图中设置两个救火点,使得最后任意一点发生火灾,救火都能及时到达,即使得救火点到达每个城镇的最大距离最小。
【分析】
首先比较明显的能想到基于树的直径做一些文章。
浙大的出题者在他的人人日志中给出了本题结论的证明:
http://blog.renren.com/blog/240107793/937020122?bfrom=01020100200
最终得出的结论是,两个点a、b必然都在树的直径上。(结论1)
之后的思路有两种:
1. 作者的正解是扫出直径之后对每一个点向外扩展一次,得到每个点外延的最大距离,然后将其转化为一个一维的RMQ问题,二分一下区间即可。
2. 第一次得到树的直径之后,从直径的中点切开,对两侧的两棵子树分别再做一次树的直径,找到的两个直径的中点就是最终的a和b。
关于思路2的证明:
1)子树的直径的中点距离这棵子树剩余的点一定是最远的,则这个点满足作为a、b点的条件。
2)然后简单推测一下,子树的直径中点一定在原树的直径上。这一点与结论1相吻合。
这个简单地举几个例子试一下就好,跟上面那个结论一样,能简单地想到大概是这样,但是原谅我数学不好......写不出证明。
3)上面两个结论组合一下,最后的交点就是a、b的最终位置了。
然后需要注意的是根据树的直径的结点个数,要分奇数和偶数分别讨论。偶数个结点只要从中间一条边切开即可,但是奇数个结点的情况就要继续分成两种情况,考虑中点分别切给左边那个子树和切给右边那棵子树的结果,取距离最小的那一个。
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : ZOJ 3820
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <bitset> using namespace std; typedef struct nod
{
int a,b;
} node; node edge[];
int start[],num[],front[],list[];
int listn; bitset<> flag; bool op(node a,node b)
{
if (a.a==b.a) return a.b<b.b;
else return a.a<b.a;
} int bfs(int s)
{
queue<int> q;
while (!q.empty()) q.pop();
int dist[]; memset(dist,,sizeof(dist));
flag[s]=;
q.push(s);
int ma=,out=s;
while (!q.empty())
{
int now=q.front();
q.pop();
for (int i=;i<num[now];i++)
{
int next=edge[start[now]+i].b;
if (!flag[next])
{
flag[next]=;
front[next]=now;
q.push(next);
dist[next]=dist[now]+;
if (ma<dist[next])
{
ma=dist[next];
out=next;
}
}
}
} return out;
} void getlist(int s)
{
if (!front[s])
{
listn=;
list[]=s;
return ;
}
while (front[s]!=)
{
listn++;
list[listn]=s;
s=front[s];
}
listn++;
list[listn]=s;
} int main()
{
freopen("3820.txt","r",stdin); int t;
scanf("%d",&t);
for (int tt=;tt<=t;tt++)
{
int n;
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d%d",&edge[i*].a,&edge[i*].b);
edge[i*-].a=edge[i*].b;
edge[i*-].b=edge[i*].a;
}
int m=n*-;
sort(&edge[],&edge[m+],op);
int o=;
memset(num,,sizeof(num));
for (int i=;i<=m;i++)
{
if (o!=edge[i].a)
{
o=edge[i].a;
start[o]=i;
}
num[o]++;
} flag.reset();
int p1=bfs();
flag.reset();
memset(front,,sizeof(front));
int p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); if (listn&)
{
int a=list[listn/],b=list[listn/+],c=list[listn/+]; flag.reset();
flag[b]=;
p1=bfs(a);
flag.reset();
flag[b]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); int ans1=listn/;
int ansa1=list[listn/+]; flag.reset();
flag[a]=;
p1=bfs(b);
flag.reset();
flag[a]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); ans1=max(ans1,listn/);
int ansb1=list[listn/+]; flag.reset();
flag[b]=;
p1=bfs(c);
flag.reset();
flag[b]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); int ans2=listn/;
int ansa2=list[listn/+]; flag.reset();
flag[c]=;
p1=bfs(b);
flag.reset();
flag[c]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); ans2=max(ans2,listn/);
int ansb2=list[listn/+]; if (ans1<ans2) printf("%d %d %d\n",ans1,ansa1,ansb1);
else printf("%d %d %d\n",ans2,ansa2,ansb2);
} else
{
int a=list[listn/],b=list[listn/+]; flag.reset();
flag[b]=;
p1=bfs(a);
flag.reset();
flag[b]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); int ans1=listn/;
int ansa1=list[listn/+]; flag.reset();
flag[a]=;
p1=bfs(b);
flag.reset();
flag[a]=;
memset(front,,sizeof(front));
p2=bfs(p1); listn=;
memset(list,,sizeof(list));
getlist(p2); ans1=max(ans1,listn/);
int ansb1=list[listn/+]; printf("%d %d %d\n",ans1,ansa1,ansb1);
}
} return ;
}
【D】 ZOJ 3822
Domination
Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge
Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns.
Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.
"That's interesting!" Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.
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:
There are only two integers N and M (1 <= N, M <= 50).
Output
For each test case, output the expectation number of days.
Any solution with a relative or absolute error of at most 10-8 will be accepted.
Sample Input
2
1 3
2 2
Sample Output
3.000000000000
2.666666666667
【题意】
有一个棋盘,目标是在棋盘中摆放棋子,使得最终每一个格子都能够被控制,这里控制的意思是满足这个格子的同行和同列上都有棋子存在。最终需要求出的是达成这种条件的期望。
【分析】
比较裸的概率DP...原谅我看到的第一眼居然没想到...还是做题太少啊
f(i,j,k)表示使用k个棋子,控制完n行中的任意i行,m列中的任意j列所需要的概率。则最后的期望就是求一个f(n,m,k)*k的总和即可。
简单的方程如下:
f(i,j,k) = Sum
{
f(i,j,k+)*(i*j-k)/(n*m-k),
f(i+,j,k+)*(n-i)*j/(n*m-k),
f(i,j+,k+)*i*(m-j)/(n*m-k),
f(i+,j+,k+)*(n-i)*(m-j)/(n*m-k)
}
方程中需要尤其注意的是当达成n行m列全部被控制之后,就可以结束了,即转移中f(n,m,k)->f(n,m,k+1)是不存在的!!!!因为这个原因悲剧了好久,后来对比其他人的代码才发现这个重大的问题。。。
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : ZOJ 3822
************************************************ */
/* ***********************************************
f(i,j,k) = Sum
{
f(i,j,k+1)*(i*j-k)/(n*m-k),
f(i+1,j,k+1)*(n-i)*j/(n*m-k),
f(i,j+1,k+1)*i*(m-j)/(n*m-k),
f(i+1,j+1,k+1)*(n-i)*(m-j)/(n*m-k)
}
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; double f[][][]; int main()
{
int t;
scanf("%d",&t);
for (int tt=;tt<=t;tt++)
{
int n,m;
scanf("%d%d",&n,&m);
memset(f,,sizeof(f)); f[][][]=;
for (int k=;k<=m*n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
{
if (k<max(i,j)) continue;
if (k+<=i*j&&i!=n||j!=m)
f[i][j][k+]+=f[i][j][k]*(i*j-k)/(n*m-k);
if (k+<=(i+)*j)
f[i+][j][k+]+=f[i][j][k]*(n-i)*j/(n*m-k);
if (k+<=i*(j+))
f[i][j+][k+]+=f[i][j][k]*i*(m-j)/(n*m-k);
if (k+<=(i+)*(j+))
f[i+][j+][k+]+=f[i][j][k]*(n-i)*(m-j)/(n*m-k);
} double ans=;
for (int i=max(n,m);i<=m*n;i++) ans+=f[n][m][i]*i; printf("%.10f\n",ans);
} return ;
}
【I】 ZOJ 3827
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 function P(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
【分析】
签到题,难点可能是在题面上,学过信息论的话看到样例就能直接打代码了。
The 2014 ACM-ICPC Asia Mudanjiang Regional的更多相关文章
- hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)
Mart Master II Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP
Clone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Submiss ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- 2014 ACM/ICPC Asia Regional Shanghai Online
Tree http://acm.hdu.edu.cn/showproblem.php?pid=5044 树链剖分,区间更新的时候要用on的左++右--的标记方法,要手动扩栈,用c++交,综合以上的条件 ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...
- 2014 ACM/ICPC Asia Regional Anshan Online
默默的签到 Osu! http://acm.hdu.edu.cn/showproblem.php?pid=5003 #include<cstdio> #include<algorit ...
- 2014 ACM/ICPC Asia Regional 北京 Online
G - Grade Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushroo ...
- 2014 ACM/ICPC Asia Regional Xi'an Online
03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为 ...
- HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)
思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’ 'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...
- HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...
随机推荐
- OSPF 原理
关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...
- Form表单的post 和get跳转区别
post是隐示请求 ----- 安全 get显示请求不安全,会在URL上显示路径和参数
- 百度地图API地点搜索-获取经纬度
分享一下地图上的地点搜索和鼠标点击获取地点经纬度,这些都是地图比较基本和实用的代码,其中还包括了根据用户IP进行地图的显示.改变地图上的鼠标样式.启用滚轮缩放等,算是半入门吧,其他的一些可以自己参考百 ...
- 当使用System,out.println()打印一个对象是自动调用toString方法
在Java中,所有的对象都是继承自Object,自然继承了toString方法,在当使用System,out.println()里面为一个对象的引用时,自动调用toString方法讲对象打印出来.如果 ...
- CSS3简介
选择器 盒模型 背景和边框 文字特效 2d/3d转换 动画 多列布局 用户界面
- CentOS下编译安装Apache2(新)
官网下载apache,apr, apr-util,pcre httpd-2.4.16.tar.gz http://httpd.apache.org/download.cgi#apache24 apr- ...
- [原创] web_custom_request 与 Viewstate
在用loadrunner对.net编写的website进行性能测试时,经常会遇上一些hidden fields,例如,CSRFTOKEN.VIEWSTATE.EVENTVALIDATION等,而对于这 ...
- CodeForces 190D Non-Secret Cypher
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- Angularjs循环二维数组
<div ng-app> <div ng-controller="test"> <div ng-repeat="links in slide ...
- POJ 1821 单调队列+dp
题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...