Cod sursa(job #2970966)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 26 ianuarie 2023 11:02:50
Problema Wanted Scor 10
Compilator cpp-64 Status done
Runda sa_fac_schema Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin  ("wanted.in");
ofstream fout ("wanted.out");

const int MAX_N = 200;
int n;
struct oras{
    int x, y;
    inline bool operator < (const oras &rhs) const{
        return x < rhs.x;
    }
} o[MAX_N + 5];

long long sol;
static inline long long solve(int st, int dr, int start){

    cout<<st<<" "<<dr<<"\n";

    if(st == dr)
        return abs(start - o[st].x) + o[st].y;

    if(st+1 == dr){
        if(abs(start - o[st].x) + o[st].y < abs(start - o[dr].x) + o[dr].y) ///stalp-ul st este mai aproape
            return abs(start - o[st].x) + 2*o[st].y + abs(o[st].x - o[dr].x) + o[dr].y;
        else ///stalp-ul dr este mai aproape
            return abs(start - o[dr].x) + 2*o[dr].y + abs(o[st].x - o[dr].x) + o[st].y;
    }

    int md = ((st + dr) >> 1);

    if((dr-st+1) % 2 == 0){
        if(abs(start - o[md].x) + o[md].y > abs(start - o[md+1].x) + o[md+1].y)
            md++;
    }

    ///merg de la start la o[md]
    long long answer = abs(start - o[md].x);

    ///vizitez oras-ul o[md]
    answer += 2 * o[md].y;

    ///in functie de raspuns ma duc in stanga respectiv dreapta
    return answer + max(solve(st, md-1, o[md].x), solve(md+1, dr, o[md].x));
}

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);

    fin>>n;
    for(int i=1; i<=n; i++)
        fin>>o[i].x>>o[i].y;
    sort(o+1, o+n+1);

    fout<<solve(1, n, 0);
    return 0;
}
/**
4
-10 3
-5 2
5 4
8 2
**/