Codeforces Round #428 (Div. 2) A B C

来源:互联网 发布:使命召唤ol流畅优化 编辑:程序博客网 时间:2024/06/14 00:01
A. Arya and Bran
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.

At first, Arya and Bran have 0 Candies. There aren days, at the i-th day, Arya findsai candies in a box, that is given by the Many-Faced God. Every day she can give Branat most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.

Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of then-th day. Formally, you need to output the minimum day index to the end of whichk candies will be given out (the days are indexed from 1 ton).

Print -1 if she can't give him k candies during n given days.

Input

The first line contains two integers n andk (1 ≤ n ≤ 100,1 ≤ k ≤ 10000).

The second line contains n integers a1, a2, a3, ..., an (1 ≤ ai ≤ 100).

Output

If it is impossible for Arya to give Bran k candies withinn days, print -1.

Otherwise print a single integer — the minimum number of days Arya needs to give Brank candies before the end of the n-th day.

Examples
Input
2 31 2
Output
2
Input
3 1710 10 10
Output
3
Input
1 910
Output
-1
Note

In the first sample, Arya can give Bran 3 candies in2 days.

In the second sample, Arya can give Bran 17 candies in3 days, because she can give him at most 8 candies per day.

In the third sample, Arya can't give Bran 9 candies, because she can give him at most8 candies per day and she must give him the candies within1 day.

Code:

#include <bits\stdc++.h>using namespace std;int main(){int n,k;int res = -1;int sum = 0;int x;int add = 0;scanf("%d%d",&n,&k);int flag = 1;for( int i = 1 ; i <= n ; i++ ){scanf("%d",&x);if( x > 8 ) { add += (x - 8) ; x = 8; }else if( x < 8 ){if( x + add >= 8 ){add -= ( 8 - x );x = 8; }else{x += add;add = 0;} } sum += x;if( sum >= k  && flag ) { res = i , flag = 0;}}printf("%d\n",res);return 0;}

B. Game of the Rows
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Daenerys Targaryen has an army consisting of k groups of soldiers, thei-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has8 seats. We call two seats neighbor, if they are in the same row and in seats{1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.

A row in the airplane

Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.

Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.

Input

The first line contains two integers n andk (1 ≤ n ≤ 10000,1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.

The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), whereai denotes the number of soldiers in thei-th group.

It is guaranteed that a1 + a2 + ... + ak ≤ 8·n.

Output

If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).

You can choose the case (lower or upper) for each letter arbitrary.

Examples
Input
2 25 8
Output
YES
Input
1 27 1
Output
NO
Input
1 24 4
Output
YES
Input
1 42 2 1 2
Output
YES
Note

In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.

In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.

In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).


思路:  看了题解才弄清楚。。一直处理很麻烦--> 把数拆成 4 + 2 + 1  这样的,先放 4 ,再 2 最后 1 . 

           但是放 2 的时候要加上 4 剩余的 , 因为 4 可以拆成 2+ 2 所以即使 2 用过仍然可以放1个,所以 算 1 的时候 要加上 2 剩的 和 4 剩的。

Code:

#include <iostream> #include <cstdio>#include <string.h>#include <algorithm>using namespace std;const int AX = 1e2+66;int a[AX];int main(){int n,k;scanf("%d%d",&n,&k);for( int i = 0 ; i < k ; i++ ){scanf("%d",&a[i]);}int add2 = 2*n , add4 = n ;for( int i = 0 ; i < k ; i++ ){int temp = min( add4 , a[i] / 4 );add4 -= temp ; a[i] -= temp * 4 ;}add2 += add4 ; for( int i = 0 ; i < k ; i ++ ){int temp = min( add2 , a[i] / 2 );add2 -= temp ;a[i] -= temp * 2;}int add1 = add2 + add4 ;for( int i = 0 ; i < k ; i++ ){add1 -= a[i] ;} cout<< ( add1 >= 0 ? "YES" : "NO" ) << endl ;return 0;}


C. Journey
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

Let the length of each road be 1. The journey starts in the city1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the linkhttps://en.wikipedia.org/wiki/Expected_value.

Input

The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.

Then n - 1 lines follow. The i-th line of these lines contains two integers ui andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — the cities connected by thei-th road.

It is guaranteed that one can reach any city from any other by the roads.

Output

Print a number — the expected length of their journey. The journey starts in the city1.

Your answer will be considered correct if its absolute or relative error does not exceed10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury isb. The checker program will consider your answer correct, if.

Examples
Input
41 21 32 4
Output
1.500000000000000
Input
51 21 33 42 5
Output
2.000000000000000

Note

In the first sample, their journey may end in cities 3 or4 with equal probability. The distance to city 3 is 1 and to city 4 is2, so the expected length is 1.5.

In the second sample, their journey may end in city 4 or5. The distance to the both cities is 2, so the expected length is 2.


思路:搜索到没有子节点的节点,每个节点的期望计算公式就是:1.0 + sum / k .  (k 为 节点数 , sum 为子节点概率和 )


Code:

#include <iostream>#include <algorithm>#include <cstdio>#include <string.h>#include <string>#include <vector>using namespace std;const int AX = 1e5+666;std::vector<int>vec[AX];int n;double dfs( int v , int pre ){double sum = 0.0 , k = 0.0;int flag = 0;for( int i = 0 ; i < vec[v].size() ; i ++ ){int u = vec[v][i];if( u == pre ) continue;k += 1.0 ;flag = 1;sum += dfs(u,v);}if( flag ) return 1.0+sum/k;else return 0;}int main(){scanf("%d",&n);int x,y;for( int i = 1 ; i < n ; i ++ ){scanf("%d%d",&x,&y);vec[x].push_back(y);vec[y].push_back(x);}printf("%.12lf\n",dfs(1,0));return 0 ;}