Grand Garden思维题

题目描述

In a flower bed, there are N flowers, numbered 1,2,…,N. Initially, the heights of all flowers are 0. You are given a sequence h={h1,h2,h3,…} as input. You would like to change the height of Flower k to hk for all k (1≤k≤N), by repeating the following “watering” operation:
Specify integers l and r. Increase the height of Flower x by
1 for all x such that l≤x≤r.
Find the minimum number of watering operations required to satisfy the condition.

Constraints
·1≤N≤1000≤hi≤100
·All values in input are integers.

输入

Input is given from Standard Input in the following format:
N
h1 h2 h3 … hN

输出

Print the minimum number of watering operations required to satisfy the condition.

样例输入

4
1 2 2 1

样例输出

2

提示

The minimum number of watering operations required is 2. One way to achieve it is:
Perform the operation with (l,r)=(1,3).
Perform the operation with (l,r)=(2,4).

这是一道思维题

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
ll num[maxn],ans;
ll n,m;
ll a[maxn],s[maxn];
start{
    int n=read;
    for(int i=1;i<=n;i++) num[i]=read;
    for(int i=1;i<=n;i++)
        if(num[i-1]<num[i]) 
        	ans+=num[i]-num[i-1];
    cout<<ans;
	end;
}

原文地址:https://www.cnblogs.com/PushyTao/p/13144217.html