Cod sursa(job #1946314)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 30 martie 2017 07:36:27
Problema Inundatii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

class sum_class{
    vector<ll> v;
public:
    sum_class(const vector<ll>& vv): v(vv.size()+1, 0){
        partial_sum(begin(vv), end(vv), begin(v)+1); }
    ll query(ll st, ll dr){ return v[dr+1] - v[st]; } };

ll sum_to(const ll n){ return n * (n+1ll) / 2ll; }

ll solve_coord(const vector<ll>& v){
    sum_class sc(v);
    ll n = v.size(), rez = (ll)1e9 * (ll)1e9;
    for(ll i = 0; i < n; ++i)
        rez = min<ll>(rez, sum_to(i) + sum_to(n-i-1) + sc.query(0, i-1) -  i * v[i] 
            + (n-i-1) * v[i] - sc.query(i+1, n-1));
    return rez; }

int main(){
    ifstream f("inundatii.in");
    ofstream g("inundatii.out");

    int n;
    f >> n;
    vector<ll> xs, ys, zs;
    for(int i = 0, x, y, z; i < n; ++i)
        f >> x >> y >> z,
        xs.push_back(x),
        ys.push_back(y),
        zs.push_back(z);
    g << solve_coord(xs) + solve_coord(ys) + solve_coord(zs) << endl;
    return 0; }