• Areas on the Cross-Section Diagram Aizu


    Your task is to simulate a flood damage.

    For a given cross-section diagram, reports areas of flooded sections.
    在这里插入图片描述

    Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides. For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.

    Input

    A string, which represents slopes and flat lands by ‘/’, ‘’ and '’ respectively, is given in a line. For example, the region of the above example is given by a string "///_//\/////__\_///".

    output

    Report the areas of floods in the following format:

    A
    k L1 L2 … Lk

    In the first line, print the total area A of created floods.

    In the second line, print the number of floods k and areas Li(i=1,2,…,k) for each flood from the left side of the cross-section diagram. Print a space character before Li.

    Constraints

    1≤ length of the string ≤20,000

    Sample Input 1

    //

    Sample Output 1

    4
    1 4

    Sample Input 2

    ///_//\/////__\_//_/

    Sample Output 2

    35
    5 4 2 1 19 9

    code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00             ^   11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                ^ 00.                                     ^^0.^
                                 1 ^ 0                                     ^^110.^
                               0.   0 ^                                    ^^^10.01
                       ^^     010^   1 1                                   ^^^1110.1
                       0001  10 0   ^ 1.1                                   ^^^1111110
                       0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                       10  10^ 0^                                             ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Areas on the Cross-Section Diagram Aizu - ALDS1_3_D .cpp created by VB_KoKing on 2019-04-29:20.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
     1.计算总面积:对输入的字符si进行逐个检查:
        如果是“”,则将表示该字符位置(从开头数第几个字符)的整数i压入栈S1;
        如果是“/”,则从栈S1顶部取出与之对应的“”的位置i,算出二者的距离i-j并累加到总面积里;
        字符“_”的作用只是将一对“”与“/”的距离加+,因此从栈中取出“”时可以直接与对应的“/”计算距离,不必考虑“_”。
     2.求各积水处面积
        另建一个栈S2来保存各积水处的面积
        栈S2中的每个元素包含一对数据,分别是“该积水处最左侧‘’的位置”和“该积水处当前的面积”。
     Functions required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first hurricane that passed through your ear is you,
    The first star you see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <iostream>
    #include <stack>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        char ch;
        int sum=0;
        stack<int> S1;
        stack< pair<int,int> > S2;
        for (int i = 0; cin>>ch ; i++) {
            if (ch=='\') S1.push(i);
            else if (ch=='/'&&S1.size()>0)
            {
                int j=S1.top();
                S1.pop();
                sum+=i-j;
                int a=i-j;
                while (S2.size()>0&&S2.top().first>j)
                {
                    a+=S2.top().second;
                    S2.pop();
                }
                S2.push(make_pair(j,a));
            }
        }
    
        vector<int> ans;
        while (S2.size()>0)
        {
            ans.push_back(S2.top().second);
            S2.pop();
        }
        reverse(ans.begin(),ans.end());
        cout<<sum<<endl<<ans.size();
        for (int i = 0; i < ans.size(); i++)
            cout<<' '<<ans[i];
        cout<<endl;
        return 0;
    }
    
  • 相关阅读:
    Java并发
    JS的强制类型转换
    JS的原生函数
    JS的类型和值
    解决Oracle临时表空间占满的问题
    nginx location匹配规则
    java.util.ConcurrentModificationException 解决办法
    SQL优化三板斧:精简之道、驱动为王、集合为本
    一次非典型SQL优化:如何通过业务逻辑优化另辟蹊径?
    一次耐人寻味的SQL优化:除了SQL改写,还要考虑什么?
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338384.html
Copyright © 2020-2023  润新知