zoukankan      html  css  js  c++  java
  • 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/problem/B

    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

    Sample Output

    5

    HINT

    题意

    a[i]数组一开始都是0

    然后你可以使得i-n都加1,也可以使得i-n都减一

    然后问你最少操作数

    题解:

    扫一遍就好了,直接暴力,注意会爆int

    代码

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    #define maxn 200005
    long long b[maxn];
    int main()
    {
        int n;cin>>n;
        for(int i=0;i<n;i++)
            scanf("%lld",&b[i]);
        long long now = 0;
        long long ans = 0;
        for(int i=0;i<n;i++)
        {
            if(now!=b[i])
            {
                ans+= abs(b[i]-now);
                now = b[i];
            }
        }
        printf("%lld
    ",ans);
    }
  • 相关阅读:
    你可见过一种基于状压的二进制筛法?
    dp
    tricks
    csp2020 游记
    洛谷P2982 [USACO10FEB]慢下来Slowing down
    NOIP 2018 大翻车记
    2019 ICPC 南京网络赛
    POJ2778 AC自动机 + 快速矩阵幂
    2019 CCPC网络赛
    2018ICPC 北京
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4967948.html
Copyright © 2011-2022 走看看