2018 German Collegiate Programming Contest (GCPC 18) D - Down the Pyramid

題目鏈接:https://codeforces.com/gym/102021/attachments

題意:假設有一個非負的長度爲nn的數列AA,那麼可以得到一個數列BBBi=Ai+Ai+1B_{i} = A_{i}+A_{i+1}。現在題目給出數列BB,問數列AA的組合方式有多少種。

解題心得:設數列AA的第一項爲xx,那麼有x>=0x>=0,根據BB的公式可以得到關於xx的一系列約束條件,需要記錄xx約束的最大值MaxMax和最小值MinMin,最後的答案若Max<MinMax<Min則組合方案數爲00,否則答案爲MaxMin+1Max-Min+1



#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;

int n;
int a[maxn];

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    ll minn = 0, maxx = 1e18, now = 0;
    for (int i = 1; i <= n; i++) {
        if (i & 1) {
            now = a[i] - now;
            maxx = min(maxx, now);
        } else {
            now = a[i] - now;
            minn = max(minn, -now);
        }
    }
    if (minn > maxx || minn < 0 || maxx < 0) cout << 0 << '\n';
    else cout << maxx - minn + 1 << '\n';
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章