The 2014 ACM-ICPC Asia Mudanjiang Regional

继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了

 

【A】签到题

【B】树结构,树的中心

【C】-_-///

【D】概率DP

【E】-_-///

【F】树结构填数

【G】-_-///

【H】模拟

【I】签到题

【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的最终位置了。

 

然后需要注意的是根据树的直径的结点个数,要分奇数和偶数分别讨论。偶数个结点只要从中间一条边切开即可,但是奇数个结点的情况就要继续分成两种情况,考虑中点分别切给左边那个子树和切给右边那棵子树的结果,取距离最小的那一个。

  1 /* ***********************************************
  2 MYID    : Chen Fan
  3 LANG    : G++
  4 PROG    : ZOJ 3820
  5 ************************************************ */
  6 
  7 #include <iostream>
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <algorithm>
 11 #include <queue>
 12 #include <bitset>
 13 
 14 using namespace std;
 15 
 16 typedef struct nod
 17 {
 18     int a,b;
 19 } node;
 20 
 21 node edge[400010];
 22 int start[200010],num[200010],front[200010],list[200010];
 23 int listn;
 24 
 25 bitset<200010> flag;
 26 
 27 bool op(node a,node b)
 28 {
 29     if (a.a==b.a) return a.b<b.b;
 30     else return a.a<b.a;
 31 }
 32 
 33 int bfs(int s)
 34 {
 35     queue<int> q;
 36     while (!q.empty()) q.pop();
 37     int dist[200010];
 38 
 39     memset(dist,0,sizeof(dist));
 40     flag[s]=1;
 41     q.push(s);
 42     int ma=0,out=s;
 43     while (!q.empty())
 44     {
 45         int now=q.front();
 46         q.pop();
 47         for (int i=0;i<num[now];i++)
 48         {
 49             int next=edge[start[now]+i].b;
 50             if (!flag[next])
 51             {
 52                 flag[next]=1;
 53                 front[next]=now;
 54                 q.push(next);
 55                 dist[next]=dist[now]+1;
 56                 if (ma<dist[next])
 57                 {
 58                     ma=dist[next];
 59                     out=next;
 60                 }
 61             }
 62         }
 63     }
 64 
 65     return out;
 66 }
 67 
 68 void getlist(int s)
 69 {
 70     if (!front[s])
 71     {
 72         listn=1;
 73         list[1]=s;
 74         return ;
 75     }
 76     while (front[s]!=0)
 77     {
 78         listn++;
 79         list[listn]=s;
 80         s=front[s];
 81     }
 82     listn++;
 83     list[listn]=s;
 84 }
 85 
 86 int main()
 87 {
 88     freopen("3820.txt","r",stdin);
 89 
 90     int t;
 91     scanf("%d",&t);
 92     for (int tt=1;tt<=t;tt++)
 93     {
 94         int n;
 95         scanf("%d",&n);
 96         for (int i=1;i<n;i++)
 97         {
 98             scanf("%d%d",&edge[i*2].a,&edge[i*2].b);
 99             edge[i*2-1].a=edge[i*2].b;
100             edge[i*2-1].b=edge[i*2].a;
101         }
102         int m=n*2-2;
103         sort(&edge[1],&edge[m+1],op);
104         int o=0;
105         memset(num,0,sizeof(num));
106         for (int i=1;i<=m;i++)
107         {
108             if (o!=edge[i].a)
109             {
110                 o=edge[i].a;
111                 start[o]=i;
112             }
113             num[o]++;
114         }
115 
116         flag.reset();
117         int p1=bfs(1);
118         flag.reset();
119         memset(front,0,sizeof(front));
120         int p2=bfs(p1);
121 
122         listn=0;
123         memset(list,0,sizeof(list));
124         getlist(p2);
125         
126         if (listn&1)
127         {
128             int a=list[listn/2],b=list[listn/2+1],c=list[listn/2+2];
129 
130             flag.reset();
131             flag[b]=1;
132             p1=bfs(a);
133             flag.reset();
134             flag[b]=1;
135             memset(front,0,sizeof(front));
136             p2=bfs(p1);
137 
138             listn=0;
139             memset(list,0,sizeof(list));
140             getlist(p2);
141 
142             int ans1=listn/2;
143             int ansa1=list[listn/2+1];
144 
145             flag.reset();
146             flag[a]=1;
147             p1=bfs(b);
148             flag.reset();
149             flag[a]=1;
150             memset(front,0,sizeof(front));
151             p2=bfs(p1);
152 
153             listn=0;
154             memset(list,0,sizeof(list));
155             getlist(p2);
156 
157             ans1=max(ans1,listn/2);
158             int ansb1=list[listn/2+1];
159 
160             flag.reset();
161             flag[b]=1;
162             p1=bfs(c);
163             flag.reset();
164             flag[b]=1;
165             memset(front,0,sizeof(front));
166             p2=bfs(p1);
167 
168             listn=0;
169             memset(list,0,sizeof(list));
170             getlist(p2);
171 
172             int ans2=listn/2;
173             int ansa2=list[listn/2+1];
174 
175             flag.reset();
176             flag[c]=1;
177             p1=bfs(b);
178             flag.reset();
179             flag[c]=1;
180             memset(front,0,sizeof(front));
181             p2=bfs(p1);
182 
183             listn=0;
184             memset(list,0,sizeof(list));
185             getlist(p2);
186 
187             ans2=max(ans2,listn/2);
188             int ansb2=list[listn/2+1];
189 
190             if (ans1<ans2) printf("%d %d %d\n",ans1,ansa1,ansb1);
191             else printf("%d %d %d\n",ans2,ansa2,ansb2);
192         } else 
193         {
194             int a=list[listn/2],b=list[listn/2+1];
195 
196             flag.reset();
197             flag[b]=1;
198             p1=bfs(a);
199             flag.reset();
200             flag[b]=1;
201             memset(front,0,sizeof(front));
202             p2=bfs(p1);
203 
204             listn=0;
205             memset(list,0,sizeof(list));
206             getlist(p2);
207 
208             int ans1=listn/2;
209             int ansa1=list[listn/2+1];
210 
211             flag.reset();
212             flag[a]=1;
213             p1=bfs(b);
214             flag.reset();
215             flag[a]=1;
216             memset(front,0,sizeof(front));
217             p2=bfs(p1);
218 
219             listn=0;
220             memset(list,0,sizeof(list));
221             getlist(p2);
222 
223             ans1=max(ans1,listn/2);
224             int ansb1=list[listn/2+1];
225 
226             printf("%d %d %d\n",ans1,ansa1,ansb1);
227         }
228     }
229 
230     return 0;
231 }
View Code

 

 

