CFGym - 101149J. Panoramic Photography - 思维

1.题目描述:

J. Panoramic Photography
time limit per test
2 seconds
memory limit per test
256 megabytes
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
4
1 3 2 0
output
3
input
6
1 2 3 1 2 3
output
5

2.题意概述:

俱乐部一群学生访问这条街道不同的位置,并且拍摄了照片,现在给这条街的建筑物编号,并且所有学生照片里面照到的建筑物,输入第i个建筑物被照了a[i]次,问你学生可能数量的最小值

3.解题思路:

这题注意的是,拍摄照片时候可能会照到相邻建筑。注意到第i个建筑如果a[i] > a[i - 1]表示从第i个建筑开始有a[i] - a[i - 1]次才开始被照到,要学生数尽可能的少,那么假设学生照到建筑的范围尽可能地大,因此只要枚举max(a[i] - a[i-1], 0)求和就行。

4.AC代码:

#include <stdio.h>
using namespace std;
typedef long long ll;


int main()
{
	int n, tmp;
	while (scanf("%d", &n) != EOF)
	{
		ll ans = 0;
		tmp = 0;
		for (int i = 0; i < n; i++)
		{
			int x;
			scanf("%d", &x);
			if (x > tmp)
				ans += x - tmp;
			tmp = x;
		}
		printf("%lld\n", ans);
	}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值