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:

题目描述


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;
}



目录
相关文章
|
2月前
|
设计模式 中间件 程序员
【C/C++ 奇异递归模板模式 】C++中CRTP模式(Curiously Recurring Template Pattern)的艺术和科学
【C/C++ 奇异递归模板模式 】C++中CRTP模式(Curiously Recurring Template Pattern)的艺术和科学
22 3
|
11天前
|
存储 算法 Java
信不信?一个R-tree问题就能刷掉99%的应聘者
R-tree是一种高效的空间索引数据结构,特别适合处理高维空间数据。它通过将数据项组织在树结构中,最小化每个节点的边界矩形覆盖范围,从而减少了数据的冗余和提高了查询效率。
信不信?一个R-tree问题就能刷掉99%的应聘者
|
运维 监控 Java
左耳朵耗子:我做系统架构的一些原则
左耳朵耗子:我做系统架构的一些原则
123 0
|
人工智能
Codeforces-Adding Powers(进制问题+思维)
Codeforces-Adding Powers(进制问题+思维)
67 0
codeforces 1393 —— B. Applejack and Storages (思维)
codeforces 1393 —— B. Applejack and Storages (思维)
64 0
Codeforces 1263D.Secret Passwords(并查集 思维)
Codeforces 1263D.Secret Passwords(并查集 思维)
54 0
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
92 0
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
99 0
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
Codeforces 796D. Police Stations (思维+bfs)
Codeforces 796D. Police Stations (思维+bfs)
79 0