【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的总和即可。

简单的方程如下:

1 f(i,j,k) = Sum
2 {
3     f(i,j,k+1)*(i*j-k)/(n*m-k),
4     f(i+1,j,k+1)*(n-i)*j/(n*m-k),
5     f(i,j+1,k+1)*i*(m-j)/(n*m-k),
6     f(i+1,j+1,k+1)*(n-i)*(m-j)/(n*m-k)
7 }

方程中需要尤其注意的是当达成n行m列全部被控制之后,就可以结束了,即转移中f(n,m,k)->f(n,m,k+1)是不存在的!!!!因为这个原因悲剧了好久,后来对比其他人的代码才发现这个重大的问题。。。

 1 /* ***********************************************
 2 MYID    : Chen Fan
 3 LANG    : G++
 4 PROG    : ZOJ 3822
 5 ************************************************ */
 6 /* ***********************************************
 7 f(i,j,k) = Sum
 8 {
 9     f(i,j,k+1)*(i*j-k)/(n*m-k),
10     f(i+1,j,k+1)*(n-i)*j/(n*m-k),
11     f(i,j+1,k+1)*i*(m-j)/(n*m-k),
12     f(i+1,j+1,k+1)*(n-i)*(m-j)/(n*m-k)
13 }
14 ************************************************ */
15 
16 #include <iostream>
17 #include <cstdio>
18 #include <cstring>
19 #include <algorithm>
20 
21 using namespace std;
22 
23 double f[60][60][4000];
24 
25 int main()
26 {
27     int t;
28     scanf("%d",&t);
29     for (int tt=1;tt<=t;tt++)
30     {
31         int n,m;
32         scanf("%d%d",&n,&m);
33         memset(f,0,sizeof(f));
34         
35         f[1][1][1]=1;
36         for (int k=1;k<=m*n;k++)
37         for (int i=1;i<=n;i++)
38         for (int j=1;j<=m;j++)
39         {
40             if (k<max(i,j)) continue;
41             if (k+1<=i*j&&i!=n||j!=m)
42             f[i][j][k+1]+=f[i][j][k]*(i*j-k)/(n*m-k);
43             if (k+1<=(i+1)*j)
44             f[i+1][j][k+1]+=f[i][j][k]*(n-i)*j/(n*m-k);
45             if (k+1<=i*(j+1))
46             f[i][j+1][k+1]+=f[i][j][k]*i*(m-j)/(n*m-k);
47             if (k+1<=(i+1)*(j+1))
48             f[i+1][j+1][k+1]+=f[i][j][k]*(n-i)*(m-j)/(n*m-k);
49         }
50 
51         double ans=0;
52         for (int i=max(n,m);i<=m*n;i++) ans+=f[n][m][i]*i;
53 
54         printf("%.10f\n",ans);
55     }
56 
57     return 0;
58 }
View Code

 

 

【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:

H(X)=E(ln(P(x)))

 

Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

H(X)=i=1nP(xi)log b(P(xi))

 

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:

0log b(0)=limp0+plog b(p)

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

【分析】

签到题,难点可能是在题面上,学过信息论的话看到样例就能直接打代码了。

 

posted @ 2014-10-30 08:58  辰帆  阅读(307)  评论(0编辑  收藏  举报