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

   

Output


Input

       

Output


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

AC代码:

#include<bits/stdc++.h>

using namespace std;
#define int long long
];
signed  main(){
     ;
    int n;
    cin>>n;
    ;i<=n;i++)
        scanf("%lld",&arr[i]);
    ];
    ;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;
    ;
}

代码2

#include<bits/stdc++.h>

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

int main(){
  int n;
  cin>>n;
  ;
  scanf(]);
  ){
    cout<<arr[];
    ;
  }
  ];
  ;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;
  ;
}

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

  1. AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)

    D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...

  2. AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】

    Problem Statement There are NN pieces of sushi. Each piece has two parameters: "kind of topping ...

  3. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  4. AtCoder Beginner Contest 068 ABCD题

    A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...

  5. AtCoder Beginner Contest 224

    AtCoder Beginner Contest 224 A - Tires 思路分析: 判断最后一个字符即可. 代码如下: #include <bits/stdc++.h> using ...

  6. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  7. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  8. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

  9. AtCoder Beginner Contest 075 C bridge【图论求桥】

    AtCoder Beginner Contest 075 C bridge 桥就是指图中这样的边,删除它以后整个图不连通.本题就是求桥个数的裸题. dfn[u]指在dfs中搜索到u节点的次序值,low ...

随机推荐

  1. Can you answer these queries III

    Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改 ...

  2. 1186: 零起点学算法93——改革春风吹满地(C)

    一.题目 http://acm.wust.edu.cn/problem.php?id=1186&soj=0 二.分析 多组输入,'0'结束: 顶点的个数在3至100之间: 一定顺序输入坐标: ...

  3. 项目遇到的问题:页面c:forEach循环的数据进行计算传回后台并保持到数据库

    应该还有更简单的方法 但是我不晓得 手动给文本框输入数据保存到数据库 A表 :通过订单编号 查询数据获得 B表 :通过A表中的字段查询遍历获得 问题: 手动输入文本框内容 保存到数据库 页面form提 ...

  4. hdu 6165

    虽然题解上说缩点然后判断入度就可以了,然后比赛的时候瞎暴力过了. #include <iostream> #include <cstring> #include <str ...

  5. 洛谷 P1047 校门外的树(待完善)

    链接:https://www.luogu.org/problemnew/show/P1047 题目: 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是11米.我们可以把马路看 ...

  6. HTTP协议探究(五):H2中一些重要的概念

    一 复习与目标 1 复习 简单密码学.对称加密与非对称加密 数字签名.数字证书 SSL/TLS HTTPS = HTTP + SSL/TLS,SSL/TLS为HTTP提供了保密性.完整性和鉴别性 2 ...

  7. 适配方案(七)iPhone各种系统分辨率、屏幕分辨率

  8. mysql-8.0.16-winx64的最新安装教程

    最近刚学习数据库,首先是了解数据库是什么,数据库.数据表的基本操作,这就面临了一个问题,mysql的安装,我这里下载的是64位的,基于Windows的,以下是在我电脑上的安装过程,希望可以帮助到大家. ...

  9. S2-033、S2-037

    前言 S2-033漏洞和S2-032类似,也是由于开启了动态方法调用,action mapper中的执行的方法名可控,导致了ognl表达式注入. 正文 Rest插件中获取action mapper是用 ...

  10. JS 百度地图 换地图主题颜色(自定义)

    JS 百度地图 换地图主题颜色(自定义) 可通过这个在线编辑得到自己想要的主题:https://developer.baidu.com/map/custom/ <div id="all ...