Codeforces Round 331 (Div 2)BWilbur and Array(贪心)
2016-01-20 23:13
453 查看
题解:
贪心,修改对p位置后的所有位置生效 最少修改次数使得数列变成b,从前到后修改即可
代码
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; typedef long long LL; int n,x; int main() { while(~scanf("%d",&n)) { LL ans=0; for(int i=1;i<=n;++i) { int v=0; scanf("%d",&x); ans+=abs(x-v); v=x; } printf("%lld\n",ans); } return 0; }
题目
Wilbur and Array
time limit per test 2 secondsmemory limit per test 256 megabytes
input standard input
output 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.
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 test(s)
input
5
1 2 3 4 5
output
5
input
4
1 2 2 1
output
3
Note
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 subtract1.
相关文章推荐
- 《恋人心》 吉他谱
- C++学习笔记之三五法则
- Alex’s Game(I)
- git SSH Key生成步骤
- awesomeplayer_openmax回调函数运行流程
- Day5_鬼代码/加密
- Android M runtime permission介绍
- python 写99乘法表
- haproxy io框架之sepoll
- 在一个jsp页面接收另一个jsp页面通过URL方式传过来的参数
- android tv-TV Apps Checklist
- Codeforces Testing Round #12 B. Restaurant (贪心)
- 0120 父类与子类创建、重写及转型练习
- Servlet3的上传配置@MultipartConfig
- ZH奶酪:JavaScript调用AngularJS的函数/$scope/变量
- 【深度学习&分布式】Parameter Server 详解
- 保留PDB,节省调试耗时
- 最详细eclipse汉化插件安装教程
- awesomeplayer_openmax 初始化分析
- EJB3 阶段总结+一个EJB3案例 (2)