• 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;
    }
    
  • 相关阅读:
    Oracle索引
    Oracle Union Union All 对查询结果集操作
    Oracle表连接
    BIOS + MBR > UEFI + GPT
    Opensource Licenses
    Linux 系统下使用dd命令备份还原MBR主引导记录
    Linux 文件类型笔记
    Linux 分支那么多,这里可以帮你缩小选择范围
    Arch Linux 的休眠设置
    Arch Linux pacman 与其他发行版操作比较
  • 原文地址:https://www.cnblogs.com/hsd-/p/4970341.html
Copyright © 2020-2023  润新知