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< ...
随机推荐
- CyanogenMod刷机以及Google Play应用商店安装方法介绍
http://blog.csdn.net/zcynical/article/details/19241595 写在前面: 本文介绍的方法除第一步外,适用于所有CM系统支持的设备,第一步由于用到了PC上 ...
- Git教程之标签管理
发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照.Git的 ...
- R语言学习笔记:数据的可视化
本文参考数据挖掘与R第二章节 读入数据 方法1,下载Data mining with r的配套包 install.packages('DMwR') 方法2,下载txt数据,并且读入数据.方法见上文. ...
- 《RabbitMQ in action》
Producers create messages and publish (send) them to a broker server (RabbitMQ).What’s a message? A ...
- js构造函数式编程
1.函数式编程 //创建和初始化地图函数: function initMap(){ createMap();//创建地图 setMapEvent();//设置地图事件 addMapControl(); ...
- 在SQLite中使用索引优化查询速度
在进行多个表联合查询的时候,使用索引可以显著的提高速度,刚才用SQLite做了一下测试. 建立三个表: create table t1 (id integer primary key,num inte ...
- function 的声明
<?php function test() { echo "abc"; } test(); ?> 结论: 一 编译 a.对 函数声明进行词法分析和语法分析:在语法分析中 ...
- Repeater的ItemCommand事件(LinkButton)
Repeater的ItemCommand事件,就是在里面加一个超链接的按钮,所有按钮都指向同一个事件,就是ItemCommand事件. 至于如何区分是点击的什么按钮,还有传的值,需要用到LinkBut ...
- [原]Unity3D深入浅出 - 常用类的成员变量和成员函数(Tranform、Time、Random、Mathf、Input)
Transform的成员变量 Transform的成员函数 Time类,获取和时间相关的信息,可用来计算帧速率,调整时间流逝的速度等. Random类,可用来生成随机数,随机点和旋转. Mathf类提 ...
- 使用委托的BeginInvoke方法来完成复杂任务的操作
现在假设我有这样一个窗体(包含一个进度条和一个按钮与两个文本框),在第一个文本框中输入一个数字进行阶乘运算,在此过程中进度条与运算进度保持一致,同时可以在第二个文本框中进行其它工作(比如输入).对付这 ...