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

    A. Grasshopper And the String

    One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump.

    Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability.

    The picture corresponds to the first example.

    The following letters are vowels: 'A', 'E', 'I', 'O', 'U' and 'Y'.

    Input

    The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.

    Output

    Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.

    给一个字符串,蚂蚱只能落在起/终点和AEIOUY字母上,求最小的最远跳跃距离。O(LEN)扫描即可。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 bool judge(char c) {
     6     return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y';
     7 }
     8 
     9 signed main(void) {
    10     int dis = 0, tmp = 0;
    11     string str; cin >> str;
    12     int len = str.length();
    13     for (int i = 0; i < len; ++i)
    14         if (judge(str[i]))tmp = 0;
    15         else dis = max(dis, ++tmp);
    16     cout << dis + 1 << endl;
    17 }
    A.cpp

    B. Parade

    Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.

    There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and risoldiers, who start to march from right leg.

    The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.

    No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.

    Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.

    Input

    The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns.

    The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.

    Output

    Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.

    Consider that columns are numbered from 1 to n in the order they are given in the input data.

    If there are several answers, print any of them.

    给出一些二元组(l,r),可以对至多一个二元组进行交换l、r的操作。求使|sumL - sumR|最大的交换方案。O(N)暴力枚举即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define N 100005
     5 
     6 int n;
     7 int l[N];
     8 int r[N]; 
     9 
    10 signed main(void)
    11 {
    12     cin >> n;
    13     for (int i = 1; i <= n; ++i)
    14         cin >> l[i] >> r[i];
    15     int L = 0, R = 0;
    16     for (int i = 1; i <= n; ++i)
    17         L += l[i], R += r[i];
    18     int ans = abs(L - R), id = 0;
    19     #define dl (l[i] - r[i])
    20     for (int i = 1; i <= n; ++i)
    21         if (ans < abs(L - R - 2*dl))
    22             ans = abs(L - R - 2*dl), id = i;
    23     cout << id << endl;
    24 }
    B.cpp

    C. Epidemic in Monstropolis

    There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.

    Soon, monsters became hungry and began to eat each other.

    One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.

    For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are:

    1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2;
    2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2;
    3. the second monster can't eat the fifth monster because they are not neighbors;
    4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2].

    After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.

    You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.

    Input

    The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters.

    The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.

    The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke.

    Monsters are listed in the order from the beginning of the queue to the end.

    Output

    In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.

    Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.

    When one monster eats another the queue decreases. If there are several answers, print any of them.

    有意思题面,经典的区间DP。

    有一队怪兽,怪兽会吃怪兽。怪兽A可以吃了怪兽B,iff 怪兽A的体重严格大于怪兽B,且A和B在队列中相邻。A吃了B之后,体重变为A、B之和

    现按顺序给出这队怪兽的体重,以及最终的序列,求一个合法的吃怪兽日志,或输出无解。

    dp[i][j]表示是否可以使原序列的[i,j]区间合并为一个怪兽,只需要枚举是i被右边吃掉还是j被左边吃掉即可,O(N^2)。

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 
      4 #define N 505
      5 
      6 int n, a[N];
      7 int m, b[N];
      8 int sumA[N];
      9 int sumB[N];
     10 int dp[N][N];
     11 
     12 struct Answer
     13 {
     14     int p; 
     15     char c;
     16     
     17     Answer(void) {};
     18     Answer(int x, char y)
     19     {
     20         p = x; c = y;
     21     }
     22 }stk[N]; int tot = 0;
     23 
     24 int tree[N];
     25 
     26 int lowbit(int x)
     27 {
     28     return x & -x;
     29 }
     30 
     31 int query(int p)
     32 {
     33     int r = 0;
     34     
     35     while (p)
     36     {
     37         r += tree[p];
     38         p -= lowbit(p);
     39     }
     40     
     41     return r;
     42 }
     43 
     44 void add(int p)
     45 {
     46     while (p <= n)
     47     {
     48         tree[p] += 1;
     49         p += lowbit(p);
     50     }
     51 }
     52 
     53 void dfs(int l, int r)
     54 {
     55     if (l == r)
     56         return;
     57     if (dp[l + 1][r] && sumA[r] - sumA[l] > a[l])
     58     {
     59         dfs(l + 1, r);
     60         stk[++tot] = Answer(l, 'L');
     61     }
     62     else
     63     {
     64         dfs(l, r - 1);
     65         stk[++tot] = Answer(r, 'R');
     66     }
     67 }
     68 
     69 signed main(void)
     70 {
     71     scanf("%d", &n);
     72     
     73     for (int i = 1; i <= n; ++i)
     74         scanf("%d", a + i);
     75         
     76     scanf("%d", &m);
     77     
     78     for (int i = 1; i <= m; ++i)
     79         scanf("%d", b + i);
     80 
     81     memset(sumA, 0, sizeof(sumA));
     82     memset(sumB, 0, sizeof(sumB));
     83         
     84     for (int i = 1; i <= n; ++i)
     85         sumA[i] = sumA[i - 1] + a[i];
     86         
     87     for (int i = 1; i <= m; ++i)
     88         sumB[i] = sumB[i - 1] + b[i];
     89     
     90     memset(dp, 0, sizeof(dp));
     91         
     92     for (int i = 1; i <= n; ++i)
     93         dp[i][i] = 1;
     94         
     95     for (int i = 1; i < n; ++i)
     96         for (int j = 1; i + j <= n; ++j)
     97         {
     98             if (sumA[i + j] - sumA[j] > a[j])
     99                 dp[j][i + j] |= dp[j + 1][i + j];
    100             if (sumA[i + j - 1] - sumA[j - 1] > a[i + j])
    101                 dp[j][i + j] |= dp[j][i + j - 1];
    102         }
    103         
    104     bool flag = true;
    105     
    106     for (int i = 1, j = 1, k; i <= m; ++i)
    107     {
    108         for (k = j; k <= n && sumA[k] != sumB[i]; ++k);
    109         
    110         if (k > n) 
    111             { flag = false; break; }
    112         if (i == m && k != n)
    113             { flag = false; break; }
    114         if (!dp[j][k])
    115             { flag = false; break; }
    116             
    117         dfs(j, k); j = k + 1;
    118     }
    119     
    120     if (!flag)
    121         return puts("NO"), 0;
    122         
    123     puts("YES");
    124     
    125     memset(tree, 0, sizeof(tree));
    126     
    127     for (int i = 1; i <= tot; ++i)
    128     {
    129         int q = query(stk[i].p);
    130         add(stk[i].p);
    131         stk[i].p -= q;
    132         
    133         if (stk[i].c == 'L')
    134             ++stk[i].p;
    135         else
    136             --stk[i].p;
    137     }
    138     
    139     for (int i = 1; i <= tot; ++i)
    140         printf("%d %c
    ", stk[i].p, stk[i].c);
    141 }
    C.cpp

    D. Kostya the Sculptor

    Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere.

    Zahar has n stones which are rectangular parallelepipeds. The edges sizes of the i-th of them are aibi and ci. He can take no more than two stones and present them to Kostya.

    If Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way.

    Help Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar.

    Input

    The first line contains the integer n (1 ≤ n ≤ 105).

    n lines follow, in the i-th of which there are three integers ai, bi and ci (1 ≤ ai, bi, ci ≤ 109) — the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.

    Output

    In the first line print k (1 ≤ k ≤ 2) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n — the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data.

    You can print the stones in arbitrary order. If there are several answers print any of them.

    给出一些长方体的三边长,你可以把至多两个长方体粘接起来,要求对在一起的两面完全相同。求最大的长方体内切球直径。

    对于一个长方体,内切球直径即为min(a,b,c),其中a,b,c为三边长度。所以用Map保存每种长方形面对应的最长边,每次暴力枚举6种粘贴方式即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define min(x,y,z) min(x,min(y,z))
     4 signed main(void)
     5 {
     6     int n, r = 0, a, b; cin >> n;
     7     typedef pair<int, int> p; map<p, p> l;
     8     for (int i = 1; i <= n; ++i)
     9     {
    10         int x, y, z; cin >> x >> y >> z;
    11         if (r < min(x, y, l[make_pair(x, y)].first + z))
    12             r = min(x, y, l[make_pair(x, y)].first + z), a = i, b = l[make_pair(x, y)].second;
    13         if (r < min(y, z, l[make_pair(y, z)].first + x))
    14             r = min(y, z, l[make_pair(y, z)].first + x), a = i, b = l[make_pair(y, z)].second;
    15         if (r < min(x, z, l[make_pair(x, z)].first + y))
    16             r = min(x, z, l[make_pair(x, z)].first + y), a = i, b = l[make_pair(x, z)].second;
    17         if (r < min(x, y, l[make_pair(y, x)].first + z))
    18             r = min(x, y, l[make_pair(y, x)].first + z), a = i, b = l[make_pair(y, x)].second;
    19         if (r < min(y, z, l[make_pair(z, y)].first + x))
    20             r = min(y, z, l[make_pair(z, y)].first + x), a = i, b = l[make_pair(z, y)].second;
    21         if (r < min(x, z, l[make_pair(z, x)].first + y))
    22             r = min(x, z, l[make_pair(z, x)].first + y), a = i, b = l[make_pair(z, x)].second;
    23         l[make_pair(x, y)] = max(l[make_pair(x, y)], make_pair(z, i));
    24         l[make_pair(y, z)] = max(l[make_pair(y, z)], make_pair(x, i));
    25         l[make_pair(x, z)] = max(l[make_pair(x, z)], make_pair(y, i));
    26         l[make_pair(y, x)] = max(l[make_pair(y, x)], make_pair(z, i));
    27         l[make_pair(z, y)] = max(l[make_pair(z, y)], make_pair(x, i));
    28         l[make_pair(z, x)] = max(l[make_pair(z, x)], make_pair(y, i));
    29     }
    30     if (!b)cout << 1 << endl << a << endl;
    31     else cout << 2 << endl << a << " " << b << endl;
    32 }
    D.cpp

    E. Sleep in Class

    The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs.

    The stairs consists of n steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number n. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction "up" changes to "down", the direction "down"  —  to the direction "up".

    Olga always moves to the next step in the direction which is shown on the pointer above the step.

    If Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the n-th) step.

    In one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second.

    For each step find the duration of the dream if Olga was at this step at the beginning of the dream.

    Olga's fall also takes one second, so if she was on the first step and went down, she would wake up in the next second.

    Input

    The first line contains single integer n (1 ≤ n ≤ 106) — the number of steps on the stairs.

    The second line contains a string s with the length n — it denotes the initial direction of pointers on the stairs. The i-th character of string s denotes the direction of the pointer above i-th step, and is either 'U' (it means that this pointer is directed up), or 'D' (it means this pointed is directed down).

    The pointers are given in order from bottom to top.

    Output

    Print n numbers, the i-th of which is equal either to the duration of Olga's dream or to  - 1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the i-th step.

    有个梦游的人,走在奇怪的楼梯上。每级台阶上有一个指示,向上或向下,当这个人踏上指示之后,会按照指示改变(或不变)方向。离开之后,台阶上的指示会取反。问,这个人从每一级台阶出发,各需要多少步就能醒来(从1向下走,或从n向上走)。

    经过模拟发现,这个人走在楼梯上总是在来回走,每次向下走到第一个U,又向上走到第一个D,然后回到出发点。每转圈一次,就会使两边的最近的一对U,D变为和他移动相同的方向。而且,对于一个出发点,只需关注下边的U和上边的D。所以从1到n依次枚举起点,u维护区间内i下边的U的个数,d维护区间内i上边的D的个数(含i自己),x维护向下走的来回路径和,y维护向上走的来回路径和,此时的x+y就是把所有两边配对的U,D全部改成顺向的步数,截束在出发位置,再判断最终会向下还是向上走即可。

    OTZ fsss_7

     1 #include <bits/stdc++.h>
     2 typedef long long LL;
     3 char str[1000005];
     4 LL x, y, u, d;
     5 int n, l, r;
     6 signed main(void)
     7 {
     8     std::cin >> n >> (str + 1);
     9     for (int i = 1; i <= n; ++i)
    10     {
    11         x += u << 1;
    12         y -= d << 1;
    13         if (str[i - 1] == 'U')
    14             ++u, x += 2;
    15         if (str[i - 1] == 'D')
    16             --d, y += 2;
    17         while (u >= d && r < n)
    18             if (str[++r] == 'D')
    19                 ++d, y += (r - i) << 1;
    20         while (u > d && r == n)
    21             if (str[++l] == 'U')
    22                 --u, x -= (i - l) << 1;
    23         std::cout << (u == d
    24             ? x + y + n - i + 1
    25             : x + y + i) << " ";
    26     }
    27 }
    E.cpp

    F. Drivers Dissatisfaction

    In one kingdom there are n cities and m two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfaction — the value wi.

    For each road we know the value ci — how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce the dissatisfaction with the i-th road by k, we should spend k·ci lamziks. And it is allowed for the dissatisfaction to become zero or even negative.

    In accordance with the king's order, we need to choose n - 1 roads and make them the main roads. An important condition must hold: it should be possible to travel from any city to any other by the main roads.

    The road ministry has a budget of S lamziks for the reform. The ministry is going to spend this budget for repair of some roads (to reduce the dissatisfaction with them), and then to choose the n - 1 main roads.

    Help to spend the budget in such a way and then to choose the main roads so that the total dissatisfaction with the main roads will be as small as possible. The dissatisfaction with some roads can become negative. It is not necessary to spend whole budget S.

    It is guaranteed that it is possible to travel from any city to any other using existing roads. Each road in the kingdom is a two-way road.

    Input

    The first line contains two integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of cities and the number of roads in the kingdom, respectively.

    The second line contains m integers w1, w2, ..., wm (1 ≤ wi ≤ 109), where wi is the drivers dissatisfaction with the i-th road.

    The third line contains m integers c1, c2, ..., cm (1 ≤ ci ≤ 109), where ci is the cost (in lamziks) of reducing the dissatisfaction with the i-th road by one.

    The next m lines contain the description of the roads. The i-th of this lines contain a pair of integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi) which mean that the i-th road connects cities ai and bi. All roads are two-way oriented so it is possible to move by the i-th road from aito bi, and vice versa. It is allowed that a pair of cities is connected by more than one road.

    The last line contains one integer S (0 ≤ S ≤ 109) — the number of lamziks which we can spend for reforms.

    Output

    In the first line print K — the minimum possible total dissatisfaction with main roads.

    In each of the next n - 1 lines print two integers x, vx, which mean that the road x is among main roads and the road x, after the reform, has the level of dissatisfaction vx.

    Consider that roads are numbered from 1 to m in the order as they are given in the input data. The edges can be printed in arbitrary order. If there are several answers, print any of them.

    @Author: YouSiki

  • 相关阅读:
    开发报表的先进工具
    强大的报表前端展现功能
    管理驾驶舱监控大屏展现
    换乘算法【转】
    提交中文数据乱码问题总结
    重定向
    容易遗漏的
    jsp基础语法【转】
    说说Java NIO【转】
    Java读取大文件的操作【转】
  • 原文地址:https://www.cnblogs.com/yousiki/p/6065146.html
Copyright © 2011-2022 走看看