【題解】Google Kick Start 2020 Round E Longest Arithmetic

【題目敘述】https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff47/00000000003bf4ed

#include <iostream>
using namespace std;

int t, Case, n, a[200005];

int main() {
    cin >> t;
    for (int Case = 1; Case <= t; Case++){
        cin >> n;
        cin >> a[0] >> a[1];
        int cnt = 2, ans = 2, d = a[1]-a[0];
        for (int i = 2; i < n; i++){
            cin >> a[i];
            if (a[i]-a[i-1] == d) cnt++;
            else{
                d = a[i]-a[i-1];
                ans = max(ans, cnt);
                cnt = 2;
            }
            ans = max(ans, cnt);
        }
        cout << "Case #" << Case << ": " << ans << "\n";
    }
}

分享本文 Share with friends