zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 32 ABC

    A. Local Extrema

    You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.

    An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

    Input

    The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

    Output

    Print the number of local extrema in the given array.

    Examples
    Input
    3
    1 2 3
    Output
    0
    Input
    4
    1 5 2 5
    Output
    2

     求极点的数量

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a[1010];
     4 int main() {
     5     int n;
     6     cin >> n;
     7     for(int i = 1; i <= n; i ++) cin >> a[i];
     8     int ans = 0;
     9     for(int i = 2; i < n; i ++) {
    10         if(a[i] > a[i-1] && a[i] > a[i+1]) ans++;
    11         if(a[i] < a[i-1] && a[i] < a[i+1]) ans++;
    12     }
    13     printf("%d
    ",ans);
    14     return 0;
    15 }
    B. Buggy Robot

    Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:

    • U — move from the cell (x, y) to (x, y + 1);
    • D — move from (x, y) to (x, y - 1);
    • L — move from (x, y) to (x - 1, y);
    • R — move from (x, y) to (x + 1, y).

    Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!

    Input

    The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).

    The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.

    Output

    Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.

    Examples
    Input
    4
    LDUR
    Output
    4
    Input
    5
    RRRUU
    Output
    0
    Input
    6
    LLRRRR
    Output
    4

    求长度最长的命令 会使的回到原点。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 char str[110];
     4 int main() {
     5     int n, a, b, c, d;
     6     a = b = c = d = 0;
     7     cin >> n >> str;
     8     for(int i = 0; i < n; i ++) {
     9         if(str[i] == 'U') a++;
    10         else if(str[i] == 'D') b++;
    11         else if(str[i] == 'L') c++;
    12         else if(str[i] == 'R') d++;
    13     }
    14     printf("%d
    ",min(a,b)*2+min(c,d)*2);
    15     return 0;
    16 }
    C. K-Dominant Character

    You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.

    You have to find minimum k such that there exists at least one k-dominant character.

    Input

    The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

    Output

    Print one number — the minimum value of k such that there exists at least one k-dominant character.

    Examples
    Input
    abacaba
    Output
    2
    Input
    zzzzz
    Output
    1
    Input
    abcde
    Output
    3

     在s字符串中,求每个个字符的最大间距的最小值。

    00Z000中'z'的最大间距算3

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N = 100010;
     4 char str[N];
     5 int sum[N];
     6 bool vis[27];
     7 int main() {
     8     cin >> str;
     9     int len = strlen(str);
    10     int k = len;
    11     for(int i = 0; str[i]; i ++) {
    12         sum[i] = str[i] - 'a';
    13         vis[sum[i]] = true;
    14     }
    15     for(int i = 0; i < 26; i ++) {
    16         if(vis[i]) {
    17             int x = -1, tmp = 0, MAX = -1;
    18             for(int j = 0; j < len; j ++) {
    19                 if(sum[j] != i) tmp ++;
    20                 else {
    21                     if(tmp + 1 > MAX) MAX = tmp + 1;
    22                     tmp = 0;
    23                 }
    24             }
    25             if(tmp + 1 > MAX) MAX = tmp + 1;
    26             if(MAX < k) k = MAX;
    27         }
    28     }
    29     printf("%d
    ",k);
    30     return 0;
    31 }
  • 相关阅读:
    解决取消input标签中disabled属性
    【转】jQuery源码分析-13 CSS操作-CSS-类样式-addClass+removeClass+toggleClass+hasClass
    Bigpipe学习【转】
    用户请求的生命周期[传统模式与BigPipe]
    gcc扩展语法一:在表达式中的语句和声明
    分享一个关于pthread线程栈在mm_struct里面的分布问题
    neo4j 安装步骤 转自:http://blog.csdn.net/luoluowushengmimi/article/details/19987995
    sudoers文件解析 转自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1971013
    匿名union
    VMware虚拟机修改BIOS启动项
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7818914.html
Copyright © 2011-2022 走看看