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 ...
随机推荐
- python之SQLite笔记
sqlite3 打开文件并创建游标 conn = sqlite3.connect('adressbook.db')c = conn.cursor() 连接对象:sqlite3.connect('数据文 ...
- 十三、GPIO子系统
由于之后的触摸屏驱动分析中使用到了GPIO子系统和i2c子系统,因此在分析触摸屏驱动之前我准备把这两个子系统进行简单分析. 之前我们使用GPIO引脚的方式并不是推荐的方式,当我们更改某一bit时,很有 ...
- golang goroutine进行通信 channel
1.channel的读取与声明 //goroutine之间利用channel进行通信 package main import ( "fmt" "time" ) ...
- linux上启动tomcat报错:Failed to read schema document 'http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! spring在加载xsd文件时总是先试图在本地查找xs ...
- ActiveMQ 简单应用
ActiveMQ简单应用到复杂的订单模块,提高前台的访问速度. 一.当提交订单后,发送消息给ActiveMQ. @Service public class JmsSend { private stat ...
- IOS+H5页面自定义按钮无效
在IOS整合H5页面的时候,自定义的按钮失去效果,Android系统可以. 如图,确定和取消按钮在IOS系统无效. 解决办法是在两个按钮上加上一个style属性即可 <span class=&q ...
- C# EntityCollection 和 List 互转
private EntityCollection<T> ToEntityCollection<T>(this List<T> list) where T : cla ...
- 20-MySQL DBA笔记-可扩展的架构
第20章 可扩展的架构 本章将为读者讲述可扩展的架构相关的知识和技术.可扩展的架构意味着这个架构伸缩性好,我们可以用更多的节点来提高吞吐率,而性能(响应时间)不会下降到不可接受的范围.互联网世界飞速发 ...
- 谈jdbcTemplate与mybatis
为什么会产生 Hibernate Mybatis 这类的dao层框架 传统的jdbc 虽然执行速度很快,但是开发效率很低,随着面向对象开发的设计思想,在面向对象编程中 将对象 进行持久化,存入关系型的 ...
- 开启HSTS让浏览器强制跳转HTTPS访问
开启HSTS让浏览器强制跳转HTTPS访问 来源 https://www.cnblogs.com/luckcs/articles/6944535.html 在网站全站HTTPS后,如果用户手动敲入网站 ...