Areas on the Cross-Section Diagram
2016-12-09 10:04
375 查看
Areas on the Cross-Section Diagram
Aizu - ALDS1_3_D
Areas on the Cross-Section Diagram
地域の治水対策として、洪水の被害状況をシミュレーションで仮想してみよう。図のように $1 \times 1 (m^2)$ の区画からなる格子上に表された地域の模式断面図が与えられるので、地域にできる各水たまりの面積を報告してください。
与えられた地域に対して限りなく雨が降り、地域から溢れ出た水は左右の海に流れ出ると仮定します。 例えば、図の断面図では、左から面積が 4、2、1、19、9 の水たまりができます。
入力
模式断面図における斜面を '/' と '\'、平地を '_' で表した文字列が1行に与えられます。例えば、図の模式断面図は文字列\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\ で与えられます。
出力
次の形式で水たまりの面積を出力してください。$A$
$k$ $L_1$ $L_2$ ... $L_k$
1行目に地域にできる水たまりの総面積を表す整数 $A$ を出力してください。
2行目に水たまりの数 $k$、各水たまりの面積 $L_i (i = 1, 2, ..., k)$ を断面図の左から順番に空白区切りで出力してください。
制約
$1 \leq 文字列の長さ \leq 20,000$ただし、得点の 50 点分は以下の条件を満たす。
水たまりの数は1つ以下であり ($k \leq 1$)、かつ文字列の長さは 100 以下である。
入力例 1
\\//
出力例 1
4 1 4
入力例 2
\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\
出力例 2
35 5 4 2 1 19 9
Note
题目分析:本题是一道对栈的应用比较好的一道题,不仅运用栈的特性解决了水坑的左右匹配问题,还将他们分开储存,也仅仅只是运用一个下标判断和栈的特性。
代码如下:
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
stack<int> S1;
stack<pair<int, int> > S2;
char ch;
int sum = 0;
for(int i = 1;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;
cout<< ans.size();
for( int i = 0; i < ans.size(); i++){
cout<< " ";
cout<< ans[i];
}
cout << endl;
return 0;
}
相关文章推荐
- ALDS1_3_D: Areas on the Cross-Section Diagram
- 第四章 ALDS1_3_D:Areas on the Cross-Section Diagram
- D - Areas on the Cross-Section Diagram Aizu - ALDS1_3_D
- On the Android ListView listSelector to solve cross-border issues
- 【转发】Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
- XiaoMi/themis: Themis provides cross-row/cross-table transaction on HBase based on google's percolator.
- 解决方法!:getImageData on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
- BackgroundWorker .net 2.0 里微软的多线程组件 学习 Cross-thread operation not valid: Control 'RichTextBox1' accessed from a thread other than the thread it was created on.
- arm-linux-gcc compile the cross-platform mpich2 on ubuntu
- Cross-thread operation not valid: accessed from a thread other than the thread it was created on.
- Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- HDU 3669 Cross the Wall
- Love is in the air and the flowers are on their way
- 关于chrome控制台警告:Synchronous XMLHttpRequest on the main thread
- The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your..
- Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME
- Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to us
- How to programmatically clear the filesystem memory cache in C++ on a Linux system?