zoukankan      html  css  js  c++  java
  • AtCoder Grand Contest 015(B

    Problem Statement

    Skenu constructed a building that has N floors. The building has an elevator that stops at every floor.

    There are buttons to control the elevator, but Skenu thoughtlessly installed only one button on each floor - up or down. This means that, from each floor, one can only go in one direction. If Si is U, only "up" button is installed on the i-th floor and one can only go up; if Si is D, only "down" button is installed on the i-th floor and one can only go down.

    The residents have no choice but to go to their destination floors via other floors if necessary. Find the sum of the following numbers over all ordered pairs of two floors (i,j): the minimum number of times one needs to take the elevator to get to the j-th floor from the i-th floor.

    Constraints

    • 2|S|105
    • Si is either U or D.
    • S1 is U.
    • S|S| is D.

    Input

    The input is given from Standard Input in the following format:

    S1S2S|S|
    

    Output

    Print the sum of the following numbers over all ordered pairs of two floors (i,j): the minimum number of times one needs to take the elevator to get to the j-th floor from the i-th floor.


    Sample Input 1

    Copy
    UUD
    

    Sample Output 1

    Copy
    7
    

    From the 1-st floor, one can get to the 2-nd floor by taking the elevator once.

    From the 1-st floor, one can get to the 3-rd floor by taking the elevator once.

    From the 2-nd floor, one can get to the 1-st floor by taking the elevator twice.

    From the 2-nd floor, one can get to the 3-rd floor by taking the elevator once.

    From the 3-rd floor, one can get to the 1-st floor by taking the elevator once.

    From the 3-rd floor, one can get to the 2-nd floor by taking the elevator once.

    The sum of these numbers of times, 7, should be printed.

    英语水平有待提高,这题是看例子猜题意的。555555

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const int N = 1e5+10;
     5 char a[N];
     6 int b[N];
     7 int main(){
     8     cin >> a;
     9     int flag = 0, flag1 = 0;
    10     ll ans = 0, len = strlen(a);
    11     for(int i = len-1; i >= 0; i--){
    12         if(a[i] == 'D'){
    13             flag = 1;
    14         }
    15         if(flag) b[i] = 1;
    16         else b[i] = 0;
    17     }
    18     for(int i = 0; i < len; i ++){
    19         if(a[i] == 'U'){
    20             flag1 = 1;
    21             ans += (len-i-1);
    22             if(b[i]){
    23                 ans += 2*i;
    24             }
    25         }else if(a[i] == 'D'){
    26             ans += i;
    27             if(flag1){
    28                 ans += 2*(len-i-1);
    29             }
    30         }
    31     }
    32     cout << ans << endl;
    33     return 0;
    34 }
  • 相关阅读:
    Java13新特性 -- 重新实现旧版套接字API
    Java13新特性 -- switch表达式动态CDS档案(动态类数据共享归档)
    Java13新特性 -- 文本块
    Java13新特性 -- switch表达式
    Java12新特性 -- 其他新增,移除,废弃项
    Java12新特性 -- 增强G1,自动返回未用堆内存给操作系统
    Java12新特性 -- 可中断的 G1 Mixed GC
    Java12新特性 -- 默认生成类数据共享(CDS)归档文件
    Java12新特性 -- 只保留一个 AArch64 实现
    python使用requests发送text/xml报文数据
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6915145.html
Copyright © 2011-2022 走看看