Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit
Status

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 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).

Output

Print the minimum number of steps that Wilbur needs to make in order to achieve
ai = bi for all
i.

Sample Input

Input
5
1 2 3 4 5
Output
5
Input
4
1 2 2 1
Output
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.

Source

Codeforces Round #331 (Div. 2)

题意:

给你一个长度为n的数组,然后对一个数组a操作,每次选取一个i,对i--n的元素进行加一或者减一的操作,问至少要多少步才可以将a变为目标数组

直接贪心模拟,num[0]=0;第一个数num[1]需要操作的次数一定是加num[1]次,后边的数在他前边数的基础上操作| num[i]-num[i-1] | 次,加或者减

#include<stdio.h>
#include<string.h>
#include<math.h>
int num[200000+10];
int main()
{
int n;
long long sum=0;//数据范围略大,操作次数多了点
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
sum+=abs(num[i]-num[i-1]);
}
printf("%lld\n",sum);
return 0;
}

CodeForces ---596B--Wilbur and Array(贪心模拟)的更多相关文章

  1. CodeForces 596B Wilbur and Array

    简单题,一个一个操作,最后就是答案. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  2. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  3. Educational Codeforces Round 52D(ARRAY,模拟最短路)

    #include<bits/stdc++.h>using namespace std;int n,x;int chess[17*17];//记录棋盘上的numberarray<int ...

  4. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  8. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  9. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

随机推荐

  1. c7---函数

    // // main.c // 函数练习 // // Created by xiaomage on 15/6/7. // Copyright (c) 2015年 xiaomage. All right ...

  2. intellij idea 运行jedis

    到这里下载 http://mvnrepository.com/ jar包! 将jar包放入项目目录中,并引入! 引入包到项目中!创建对象! package com.company; import re ...

  3. MVC/MVP/MVVM区别——MVVM就是angular,视图和数据双向绑定

    摘自:http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html 一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面 ...

  4. ElementUI 表格表头筛选框的高度设置,超出一定高度,显示滚动条

    最近项目发现一个问题table表头筛选的时候,由于筛选内容过多导致弹出框超出屏幕,并且无法滚动,应急的办法是缩小浏览器显示比例让更多内容显示

  5. line-height与间距总总

    一点说明(个人吐槽,可以略过) 之所以想写这篇文章,是因为自己工作的经验总结.以前的页面编写极度不注重间距大小,特别是行级元素间距.认为只要差不多好就行了,没必要花那么大的精力去抠几px的小细节.事实 ...

  6. Android 制作类似支付圆圈和打钩界面ProgressWheel

    首先要说明的是,制作圆圈旋转的效果并不是博主做的,是参照了github上的一个代码,只是在上面添加了修改,对其优化并增加了一个打钩的动画. 先来看下效果,1+的手机获取root权限真是难,没法录屏,只 ...

  7. 自动化框架的两种断言设计(pytest 版)

    自动化测试断言失败时,根据不同业务场景,可能需要立即终止或继续执行.这里以 Appium + pytest 为例. 一. 断言失败立即终止 用途一:用例的预期结果是其他用例的前提条件时,assert ...

  8. Incorrect integer value: '' for column 'RegNum' at row 1

    数据库版本: 5.6.16​​操作系统: Red Hat Enterprise Linux Server release 6.3 (Santiago)​​在插入数据的时候提示告警:​mysql> ...

  9. 数据库自动备份压缩脚本(备份最近七天,七天之前自动删除,只保留rar文件)

    把下面脚本添加到服务器计划任务中去,设置为每天执行即可,文件备份路径即为脚本所在路径,必须安装压缩文件 @echo offrem 计算指定天数之前的日期,用于后面删除指定天数的数据set DaysAg ...

  10. 页面定制CSS代码初探(六):h2、h3 标题自动生成序号 详细探索过程

    前言 最近在整理博客写作格式的规范,碰到一个问题:标题要不要加序号? 直到我碰到一个人这么说 手动维护编号实在是一件很闹心的事情, 如果位置靠前的某个段落被删除了, 那么几乎每个段落的编号都要手动修改 ...