Cod sursa(job #1773814)

Utilizator mihai.alphamihai craciun mihai.alpha Data 8 octombrie 2016 11:30:58
Problema Orase Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#define BUF_SIZE 1<<17
char buf[BUF_SIZE];
int pos = BUF_SIZE;
FILE *fin, *fout;
inline char nextch(){
    if(pos==BUF_SIZE) fread(buf, BUF_SIZE, 1, fin), pos=0;
    return buf[pos++];
}

inline int read(){
    int x=0;
    char ch=nextch();
    while(!isdigit(ch)) ch=nextch();
    while(isdigit(ch)){
        x=10*x+ch-'0';
        ch=nextch();
    }
    return x;
}
 #define MAX 50000

using namespace std;
struct object {
int d;
int l;
};
object v[MAX];
bool cmp(object A, object B)  {
return A.d < B.d;
}
int main()  {
    fin = fopen("orase.in", "r");
    fout = fopen("orase.out", "w");
    int m, n;
    int dmax = -1, distc, i1, i2;
    m=read();
    n=read();
    int i, maxl, maxd, md = -1, ml =-1;
    for(i = 0;i < n;i++)  {
        v[i].d=read();
        if(v[i].d>maxd)
        maxd = v[i].d, md = i;
        v[i].l=read();
    }
    int a, b;
    a = 0;
    b = 0;
    for(i = 0;i < n;i++)  {
        if(v[i].d+v[i].l>a)
            a = v[i].d+v[i].l;
        if(maxd-v[i].d+v[i].l>b)
            b = maxd-v[i].d+v[i].l;
    }
    fprintf(fout, "%d", a + b - maxd);
//    sort(v, v+n, cmp);
//    distc = v[1].l + v[0].l + v[1].d - v[0].d;
//    i1 = 0;
//    i2 = 1;
//    for(i = 0;i < n;i++)
//        printf("%d %d\n", v[i].d,v[i].l);
//int distcurent, i1curent, i2curent, i1max, i2max;
//    for(i = 2;i < n;i++)  {
//        i1curent = i-1;
//        i2curent = i;
//        if(distc + v[i].l - v[i2].l + v[i].d-v[i2].d > v[i].l + v[i - 1].l + v[i].d-v[i-1].d)  {
//
//            distcurent = distc + v[i].l - v[i2].l + v[i].d-v[i2].d;
//            i1curent = i1, i2curent = i;
//        }
//            if(distcurent > dmax)  {
//                dmax = distcurent;
//                i1max = i1curent;
//                i2max = i2curent;
//            }
//        distc = distcurent;
//        i1 = i1curent;
//        i2 = i2curent;
//    }
//    fprintf(fout, "%d %d %d", dmax, i1max, i2max);
//    printf("\n\n\n%d", v[i2max].l+v[i1max].l+v[i2max].d-v[i1max].d);
    fclose(fin);
    fclose(fout);
    return 0;
}