zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 42 (Rated for Div. 2)

                                            A. Equator

    Polycarp has created his own training plan to prepare for the programming contests. He will train for nn days, all days are numbered from 11 to nn, beginning from the first.

    On the ii-th day Polycarp will necessarily solve aiai problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems.

    Determine the index of day when Polycarp will celebrate the equator.

    Input

    The first line contains a single integer nn (1n2000001≤n≤200000) — the number of days to prepare for the programming contests.

    The second line contains a sequence a1,a2,,ana1,a2,…,an (1ai100001≤ai≤10000), where aiai equals to the number of problems, which Polycarp will solve on the ii-th day.

    Output

    Print the index of the day when Polycarp will celebrate the equator.

    注意奇数的一半和偶数的一半

     1 #pragma warning(disable:4996)
     2 #include<bitset>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 const int maxn = 200005;
    12 
    13 int n;
    14 int a[maxn];
    15 
    16 int main()
    17 {
    18     while (cin >> n) {
    19         int tmp, sum = 0;
    20         for (int i = 1; i <= n; i++) {
    21             cin >> a[i];
    22             sum += a[i];
    23         }
    24         a[0] = 0;
    25         for (int i = 1; i <= n; i++) {
    26             a[i] += a[i - 1];
    27             if (sum % 2) {
    28                 if (a[i] >= sum / 2 + 1) { cout << i << endl; break; }
    29             }
    30             else {
    31                 if (a[i] >= sum / 2) { cout << i << endl; break; }
    32             }
    33         }
    34     }
    35     return 0;
    36 }

                                  B. Students in Railway Carriage

    There are nn consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.

    The university team for the Olympiad consists of aa student-programmers and bb student-athletes. Determine the largest number of students from all a+ba+b students, which you can put in the railway carriage so that:

    • no student-programmer is sitting next to the student-programmer;
    • and no student-athlete is sitting next to the student-athlete.

    In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.

    Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

    Input

    The first line contain three integers nn, aa and bb (1n21051≤n≤2⋅105, 0a,b21050≤a,b≤2⋅105, a+b>0a+b>0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.

    The second line contains a string with length nn, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

    Output

    Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

    题解:当前位置能放的话,如果前面没限制,则放最多的那类,否则,只能放另一类。

    感受:我想到的是分类讨论,结果写不粗来,好菜啊!!!,在大佬的提示下,说这是很明显的贪心,随恍然大悟。结果又写挂了,忘了memset (use)!!!!!

     1 #pragma warning(disable:4996)
     2 #include<bitset>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 const int INF = 2e9 + 7;
    12 const int maxn = 200005;
    13 
    14 int n, a, b;
    15 int use[maxn];
    16 char s[maxn];
    17 
    18 int main()
    19 {
    20     while (cin >> n >> a >> b) {
    21         scanf("%s", s);
    22         memset(use, 0, sizeof(use));
    23         for (int i = 0; i < n; i++) if (s[i] == '*') use[i] = -1;
    24 
    25         int t = 0;
    26         if (s[0] == '.') {
    27             if (a > b) {
    28                 use[0] = 0;
    29                 a--;
    30                 t++;
    31             }
    32             else {
    33                 use[0] = 1;
    34                 b--;
    35                 t++;
    36             }
    37         }
    38 
    39         for (int i = 1; i < n; i++) {
    40             if (use[i] == -1) continue;
    41             if (a == 0 && b == 0) break;
    42             if (use[i - 1] == -1) {
    43                 if (a > b) {
    44                     use[i] = 0;
    45                     a--;
    46                     t++;
    47                 }
    48                 else {
    49                     use[i] = 1;
    50                     b--;
    51                     t++;
    52                 }
    53             }
    54             else {
    55                 if (use[i - 1] == 0) {
    56                     if (b) {
    57                         use[i] = 1;
    58                         b--;
    59                         t++;
    60                     }
    61                     else use[i] = -1;
    62                 }
    63                 else {
    64                     if (a) {
    65                         use[i] = 0;
    66                         a--;
    67                         t++;
    68                     }
    69                     else use[i] = -1;
    70                 }
    71             }
    72         }
    73 
    74         cout << t << endl;
    75     }
    76     return 0;
    77 }

                                            C. Make a Square

    You are given a positive integer nn, written without leading zeroes (for example, the number 04 is incorrect).

    In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.

    Determine the minimum number of operations that you need to consistently apply to the given integer nn to make from it the square of some positive integer or report that it is impossible.

    An integer xx is the square of some positive integer if and only if x=y2x=y2 for some positive integer yy.

    Input

    The first line contains a single integer nn (1n21091≤n≤2⋅109). The number is given without leading zeroes.

    Output

    If it is impossible to make the square of some positive integer from nn, print -1. In the other case, print the minimal number of operations required to do it.

    题解:暴力寻找y,然后直接匹配就行了。

     1 #pragma warning(disable:4996)
     2 #include<bitset>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 const int INF = 2e9 + 7;
    12 const int maxn = 100005;
    13 
    14 int n, m;
    15 int a[105], b[105];
    16 
    17 int main()
    18 {
    19     while (cin >> n) {
    20         int p = 0, q = 0, t = 0;
    21         int k = n;
    22         while (k) {
    23             b[q++] = k % 10;
    24             k = k / 10;
    25         }
    26     
    27         int ans = INF;
    28         for (int i = 1; i*i <= n; i++) {
    29 
    30             p = 0;
    31             int tmp = i * i;
    32             while (tmp) {
    33                 a[p++] = tmp % 10;
    34                 tmp = tmp / 10;
    35             }
    36 
    37             t = 0;
    38             for (int j = 0; j < q; j++) if (b[j] == a[t]) {
    39                 t++;
    40                 if (t == p) break;
    41             }
    42             if (t == p) ans = min(ans, q - p);
    43         }
    44         if (ans == INF) cout << "-1" << endl;
    45         else cout << ans << endl;
    46     }
    47     return 0;
    48 }

                                            D. Merge Equals

    You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value xx that occurs in the array 22 or more times. Take the first two occurrences of xx in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2x2⋅x ).

    Determine how the array will look after described operations are performed.

    For example, consider the given array looks like [3,4,1,2,2,1,1][3,4,1,2,2,1,1] . It will be changed in the following way: [3,4,1,2,2,1,1]  [3,4,2,2,2,1]  [3,4,4,2,1]  [3,8,2,1][3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1] .

    If the given array is look like [1,1,3,1,1][1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1]  [2,3,1,1]  [2,3,2]  [3,4][1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4] .

    Input

    The first line contains a single integer nn (2n1500002≤n≤150000 ) — the number of elements in the array.

    The second line contains a sequence from nn elements a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109 ) — the elements of the array.

    Output

    In the first line print an integer kk — the number of elements in the array after all the performed operations. In the second line print kk integers — the elements of the array after all the performed operations.

    Examples
    Input
    Copy
    7
    3 4 1 2 2 1 1
    Output
    Copy
    4
    3 8 2 1
    Input
    Copy
    5
    1 1 3 1 1
    Output
    Copy
    2
    3 4
    Input
    Copy
    5
    10 40 20 50 30
    Output
    Copy
    5
    10 40 20 50 30
    Note

    The first two examples were considered in the statement.

    In the third example all integers in the given array are distinct, so it will not change.

    题解:用map记录每个数的位置,如果当前数已经出现过,则消掉当前数的位置,同时数乘以2。还要记录总共消掉了多少个数。

    感受:过了这么久才补,一直想不出怎么做。。。只有看题解了,兰儿题解也看不懂。。。然后去看别人的代码。。。这写法好清新脱俗啊,易懂又好写,就它了。不过python的代码挺有意思的,可以学习一波。

     1 #pragma warning(disable:4996)
     2 #include<map>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #define ll long long 
     8 using namespace std;
     9 
    10 const int maxn = 200005;
    11 
    12 int n;
    13 ll a[maxn];
    14 
    15 int main()
    16 {
    17     while (scanf("%d", &n) != EOF) {
    18         map<ll,int> mp;
    19 
    20         int cnt = 0;
    21         for (int i = 1; i <= n; i++) {
    22             scanf("%I64d", a + i);
    23             while (mp[a[i]]) {
    24                 a[mp[a[i]]] = 0;     //将第一次出现的数置0,为了输出方便
    25                 mp[a[i]] = 0;        //重点,将这个数出现的位置消除掉(即置0),是避免以后出现相同的数,重复计算,比如4 2 2 2
    26                 a[i] = a[i] * 2;
    27                 cnt++;
    28             }
    29             mp[a[i]] = i;
    30         }
    31 
    32         printf("%d
    ", n - cnt);
    33         for (int i = 1; i <= n; i++) if (a[i]) printf("%I64d ", a[i]);
    34         printf("
    ");
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    如何在一个控件上同时实现单触和多触事件
    看看iOS 5.0 beta 6都有哪些变化吧
    转,net实现下载
    转检测到潜在危险
    如何分析已有项目如何能够得到最快最大的提升???
    转Unity 入门
    近段总结
    转,net几个热点问题
    dll动态链接库
    转使用NUnit在.Net编程中进行单元测试
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8795130.html
Copyright © 2011-2022 走看看