Codeforces Round #331 (Div. 2) B. Wilbur and Array
2 seconds
256 megabytes
standard input
standard output
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.
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 position i, so this array is not given in the input.
The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).
Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.
5
1 2 3 4 5
5
4
1 2 2 1
3
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.
题意:输入n 接下来 输入n个数
n个数初始都为零 现在可以执行两种操作 增加1或者减少1 例如 i个数增加1时 第i+1,i+2..到n 个数 都增加1
执行一次 算一次操作 问最少经过多少次操作 使得这n个数的值为 输入的排列
解答: for循坏遍历一遍 就可以保证操作数最小
比如第i个位置操作几次 只与第i-1位置上的数有关 因为题目规定的操作只对之后的有影响 (注意理解)!!
注意 :当处理第一位的时候 默认之前一位为0
__int64
别乱用abs
做的一手死!!!
#include<bits/stdc++.h>
using namespace std;
__int64 next,a;
__int64 re;
__int64 n;
__int64 ans;
int main()
{
re=0;
scanf("%I64d",&n);
//scanf("%I64d",&a);
re=0;
next=0;
for(int i=0;i<n;i++)
{
scanf("%I64d",&a);
ans=a-next;
next=a;
if(ans<0)
ans=-ans;
re+=ans; }
printf("%I64d\n",re);
return 0;
}
Codeforces Round #331 (Div. 2) B. 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) E. Wilbur and Strings dfs乱搞
E. Wilbur and Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596 ...
- Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索
D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...
- Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心
C. Wilbur and Points Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/ ...
- Codeforces Round #331 (Div. 2) A. Wilbur and Swimming Pool 水题
A. Wilbur and Swimming Pool Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool
A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #331 (Div. 2)
水 A - Wilbur and Swimming Pool 自从打完北京区域赛,对矩形有种莫名的恐惧.. #include <bits/stdc++.h> using namespace ...
- Codeforces Round #331 (Div. 2) A
A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- Siki_Unity_3-13_编程内功修炼-算法
Unity 3-13 编程内功修炼 -- 算法 任务1&2:课程介绍 主要算法: 分治法 堆排序 二叉树 动态规划 贪心算法 图 任务3:分治算法 -- Divide and Conquer ...
- C#使用EF连接PGSql数据库
前言 由于项目需要,使用到了PGSql数据库,说实话这是第一次接触并且听说PGSql(PostgreSQL)关系型数据库,之前一直使用的都是SqlServer,一头雾水的各种找资源,终于将PGSql与 ...
- Apache POI:Excel读写库
1)Apache POI 简介 Apache POI是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写 ...
- SGU 326 Perspective(最大流)
Description Breaking news! A Russian billionaire has bought a yet undisclosed NBA team. He's plannin ...
- Java学习个人备忘录之异常
概述 异常:是在运行时期发生的不正常情况. 在java中用类的形式对不正常情况进行了描述和封装对象. 描述不正常的情况的类,就称为异常类. 以前正常流程代码和问题处理代码相结合,现在将正常流程代码和问 ...
- Spark Streaming - DStream
1 Overview Spark Streaming is an extension of the core Spark API that enables scalable, high-through ...
- HttpServletRequest和HttpServletResponse实例
先看一下web.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- css那些事儿2 经典两列布局
<!DOCTYPE html> <html> <head> <title></title> <link rel="style ...
- 通过access_token openid获取微信用户昵称等信息
<?php header('Access-Control-Allow-Origin:*'); $access_token = !empty($_GET['access_token'])?$_GE ...
- kafka启动出现:Unsupported major.minor version 52.0 错误
具体的错误输出: Exception in thread "main" java.lang.UnsupportedClassVersionError: kafka/Kafka : ...