Codeforces Round #364 (Div. 1) A B C D E

来源:互联网 发布:rem js计算font size 编辑:程序博客网 时间:2024/06/05 17:28


E题好艰难。

唉,补完去搞midterm了。

A. As Fast As Possible
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than kpeople at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.

Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected.

Input

The first line of the input contains five positive integers nlv1v2 and k (1 ≤ n ≤ 10 0001 ≤ l ≤ 1091 ≤ v1 < v2 ≤ 1091 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus.

Output

Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.

Examples
input
5 10 1 2 5
output
5.0000000000
input
3 6 1 2 1
output
4.7142857143
Note

In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.



数学题,推个公式。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int main() {int n,k;db T,v1,v2,l,ans,j;cin >> n >> l >> v1 >> v2 >> k;int s=n/k;if (n%k!=0) s++;ans=l*(2.0*(db)s+v1/v2-l)/(v2+v1*2.0*(db)s-v1);printf("%.10lf\n",ans);return 0;}

B. Connecting Universities
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.

In Treeland there are 2k universities which are located in different towns.

Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!

To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.

Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.

Input

The first line of the input contains two integers n and k (2 ≤ n ≤ 200 0001 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.

The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.

The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj(1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.

Output

Print the maximum possible sum of distances in the division of universities into k pairs.

Examples
input
7 21 5 6 21 33 24 53 74 34 6
output
6
input
9 33 2 1 6 5 98 93 22 73 47 64 52 12 8
output
9
Note

The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.



有一棵树,其中k*2个点有大学。现在让你把他们两两配对,使得每对的距离加起来最大。


树型DP。

dfs,如果一个点及其子树上点少于k个,则把它们向上配对;否则,把不属于子树的那些大学向下配对。


注意int会爆,需要long long.


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=200005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int head[maxn],size[maxn];bool visit[maxn],col[maxn];int num,k;ll ans;struct Edge {int from,to,pre;};Edge edge[maxn*2];void addedge(int from, int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}void dfs(int now) {visit[now]=1;size[now]=0;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {dfs(to);size[now]+=size[to];}}if (col[now]) size[now]++;if (now!=1) if (size[now]<=k) ans+=(ll)size[now]; else ans+=(ll)(2*k-size[now]); }int main() {int n,i,j,x,y;num=ans=0;scanf("%d%d",&n,&k);mem0(col);memset(head,-1,sizeof(head));for (i=1;i<=2*k;i++) {scanf("%d",&x);col[x]=1;}for (i=1;i<n;i++) {scanf("%d%d",&x,&y);addedge(x,y);}ans=0;mem0(visit);dfs(1);printf("%I64d\n",ans);return 0;}

C. Break Up   tarjan算法求桥


D. Huffman Coding on Segment   莫队算法


E. Cool Slogans   后缀数组+线段树


原创粉丝点击