CodeForces 596B Wilbur and Array 贪心

原创 2015年11月18日 18:21:13

给出原始{ai}={0},{bi}。每次修改{ai..n}+1或-1,求最小操作次数使{ai}=={bi}。

累计相邻两数差即可。

因为差最大10^9,数字有2*10^5,要long long。

#include <cstdio>
#include <cstdlib>
typedef long long ll;
int main() {
    static ll a[200001];
    int n,i;
    scanf("%d", &n);
    for(i=1;i<=n;i++) scanf("%I64d", &a[i]);
    ll ans=abs(a[1]);
    for(i=2;i<=n;i++)
        ans+=abs(a[i]-a[i-1]);
    printf("%I64d", ans);
    return 0;
}


B. Wilbur and Array
time limit per test
2 seconds
memory 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 elementsai, 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 1234, 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.



版权声明:转载请注明 http://blog.csdn.net/huanghongxun/

相关文章推荐

CF - 596B. Wilbur and Array 贪心

1.题目描述: B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces596C Wilbur and Points(贪心)

@(K ACMer)题意: 给你一个点集,每个点的weight为(y−x)(y - x),你需要给出这些点的一个排列,这些点满足:如果y2>=y1且x2>=x1y2 >= y1 且x2 >= x1,...

【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b

Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes ...

续续 Codeforces 596 C Wilbur and Points

传送门: http://codeforces.com/contest/596/problem/C附上那份符合我当初屌丝想法的只排序不需要任何数据结构的写法 其实本质思想都是贪心,即先通过排序找到每...

Codeforces 596E Wilbur and Strings(序列自动机)

@(K ACMer)题意: 在一个n∗mn*m的图上,每个空格写着0−90-910个数字中的一个.有两个长度为10的数组a1,a2...ana_1,a_2...a_nb1,b2,b3,...bnb_...

Codeforces 596 C Wilbur and Points

传送门: http://codeforces.com/contest/596/problem/C题意: 给n个点的坐标,然后让你重新排序,规则是x,y坐标均小的要排在前面,规则具体看题即可,使得点...

CodeForces 596A-Wilbur and Swimming Pool

After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a ...

Codeforces596C. Wilbur and Points

C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input stan...

续 Codeforces 596 C Wilbur and Points

传送门: http://codeforces.com/contest/596/problem/C前一篇是数据结构的解法,这回换一种解法,其实是屌丝解法,方法和上面一样,只是用 vector加上局部...

codeforces 596E Wilbur and Strings(DFS)

题目链接 E. Wilbur and Strings time limit per test 3 seconds memory limit per test 25...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)