382C - Arithmetic Progression - CodeForces Solution


implementation sortings *1700

Please click on ads to support us..

C++ Code:

#include <iomanip>
#include <iostream>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <numeric>
#include <cstring>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<char, int> mci;
 
#define f first
#define s second
#define mp make_pair
 
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) x.size()
 
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int MAX = 1e7;

void solve() {
    int n; cin >> n;
    vll a(n);
    for (int i = 0; i < n; i++) 
        cin >> a[i];

    set<int> ans;
    sort(all(a));
    if (n == 1) {
        cout << -1 << '\n';
        return;
    } else if (n == 2) {
        ll d = a[1] - a[0];
        if (d % 2 == 0) {
            ans.insert(a[0] + d/2);
        }
        ans.insert(a[0] - d);
        ans.insert(a[1] + d);
    } else {
        map<ll, vll> diff;
        for (int i = 0; i < n-1; i++) {
            ll d = a[i+1] - a[i];
            diff[d].pb(a[i] + d/2);
        }

        if (diff.size() == 1) {
            ll d = diff.begin()->f;
            ans.insert(a[0] - d);
            ans.insert(a[n-1] + d);
        } else if (diff.size() == 2) {
            auto x = diff.begin();
            auto y = ++diff.begin();
            if (x->s.size() == 1 && x->f % 2 == 0 && x->f/2 == y->f) {
                ans.insert(x->s.front());
            } else if (y->s.size() == 1 && y->f % 2 == 0 && y->f/2 == x->f) {
                ans.insert(y->s.front());
            }
        }
    }

    cout << ans.size() << '\n';
    for (auto x : ans) {
        cout << x << ' ';
    }
    cout << '\n';
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    // int t; cin >> t;
    // while (t--)
    //     solve();
	solve(); 
	return 0;
}


Comments

Submit
0 Comments
More Questions

1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset