zoukankan      html  css  js  c++  java
  • Codeforces Round #428 (Div. 2)

    A. Arya and Bran

    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 aicandies in a box, that is given by the Many-Faced God. Every day she can give Bran at most8 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 beforethe 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 ≤ 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 within n days, print -1.

    Otherwise print a single integer — the minimum number of days Arya needs to give Bran kcandies 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 8candies per day and she must give him the candies within 1 day.

     
    题意:

    模拟题,注意一天没有用完的可以第二天继续用。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int a[105];
    
    bool cmp(int a,int b) {
        return a > b;
    }
    
    int main()
    {
        int n,k;
        scanf("%d%d",&n,&k);
    
        int ans = 0;
        int sum = 0;
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
    
        for(int i=0; i<n; i++)
        {
            if(a[i]>8)
            {
                sum+=8;
                a[i+1] +=(a[i]-8);
                if(sum>=k) {
                    ans = i+1;
                    break;
                }
            }
            else
            {
                sum+=a[i];
                if(sum>=k) {
                    ans = i+1;
                    break;
                }
            }
        }
    
        if(ans==0)
            ans = -1;
        printf("%d
    ",ans);
    
        return 0;
    }
    View Code
    B. Game of the Rows

    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 ≤ 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), 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).

     题意:

    注意:飞机中间的算一个整体。

    首先考虑>=4的部分,先往中间放,然后,==3 和 ==2的部分,2放到两边,而3先拿两个放两边。最后考虑1的部分。插在中间,或者放到两边。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int a[10005];
    
    int main()
    {
        int n,k;
        cin>>n>>k;
    
        for(int i=0;i<k;i++)
            scanf("%d",&a[i]);
    
        int sum1 = n,sum2 = 2*n;
    
        for(int i=0;i<k;i++) {
            int t = min(a[i]/4,sum1);
            sum1 -=t;
            a[i]-=4*t;
        }
    
        sum2+=sum1;
    
        for(int i=0;i<k;i++) {
            int t = min(a[i]/2,sum2);
            sum2-=t;
            a[i]-=2*t;
        }
    
        sum2+=sum1;
        for(int i=0;i<k;i++) {
            sum2-=a[i];
        }
    
        if(sum2>=0)
            puts("YES");
        else puts("NO");
    
        return 0;
    }
    View Code
    C. Journey

    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.

     题意:求期望。

    分析:DFS瞎搞,首先DFS出每个节点的子节点个数,和dist,然后根据节点的父亲节点的子节点的个数DFS出所有的概率。

    最后,DFS出每个dist[u]*p[u]。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int maxn = 100000+5;
    
    int degree[maxn];
    int dist[maxn];
    
    vector<int> G[maxn];
    double ans;
    double p[maxn];
    int num[maxn];
    
    void sz(int u,int f)
    {
        num[u] = 0;
        for(int i=0; i<G[u].size(); i++)
        {
            int v = G[u][i];
            if(v==f) continue;
            num[u]++;
            dist[v] = dist[u] + 1;
            sz(v,u);
        }
    }
    
    void dp(int u,int f)
    {
        p[u] = p[f]/num[f];
        for(int i=0; i<G[u].size(); i++)
        {
            int v = G[u][i];
            if(v==f) continue;
            dp(v,u);
        }
    }
    
    void ret(int u,int f)
    {
        int x = 0;
        for(int i=0; i<G[u].size(); i++)
        {
            int v = G[u][i];
            if(v==f) continue;
            x++;
            ret(v,u);
        }
    
        if(x==0)
        {
            ans+= dist[u]*p[u];
        }
    }
    
    int main()
    {
    
        int n;
        cin>>n;
    
        for(int i=0; i<n-1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            degree[u]++;
            degree[v]++;
        }
    
        num[0] = 1;
        dist[1] = 0;
        sz(1,0);
    
        p[0] = 1;
        dp(1,0);
        ret(1,0);
    
        printf("%.10lf
    ",ans);
    
        return 0;
    }
    View Code
  • 相关阅读:
    子程序的设计
    多重循环程序设计
    汇编语言的分支程序设计与循环程序设计
    代码调试之串口调试2
    毕昇杯模块之光照强度传感器
    毕昇杯之温湿度采集模块
    【CSS】盒子模型 之 IE 与W3C的盒子模型对比
    【css】盒子模型 之 概述
    【css】盒子模型 之 弹性盒模型
    【网络】dns_probe_finished_nxdomain 错误
  • 原文地址:https://www.cnblogs.com/TreeDream/p/7356865.html
Copyright © 2011-2022 走看看