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< ...
随机推荐
- struts2 action中传递两个参数到url
<action name="outInDetail" class="formManage_outInDetailAction"> <resul ...
- codeforces #309 div1 A
先说我的解法吧 首先设f(i,j)表示选了前i个球且j种颜色都已经选完了的方案数 这显然是可以随便转移的 #include<cstdio> #include<cstring> ...
- iphone6S“玫瑰金”的秘密——阳极氧化
阳极氧化对多数人来说是一个熟悉又陌生的名词,大多数可能知道它的作用之一就是是能使金属呈现各种各样色彩.最为人熟知的运用阳极氧化技术的产品就是iphone系列产品了,已经推出了金色,玫瑰金色,深空灰色, ...
- 屏蔽QQ聊天对话框中的广告
原文地址: 怎么在QQ聊天对话框中屏蔽广告_百度经验 http://jingyan.baidu.com/article/48a42057ca12c1a924250402.html QQ已经成为 ...
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/z ...
- Layout Resource官方教程(4)<include>与<merge>
Re-using Layouts with <include/> THIS LESSON TEACHES YOU TO Create a Re-usable Layout Use the ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- apache开源项目--Camel
Apache Camel 是一个非常强大的基于规则的路由以及媒介引擎,该引擎提供了一个基于POJO的 企业应用模式(Enterprise Integration Patterns)的实现,你可以采用其 ...
- 【原创】深度神经网络(Deep Neural Network, DNN)
线性模型通过特征间的现行组合来表达“结果-特征集合”之间的对应关系.由于线性模型的表达能力有限,在实践中,只能通过增加“特征计算”的复杂度来优化模型.比如,在广告CTR预估应用中,除了“标题长度.描述 ...
- PostgreSql字符串函数和操作符
本节描述了用于检查和操作字符串数值的函数和操作符.在这个环境中的字符串包括所有 character, character varying, text 类型的值.除非另外说明,所有下面列出的函数都可以处 ...