CodeForces ---596B--Wilbur and Array(贪心模拟)
| Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
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
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.
Source
题意:
给你一个长度为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(贪心模拟)的更多相关文章
- CodeForces 596B Wilbur and Array
简单题,一个一个操作,最后就是答案. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Educational Codeforces Round 52D(ARRAY,模拟最短路)
#include<bits/stdc++.h>using namespace std;int n,x;int chess[17*17];//记录棋盘上的numberarray<int ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 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 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
随机推荐
- HttpClient学习系列 -- 学习总结
jar包: HttpClient 4.x版本 简要介绍 HttpComponents 包括 HttpCore包和HttpClient包 HttpClient:Http的执行http请求 Default ...
- TP5异常处理
TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...
- 1. Two Sum[E]两数之和
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- GIT 常用方法
代码提交顺序: conmmit(提交代码到本地仓库) --->>> pull(将本地仓库代码合并) ---->>> push(将本地合并后的代码提交到 ...
- js-apply call bind 浅析
call 1.第一个参数指定了this,第二个参数传给this,也就是call前面的函数,作为他的参数第三个参数也一样 指定了this,就是执行环境,greet的this在i里面找 function ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- 谈谈javascript中原型继承
什么是继承?拿来主义:自己没有,别人有,把别人的拿过来使用或者让其成为自己的 如何实现继承的方式 原型继承 混入继承 经典继承 1. 混入继承 由于一个对象可以继承自任意的对象,即:o可以继承自对象o ...
- Hessian 接口使用示例总结
一.使用hessian接口准备 首先,hessian接口的使用,必须要准备hessian接口的jar包,本文使用的jar包如下:hessian-4.0.7.jar; Hessian接口的使用一般是在两 ...
- Oracle PL/SQL开发基础(第三十三弹:EXCEPTION_INIT)
如果有一些异常并没有异常名称,比如一些ORA-开头的异常并没有一个友好的预定义的异常定义,此时在WHEN子句中无法使用具体的异常名称,必须要使用OTHERS异常处理器进行捕捉.通过EXCEPTION_ ...
- win7系统桌面上图标都变成lnk后缀
1.右键点击空白处,选择“新建”,点击“文本文档”: 2.将文档命名为“1”,后缀名改为inf: 3.双击打开,复制以下内容: [Version] Signature="$Chicago$& ...