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);
    }
  • 相关阅读:
    python3 多线程的基本用法
    ASP.NET MVC下的四种验证编程方式
    漫步ASP.NET MVC的处理管线
    asp.net mvc源码分析-Action篇 IModelBinder
    在 Asp.NET MVC 中使用 SignalR 实现推送功能
    说说Python 中的文件操作 和 目录操作
    Python 用 os.walk 遍历目录
    我来说说MVC过滤器
    学习Python必须要知道的常用模块
    Python中的并发编程
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4967948.html
Copyright © 2011-2022 走看看