Gym

来源:互联网 发布:python simyou 编辑:程序博客网 时间:2024/06/12 01:22
J. Panoramic Photography
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Most of the students of the law school prefer visiting photo club to the competitions in Roman law. Members of the photo club visit different interesting places, take photos of each other in front of them, and then rate their photos.

Once they appeared on a unbelievably long street which had n buildings in a row. Every member of the photo club took a photo contained, besides the members of the club and people passing by, a segment of the street. In other words, if you number the buildings in the order they are located on the street, each photo contained some buildings with the consecutive numbers.

Some day a Roman law professor of that law school came across the exhibition of the photos from that street. He hasn't remembered how many photos were there, but he has noticed that the i-th building was captured on ai photos. Now he wants to estimate the minimal number of his students in the photo club, considering that no one could present more that one photo at the exhibition.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of buildings.

The second line contains n space-separated integers: ai (0 ≤ ai ≤ 109) — the number of photos that contain the i-th building.

Output

Output a single integer — the minimal number of students in the photo club.

Examples
input
41 3 2 0
output
3
input
61 2 3 1 2 3
output
5


题意:
给你每个建筑被排的次数
问你能几次拍照可以将全部的拍进去

分析一波数据范围  n=1e 5
不能 n^2
n 或者 log(n)*n

log 不知从何而来。。还是往n的复杂度靠吧。

暴力敲了一下 Tle了。。

暴力思路 ,先给高的加上往两边减少次数。。
每次取最高直到最高为0

果断T

看了会暴力发现 每次记录一个tmp (存储上一个建筑的高度)
如果上一个比这个高了那么这个就在拍上一个的时候附带着了。。
如果低了那就res += 高度差。。tmp更新。。
然后再往后走。。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 200000#define ll long longll a[N];int main(){    ll n;    while(~scanf("%lld",&n))    {        ll res=0;        for(int i=1; i<=n; i++)        {            scanf("%lld",&a[i]);        }        ll tmp=0;        int num=0;        for(int i=1; i<=n; i++)        {            if(tmp<a[i])            {                num=a[i]-tmp;                tmp=a[i];            }            else {                tmp=a[i];                num=0;            }            res+=num;           // printf("%lld ",res);        }       // printf("\n");        printf("%lld\n",res);    }}


原创粉丝点击