Cod sursa(job #1893625)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 25 februarie 2017 20:32:22
Problema Wanted Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

#define INFINITY 0x7FFFFFFF

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__       FORTIS FORTUNA ADIUVAT       __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

class parser{
    public:
        parser() {}
        parser(const char *file_name){
            input_file.open(file_name,ios::in | ios::binary);
            input_file.sync_with_stdio(false);
            index&=0;
            input_file.read(buffer,SIZE);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n&=0;
            sign&=0;
            sign|=(buffer[index-1]=='-');
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            n^=((n^-n)&-sign);
            return *this;}
        ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x400000; ///4MB
        char buffer[SIZE];
        int index,sign;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

parser f ("wanted.in");
ofstream t ("wanted.out");

struct coord{
    int x,y;
};

int n,st[210][210],dr[210][210];
vector <coord> a;

void update(int low,int up)
{
    st[low][up]=min((a[low].y<<1)+a[low+1].x-a[low].x+st[low+1][up],(a[up].y<<1)+a[up].x-a[low].x+a[up].x-a[up-1].x+dr[low][up-1]);
    dr[low][up]=min((a[up].y<<1)+a[up].x-a[up-1].x+dr[low][up-1],(a[low].y<<1)+a[up].x-a[low].x+a[low+1].x-a[low].x+st[low+1][up]);
    for (int i=1+low;i<up;++i) {
        st[low][up]=min(st[low][up],a[i].x-a[low].x+(a[i].y<<1)+max(a[i].x-a[i-1].x+dr[low][i-1],a[i+1].x-a[i].x+st[i+1][up]));
        dr[low][up]=min(dr[low][up],a[up].x-a[i].x+(a[i].y<<1)+max(a[i].x-a[i-1].x+dr[low][i-1],a[i+1].x-a[i].x+st[i+1][up]));
    }
}

int main()
{
    f>>n;
    a.resize(n);
    for (auto &i:a)
        f>>i.x>>i.y;
    sort(a.begin(),a.end(),[](coord a,coord b){return a.x<b.x;});
    for (int i=0;i<n;++i)
        st[i][i]=dr[i][i]=a[i].y;
    for (int i=1;i<n;++i)
        for (int j=0;j+i<n;++j)
            update(j,j+i);
    int answer=INFINITY;
    for (int i=0;i<n;++i)
        answer=min(answer,abs(a[i].x)+(a[i].y<<1)+max(a[i].x-a[i-1].x+dr[0][i-1],a[i+1].x-a[i].x+st[i+1][n-1]));
    t<<answer;
    return 0;
}