3:noip2013t Day2 T1: Block ContestView Submit Statistics question total time limit: 1000ms memory limit: 131072kB description
Spring and Spring Kindergarten held the annual "Building Block Contest". This year's game is to build a building with a width of n, the building can be seen by the n block width of 1 blocks, the final height of block I need to be hi.
Before the construction begins, there is no building block (it can be seen as a block of n blocks of height 0). After each operation, the children can select a continuous interval [l,r] and then increase the height of all blocks from block L to block R (including Block L and R Block) by 1 respectively.
Little M was a clever little friend, and she soon came up with the best strategy for building, making the minimum number of operations required. But she is not a diligent child, so I would like to ask you to help achieve this strategy, and to find the minimum number of operations. The input input contains two lines, and the first row contains an integer n, which indicates the width of the building.
The second line contains n integers, and the first integer is hi. Outputs only one row, which is the minimum number of operations required to build. Sample input
5
2 3 4 1 2
Sample output
5
Hint Data range
One of the best possible scenarios, select [1,5] [1,3] [2,3] [3,3] [5,5]
For 30% of the data, there are 1≤n≤10;
For 70% of the data, there are 1≤n≤1000;
For 100% of the data, there is 1≤n≤100000,0≤hi≤10000. SOURCE noip2013t Day2 T1
Greedy method
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
using namespace std;
int n;
int h[100005]={0};
int cnt=0;
void Dfs (int l,int R)//starting from the bottom, find the longest operational interval
{
if (l>r) return each time;
if (l==r)
{
cnt+=h[l];
return;
}
int t=h[l];
for (int i=l;i<=r;++i)
{
if (h[i]<t) t=h[i];
}
cnt+=t;
int j=l;
int i;
for (i=l;i<=r;++i)//split for several heaps after recursion for the same operation
{
h[i]-=t;
if (h[i]==0)
{
dfs (j,i-1);
j=i+1;
}
}
DFS (J,I-1);
}
int main ()
{
cin>>n;
for (int i=0;i<n;++i)
{
cin>>h[i];
}
DFS (0,n-1);
cout<<cnt<<endl;
return 0;
}