Codeforces Wilbur and Array
Description
Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn.
Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every positioni, so this array is not given in the input.
The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).
Output
Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.
Sample Input
5
1 2 3 4 5
5
4
1 2 2 1
3
Hint
In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.
In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.
题意:给你一个数组b[](这个数组输入值)每次可以修改数组a[]中a[i]到a[n]的值(这些数同时加1或者减1 a数组初始化为0)问你最少操作多少次可以使a数组完全等于b数组
题解:因为我们每次只能确定一个数的值(即a[i] i从1到n依次取值)所以我们应当记录下每次确定一个数需要的步数,总步数就是确定所有数的步数之和 将b数组循环遍历一遍,用sum记下操作步数,用s记录当前操作过后a[i+1]的值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
#define MAX 200100
using namespace std;
int b[MAX];
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&b[i]);
__int64 s=0;
__int64 sum=0;
for(i=0;i<n;i++)
{
if(s<b[i])
{
sum+=(b[i]-s);//s小于b[i]则这个数需要操作b[i]-s步
s+=(b[i]-s);//因为上边的操作为a[i]+1 +1操作所以s取值变为 +=
}
if(s>b[i])
{
sum+=(s-b[i]);//s大于b[i]则这个数需要操作s-b[i]步
s-=(s-b[i]);//因为上边的操作为a[i]-1 -1操作所以s取值变为 -=
}
}
printf("%I64d\n",sum);
}
return 0;
}
Codeforces Wilbur and Array的更多相关文章
- Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题
B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...
- Codeforces Round #331 (Div. 2) B. Wilbur and Array
B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- Codeforces 1077C Good Array 坑 C
Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...
- codeforces 482B. Interesting Array【线段树区间更新】
题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...
- codeforces 407C Curious Array
codeforces 407C Curious Array UPD: 我觉得这个做法比较好理解啊 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377 ...
- codeforces 797 E. Array Queries【dp,暴力】
题目链接:codeforces 797 E. Array Queries 题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...
- CodeForces 596B Wilbur and Array
简单题,一个一个操作,最后就是答案. #include<cstdio> #include<cstring> #include<cmath> #include< ...
随机推荐
- linux踢人命令 pkill踢人用法
首先使用who命令查看在线用户,然后踢人. 强制踢人命令格式:pkill -kill -t tty 解释: pkill -kill -t 踢人命令 tty 所踢用户的TTY或者pts/x(x代表数字) ...
- 孟岩的c++ 的学习方法,这何尝有不是做人做事的方法呢?
“(孟岩)我主张,在具备基础之后,学习任何新东西,都要抓住主线,突出重点.对 于关键理论的学习,要集中精力,速战速决.而旁枝末节和非本质性的知识内容,完全可 以留给实践去零敲碎打. “原因是这样的,任 ...
- HeadFirst设计模式之门面模式
一. 1.The Facade Pattern provides a unifi ed interface to a set of interfaces in a subsytem. Facade d ...
- Android:控件ListView列表项与适配器结合使用
Listview是用来展示一些重复性的数据用的,比如一些列表集合数据展示到手机,需要适配器作为载体获取数据,最后将数据填充到布局. ListView里面的每个子项Item可以使一个字符串,也可以是一个 ...
- laravel5的坑
以此记录学习laravel的一些问题 问题:laravel转移文件夹到另外一pc或者环境后访问出现500 设置权限为777 问题: 设置路由后页面总是404 not found 解决:需要在apach ...
- size_t和ssize_t
Ssize_t 与size_t 跟踪linux源码得到以下宏: #ifndef _SIZE_T #define _SIZE_T typedef __kernel_size_t size ...
- strcat与strncat的C/C++实现
2013-07-05 15:47:19 本函数给出了几种strcat与strncat的实现,有ugly implementation,也有good implementation.并参考标准库中的imp ...
- ubuntu装机
备份: .bashrc profile .vimrc exports defults/ 各种workspace中的源码 goagent/ 重转后安装: apt-get install openjdk- ...
- JSP内置标签 JSP中JavaBean标签 JSP开发模式 EL和JSTL快速入门
2 JSP内置标签(美化+业务逻辑) 1)为了取代<%%>脚本形式,使用JSP标签/JSP动作,目的:与JSP页面的美化,即JSP面页都是由标签组成,不再有其它的内容 2)JSP内 ...
- Android开发之隐式Intent中Intent-filter的三个属性-action,category,data
使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...