Rainwater
/*
Time limit: 1000ms
Memory limit: 256mb
---------------------Copy the following code, complete it and submit---------------------
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define DEFAULT_VALUE -1
// Notice: Each element in the stack has an additional field `pos` to store the position of the value in the array (0-indexed)
struct stack {
int capacity;
int* index;
int* pos;
int top;
};
typedef struct stack *StackADT;
StackADT createStack(int capacity);
void deleteStack(StackADT stack);
int isEmpty(StackADT stack);
int isFull(StackADT stack);
void push(StackADT stack, int value, int pos);
void pop(StackADT stack);
int top_value(StackADT stack);
int top_pos(StackADT stack);
StackADT createStack(int capacity) {
StackADT stack = (StackADT)malloc(sizeof(struct stack));
stack->capacity = capacity;
stack->index = (int*)malloc(sizeof(int) * capacity);
stack->pos = (int*)malloc(sizeof(int) * capacity);
stack->top = -1;
return stack;
}
void deleteStack(StackADT stack)
{
// WRITE YOUR CODE HERE
}
int isEmpty(StackADT stack)
{
// WRITE YOUR CODE HERE
}
int isFull(StackADT stack)
{
// WRITE YOUR CODE HERE
}
// Notice: The position of the new element is determined by the parameter `pos`
void push(StackADT stack, int value, int pos) {
// WRITE YOUR CODE HERE
}
void pop(StackADT stack) {
// WRITE YOUR CODE HERE
}
// Return the value of the top element in the stack
int top_value(StackADT stack) {
// WRITE YOUR CODE HERE
}
// Return the position of the top element in the stack (0-indexed)
int top_pos(StackADT stack) {
// WRITE YOUR CODE HERE
}
long long solve(int n, int* a)
{
long long sum = 0;
StackADT stack = createStack(n);
// WRITE YOUR CODE HERE
}
// DO NOT MODIFY THE CODE BELOW
int main(void) {
int n;
scanf("%d", &n);
int* a = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
printf("%lld\n", solve(n, a));
return 0;
}
---------------------------------------End of Code---------------------------------------
## Rainwater
Given $n$ non-negative integers $a[0..n-1]$ that represent the height map of bars (each bar has a width of 1), calculate the amount of rainwater that can be trapped after raining for the bars arranged in this configuration.
Notice that the answer may be very large, so you need an appropriate variable type to store it.
**Hint**: Solve the problem by maintain a monotonic decreasing stack of bar heights.
You need to implement the stack ADT by array.
And you need to store both the indices and heights in the stack.
### Input
First line contains one integer $n$ ($1 \leq n \leq 500000$): the number of bars.
The second line contain $n$ non-negative integers $a[0..n-1]$. ($0\leq a_i \leq 500000$)
### Output
Output one number representing the amount of rainwater that can be trapped.
### Example
**Input**
7
7 5 8 2 1 0 4
**Output**
11
**Explanation**
'*' represents the bars and '.' represent the rainwater that can be trapped.
The demonstration for the example is as follows:
*
*.*
*.*
***
***...*
***...*
****..*
*****.*
The number of '.', which is the amount of the rainwater, is 11.
Submit