Codeforces Round 428 div2 A-C

来源:互联网 发布:seo主管岗位职责 编辑:程序博客网 时间:2024/06/08 00:50

 A - Arya and Bran(by Arpa)

Let t be number of her candies. At i-th day , we increase t by ai ,then we give Bran min(8, t) . So we decrease k from this value. We will print the answer once k becomes smaller or equal to 0 . Or we will print  - 1 if it does’n happen after n days.

#include <bits/stdc++.h>
// by VonSdite
using namespace std;const int SIZE = 105;int candy[SIZE];int main(int argc, char const *argv[]){ int n, k; while (~scanf("%d %d", &n, &k)) { memset(candy, 0, sizeof(candy)); int sum = 0; for (int i = 0; i < n; ++i) { scanf("%d", &candy[i]); sum += candy[i]; } if(sum < k) printf("-1\n"); else { int ans = 0; for (int i = 0; i < n; ++i) { if(candy[i] > 8) { k -= 8; candy[i+1] += candy[i] - 8; } else { k -= candy[i]; } ++ans; if(k <= 0) break; } if(k > 0) printf("-1\n"); else printf("%d\n", ans); } } return 0;}


B - Game of the Rows (by Arpa)

Use greedy solution. Consider a group with x ≥ 4 members, put 4 of them in seats [3, 6] of some row, and throw the row. Now we have x - 4 members in this group now. Continue till all of the seats in the range [3, 6] become full, continue with [1, 2] and [7, 8]. Now handle groups with size  ≤ 3.

For groups with size  = 3, allocate 4 seats in range [3, 6] or 4 seats in range [1, 2] or [7, 8].

For groups with size  = 2, allocate 2 seats in range [1, 2] or [7, 8] or 3 seats in range [3, 6]. If no seat found, divide this group and make it two groups with size 1.

Fill the other parts with groups with groups with size  = 1.

If in any part we ran out of seat, the answer is NOYES otherwise.

#include <bits/stdc++.h>
// by VonSdite
using namespace std;const int SIZE = 105;int candy[SIZE];int main(int argc, char const *argv[]){ int n, k; while (~scanf("%d %d", &n, &k)) { memset(candy, 0, sizeof(candy)); int sum = 0; for (int i = 0; i < n; ++i) { scanf("%d", &candy[i]); sum += candy[i]; } if(sum < k) printf("-1\n"); else { int ans = 0; for (int i = 0; i < n; ++i) { if(candy[i] > 8) { k -= 8; candy[i+1] += candy[i] - 8; } else { k -= candy[i]; } ++ans; if(k <= 0) break; } if(k > 0) printf("-1\n"); else printf("%d\n", ans); } } return 0;}



C - Journey (by Arpa)

Let the cities be vertices and roads be edges of a tree and vertex 1 be the root of the tree.

Let ans[i] be the answer for the i-th vertex (the expected value if they start their journey from that vertex and the horse doesn't go to it's parent). Now we can calculate ans[i] by knowing the answer for it’s children. Let v1, v2, …., vk be the children of i-th vertex , then  . Because when we are at i-th vertex , we have k choices with equal probabilities and  + 1for going to one of them (length of the edge between i-th vertex and it’s children).

So if we know the answer of some vertex’s children, we can calculate its expected value and we can do it by a simple DFS (note that the answer for a leave is 0).


#include <bits/stdc++.h> // by VonSditeusing namespace std;const int SIZE = 105;int candy[SIZE];int main(int argc, char const *argv[]){    int n, k;    while (~scanf("%d %d", &n, &k))    {        memset(candy, 0, sizeof(candy));        int sum = 0;        for (int i = 0; i < n; ++i)        {            scanf("%d", &candy[i]);            sum += candy[i];        }               if(sum < k) printf("-1\n");        else        {            int ans = 0;            for (int i = 0; i < n; ++i)            {                if(candy[i] > 8)                 {                    k -= 8;                    candy[i+1] += candy[i] - 8;                }                else                {                    k -= candy[i];                }                ++ans;                if(k <= 0) break;            }            if(k > 0) printf("-1\n");            else printf("%d\n", ans);        }    }    return 0;}


原创粉丝点击