zoukankan      html  css  js  c++  java
  • 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 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 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
    ",re);
        return 0;
    }
    
  • 相关阅读:
    C#获取EF实体对象或自定义属性类的字段名称和值
    Android 短信广播接收相关问题
    Silverlight5 Tools安装失败及解决方案
    验证码刷新
    Silverlight动态生成控件实例
    spring 属性注入
    asp.net XMLHttpRequest 进度条以及lengthComputable always false的解决办法
    Spring.Net 初探之牛刀小试
    iframe载入页面过程显示动画效果
    一次让人晕到吐血的接包经历
  • 原文地址:https://www.cnblogs.com/hsd-/p/4970341.html
Copyright © 2011-2022 走看看