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
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次数得到题目给定的区间】】{思维好题}的更多相关文章
- AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)
D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...
- AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】
Problem Statement There are NN pieces of sushi. Each piece has two parameters: "kind of topping ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 068 ABCD题
A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...
- AtCoder Beginner Contest 224
AtCoder Beginner Contest 224 A - Tires 思路分析: 判断最后一个字符即可. 代码如下: #include <bits/stdc++.h> using ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
- AtCoder Beginner Contest 075 C bridge【图论求桥】
AtCoder Beginner Contest 075 C bridge 桥就是指图中这样的边,删除它以后整个图不连通.本题就是求桥个数的裸题. dfn[u]指在dfs中搜索到u节点的次序值,low ...
随机推荐
- [NOIP普及组2001]最大公约数和最小公倍数问题
目录 链接 博客链接 题目链接 题目内容 题目描述 格式 输入 输出 数据 样例 输入 输出 说明 题目名称:最大公约数和最小公倍数问题 来源:2001年NOIP普及组 链接 博客链接 CSDN 洛谷 ...
- 自定义模块,time,datetime以及random
自定义模块,time,datetime以及random 1.自定义模块 自定义一个模块 import #导入 (拿工具箱) 模块分类 1.内置模块(标准库) --python解释器自带的.py文件(模 ...
- 在论坛中出现的比较难的sql问题:7(子查询 判断某个字段的值是否连续)
原文:在论坛中出现的比较难的sql问题:7(子查询 判断某个字段的值是否连续) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以 ...
- 第三代PacBio测序技术的测序原理和读长
针对PacBio单分子测序——第三代测序技术的测序原理和读长 DNA基因测序技术从上世纪70年代起,历经三代技术后,目前已发展成为一项相对成熟的生物产业.测序技术的应用也扩展到了生物.医学.制 ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【轻松一刻】Java制作字符动画
前言 今晚闲来无事,整理了一下电脑中尘封已久的旧代码,看着那些年自己写过的代码,踩过的坑,顿时老泪纵横.正当在感叹之际,突然发现在“马克思”文件夹下出现了一个好玩的项目,那就是N年前刚学Java时写的 ...
- es和redis cluster高可用扩容等等
https://www.jianshu.com/p/ec465da21b4a https://www.cnblogs.com/hello-shf/p/11543468.html https://www ...
- js入门之数组
数组是一种数据类型,数组可以存储很多项, 有序,集合 Array 定义: var names = ['zs','ls','ww','zl'] 访问: 用索引或/下标 数组可以存储多种类型的数据 但是一 ...
- SQLiteDatabase执行update、insert操作的时候,conflictAlgorithm参数的含义区别
/** * When a constraint violation occurs, an immediate ROLLBACK occurs, * thus ending the current tr ...
- 封装promise
// new 做了什么 //1.创建一个新的空对象 //2.将构造函数中的this指向这个新空对象 //3.执行构造函数中的代码 //4.返回这个对象 //5.这个对象有一个__proto__指向构造 ...