Codeforces Round #428 (Div. 2) -ABC

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 are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at 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 the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).

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

Input

The first line contains two integers n and k (1 ≤ n ≤ 1001 ≤ 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 within n days, print -1.

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

Examples
input
2 3
1 2
output
2
input
3 17
10 10 10
output
3
input
1 9
10
output
-1
Note

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

In the second sample, Arya can give Bran 17 candies in 3 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 most 8 candies per day and she must give him the candies within 1 day.



题意:
二丫每天最多可以给布兰8块糖,千面之神每天可以给二丫a[i]块糖,给不完的可以存下来以后给。
问你最少几天给完布兰k块糖。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
    int n,k;
    while(~scanf("%d %d",&n,&k))
    {
        int now=0;
        int ans=0;
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            int a;scanf("%d",&a);
            if(flag)continue;
            now+=a;
            if(now<=8)
            {
                k-=now;
                now=0;
                if(k<=0) ans=i,flag=1;
            }
            else
            {
                now-=8;
                k-=8;
                if(k<=0) ans=i,flag=1;
            }
            
        }
        if(!ans) printf("-1\n");
        else printf("%d\n",ans);
    }
    
}

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, the i-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 nrows, each of them has 8 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 and k (1 ≤ n ≤ 100001 ≤ 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), where ai denotes the number of soldiers in the i-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 2
5 8
output
YES
input
1 2
7 1
output
NO
input
1 2
4 4
output
YES
input
1 4
2 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).


题意:
龙母有很多军队,他们坐飞机,不同的队伍不能做一起,让你判断能不能坐下。

POINT:
贪心思想,如果有4|3个人就先做4个人的,再去做2个人的,(2个人的情况---如果2人座没人就去坐4人zuo--这个情况可以坐落单的一个人)落单的记录下来。最后判断落单的能不能坐上就行。  4人座可以坐2个落单的。2人座只能坐1个,别忘了剪去那个特殊情况。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
    int n,k;
    while(~scanf("%d %d",&n,&k))
    {
        int two=n*2;
        int four=n;
        int one=0;
        int dan=0;
        int you=0;
        for(int i=1;i<=k;i++)
        {
            int a;scanf("%d",&a);
            if(four>0&&a>=4)
            {
                int nowf=a/4;
                if(nowf>four)
                {
                    
                    a=a-four*4;
                    four=0;
                }
                else
                {
                    a=a-nowf*4;
                    four-=nowf;
                }
            }
            if(four>0&&a>=3)
            {
                int nowf=a/3;
                if(nowf>four)
                {
                    a=a-four*3;
                    
                    four=0;
                }
                else
                {
                    a=a-nowf*3;
                    four-=nowf;
                }
            }
            if(two>0&&a>=2)
            {
                int nowt=a/2;
                if(nowt>two)
                {
                    a=a-two*2;
                    two=0;
                }
                else
                {
                    a=a-nowt*2;
                    two-=nowt;
                }
            }
            if(four>0&&a>=2)
            {
                four--;
                a-=2;
                you++;
            }
            if(a>=1)
            {
                dan+=a;
            }
        }
        if(dan-you>two+four*2)
        {
            printf("NO\n");
        }
        else printf("YES\n");
    }
    
}

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 city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://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 and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities connected by the i-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 city 1.

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

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

Examples
input
4
1 2
1 3
2 4
output
1.500000000000000
input
5
1 2
1 3
3 4
2 5
output
2.000000000000000
Note

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

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


题意:
铁群岛的铁T和他的妹妹席恩过马路,求出走的长度的期望。
POINT:
邻接表搜索搞一下。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
const int N = 100000+4;
struct node
{
    int v,next;
}len[N<<1];
int head[N];
void add(int u,int v,int cnt)
{
    len[cnt].v=v;
    len[cnt].next=head[u];
    head[u]=cnt;
}
double ans=0;
void dfs(int x,int pre,double p,int l)
{
    int flag=0;
    int num=0;
    for(int i=head[x];i!=-1;i=len[i].next)
    {
        if(len[i].v==pre) continue;
        num++;
    }
    for(int i=head[x];i!=-1;i=len[i].next)
    {
        if(len[i].v==pre) continue;
        dfs(len[i].v,x,p*1.0/num,l+1);
        flag=1;
    }
    if(!flag)
        ans+=p*(double)l;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        ans=0;
        memset(head,-1,sizeof head);
        for(int i=1;i<=n-1;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            add(u,v,++cnt);
            add(v,u,++cnt);
        }
        dfs(1,-1,1.0,0);
        printf("%lf\n",ans);
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值