西南交通大学峨眉校区第二届"INT"杯程序设计竞赛——决赛

当时比赛的时候心情不好,现在重新做

增减序列

Acwing 提高课 差分
题解
对于差分来说
对一个区间[l , r]加一或减一

d[l] += 1 && d[r + 1] += 1;

对于这个题来说有4种可执行的操作

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long ll;

vector<ll> a;
ll d[100005];
ll q = 0 ;//+
ll p = 0 ;//-

int main()
{

    int n ;
    cin >> n;
    for(int i = 0; i < n ; i++){
        ll te;
        cin >> te;
        a.push_back(te);
    }
    
    d[0] = a[0];
    for(int i = 1; i < n ; i ++)    d[i] = a[i] - a[i - 1];

    for(int i = 1; i < n ; i++){
        if( d[i] < 0) p += d[i];
        else if( d[i] > 0 ) q += d[i];
    }

    p = -p;

    cout << max(q,p) << endl;
    cout << abs( p - q ) + 1 <<endl;

    return 0;
}
/*
6
9 8 7 19 2 5

*/

posted @ 2020-12-15 15:39  Hoppz  阅读(86)  评论(0编辑  收藏  举报