AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}

C - Grand Garden

In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially, the heights of all flowers are 00. You are given a sequence h={h1,h2,h3,......}h={h1,h2,h3,......} as input. You would like to change the height of Flower kk to hkhk for all kk (1≤k≤N)(1≤k≤N), by repeating the following "watering" operation:

  • Specify integers ll and rr. Increase the height of Flower xx by 11 for all xx such that l≤x≤rl≤x≤r.

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

Constraints

  • 1≤N≤1001≤N≤100
  • 0≤hi≤1000≤hi≤100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
h1h1 h2h2 h3h3 ............ hNhN

Output

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

Input

4
1 2 2 1

Output

2

Input

8
4 23 75 0 23 96 50 100

Output

221

题意:可以任意在区间【L,R】上加1,求通过最少次数得到题目给定的区间的值】

AC代码:

#include<bits/stdc++.h>

using namespace std;
#define int long long
int arr[152052];
signed  main(){
     int ans=0;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%lld",&arr[i]);
    int temp=arr[1];
    for(int i=1;i<=n;i++){
        if(i==n){  // 特判一下
            ans+=max(arr[i],temp);
        }else{
            if(arr[i]>=temp){
                temp=arr[i];
            }else{
                ans+=abs(arr[i]-temp);// 需要减去多加的数
                temp=arr[i];
            }
        }
    }
    cout<<ans;
    return 0;
}

代码2

#include<bits/stdc++.h>

using namespace std;
#define N 515155
int arr[N];

int main(){
  int n;
  cin>>n;
  int ans=0;
  scanf("%d",&arr[1]);
  if(n==1){
    cout<<arr[1];
    return 0;
  }
  int temp=arr[1];
  for(int i=2;i<=n;i++){
    scanf("%d",&arr[i]);
    if(i==n){
      ans+=max(temp,arr[i]);
      break;
    }
    if(arr[i]<temp){
      ans+=temp-arr[i];
      temp=arr[i];
    }else{
      temp=arr[i];
    }
  }
  cout<<ans;
  return 0;
}

原文地址:https://www.cnblogs.com/pengge666/p/11599573.html

时间: 2024-08-04 02:32:04

AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}的相关文章

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)

D - Handstand Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement NN people are arranged in a row from left to right. You are given a string SS of length NN consisting of 0 and 1, and a positive integer KK. The ii-th per

Atcoder Beginner Contest 124 解题报告

心态爆炸.本来能全做出来的.但是由于双开了Comet oj一个比赛,写了ABC就去搞那个的B题 还被搞死了. 回来写了一会D就过了.可惜比赛已经结束了.真的是作死. A - Buttons #include <cstdio> using namespace std; int main() { int x, y; scanf("%d%d", &x, &y); int ans = x > y ? x : y; if (x > y) x--; else

AtCoder Beginner Contest 125 解题报告

那天晚上刚好有事就咕了. 最近的那一场E题还不会写.F题全场又只过了三个?留坑吧... A - Biscuit Generator #include <cstdio> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (