D - Circular Addition
We have an integer sequence of length NN: x=(x_0,x_1,\cdots,x_{N-1})x=(x0,x1,⋯,xN−1) (note that its index is 00-based). Initially, all elements of xx are 00. You can repeat the following operation any number of times.
You are given an integer sequence of length NN: A=(A_0,A_1,\cdots,A_{N-1})A=(A0,A1,⋯,AN−1). Find the minimum number of operations needed to make xx equal AA. Constraints
Input Input is given from Standard Input in the following format: NN A_0A0 A_1A1 \cdots⋯ A_{N-1}AN−1 Output Print the answer. Sample 1
We should do the following.
Sample 2
Sample 3
|
|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
思路:我们需要把数组从0变成他给出的数组,不妨考虑把他给的数组变成全是0,因为全是0,原数组的数相等,所以差分数组最后全是0,我们要把他的差分变成0,他是一个环(因为j超过n-1就会modn),所以差分数组的正数和等于负数和,因为我们本来需要对原数组进行+1的操作,但是我们现在转化思维对他给出的数组进行-1的操作使他变成0,当我们对他l-r的区间进行减1的操作时差分数组就会进行一个b[l]-1,b[r+1]+1的操作,因为lr是我们任意选的,所以我们只用操作差分的正数之和个操作就可以让原数组一样(选一个正数-1,选一个负数+1),但是当最小的值太大的时候我们把差分数组变成0之后只是让他们变成了一样的数,还需要把操作之后的n个相同的数x操作x下减到0,根据贪心的思想我们每次操作势必会对max(a[i])操作,假设操作了o次,操作之后的数再变成0所需要的操作数h加上o肯定还是max(a[i]),我们把max(a[i])和把差分数组变成0所进行的con次操作数做比较,取最大值:如果max(a[i])>con说明他经过con次操作只是把原数组变成了一样的数而没有变成零,所以总共需要进行max(a[i])次操作,如果con>max(a[i])说明经过con次操作不仅原数组的数一样而且等于0。
注意:以后不管是整数多大都给我整成longlong!!!int不过!!!!
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;long long a[200000];int main(){long long n,ans=0;scanf("%lld",&n);for(long long i=0;i<n;i++){scanf("%lld",&a[i]);}long long max1=a[0];for(long long i=1;i<n;i++){long long t=a[i]-a[i-1];if(t>0){ans+=t;}if(a[i]>max1){max1=a[i];}}long long q=a[0]-a[n-1];if(q>0){ans+=q;}ans=max(max1,ans);printf("%lld",ans);return 0;
}
D - Circular Addition相关推荐
- [ASP.NET4之旅]Circular file references are not allowed
将ASP.NET 2.0的项目升级到ASP.NET 4后,用VS2010编译站点,某些控件出现编译错误"Circular file references are not allowed&qu ...
- 【UVA/Codeforces】1584 Circular Sequence / 792B Counting-out Rhyme(就是一个圈儿...)
https://vjudge.net/problem/UVA-1584 1584 Circular Sequence 输入一个字符串,可以以字符串中任意一个字母作为起始,输出字典序最小的那个字符串 两 ...
- python使用matplotlib可视化饼图(pie plot)、可视化嵌套的环形饼图(Nested circular pie chart)
python使用matplotlib可视化饼图(pie plot).可视化嵌套的环形饼图(Nested circular pie chart) 目录 python使用matplotlib
- partially initialized module ‘numpy‘ has no attribute ‘array‘ (most likely due to a circular import)
partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import) ...
- Accurate circular consensus long-read sequencing improves variant detection and assembly of a human
Accurate circular consensus long-read sequencing improves variant detection and assembly of a human ...
- Requested bean is currently in creation: Is there an unresolvable circular reference?
2019独角兽企业重金招聘Python工程师标准>>> 今天开发写代码,运行代码的时候 出现了 这个错误 : Requested bean is currently in creat ...
- Linux平台Qt creator报错:Circular all - first dependency dropped
在Linux下安装好Qt 5.0之后,使用Qt Creator创建了一个基于QMainWindow的框架程序.原本应该可以顺利的完成编译工作,因为自带的模板工程没有经过任何修改.可是在编译整个工程的时 ...
- WEEX 报错 TypeError: Converting circular structor to JSON 的解决方法
背景 在进行千牛QAP插件开发的时候,有时会遇到 TypeError: Converting circular structor to JSON,并且看不到报错位置,对新手可能会造成疑惑. 原因 we ...
- python 报错 most likely due to a circular import 解决方法
原因各个python文件,互相引用,造成的 循环引用问题. 解决方法:把需要引用的独立成一个文件,让其单向引用 使用python写一个稍微大一点的工程时,经常会遇到循环import,即cicular ...
最新文章
- Oracle嵌套表实例说明
- PAT甲级1144 The Missing Number:[C++题解]哈希表
- 卸载vuecli3_卸载vue2.0并升级vue_cli3.0的实例讲解
- gitkraken把github上的东西clone到本地
- CDH6.x Solr7.x 集成 Ik 分词
- php callable 参数,php 利用反射执行callable
- 手机号正则_一起刷题学习正则表达式
- Excel 使用技巧之 -- 统计、过滤与引用数据
- 期货品种产业链图表_工业
- webstorm 高效开发 (html)
- 记Python的一些用法
- 信息安全工程师 学习笔记 完结
- Java面试必看的18个开源项目
- NVIDIA GeForce Experience报错:“验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序”的解决方法
- [译]PostgreSQL15 public shema权限增强
- hdu1728 (直撞bfs)
- ElasticSearch 2 (25) - 语言处理系列之同义词
- C语言汇总学生成绩,c语言用二维数组统计学生成绩.doc
- 看完南京前11年房价,我哭了!今年实在太特么狠了
- 从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期