Cod sursa(job #3232722)

Utilizator Ana_22Ana Petcu Ana_22 Data 1 iunie 2024 10:23:33
Problema Inundatii Scor 0
Compilator cpp-64 Status done
Runda Simulare E4 #1 Marime 0.94 kb
#include <iostream>
#include <fstream>
#define NMAX 50001

using namespace std;

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

struct str {
    int x, y, z;
} c[NMAX];

int absv( int a ) {
    return a > 0 ? a : 0;
}

int dist( str a, str b ) {
    return absv( a.x - b.x + 1 ) + absv( a.y - b.y + 1 ) + absv( a.z - b.z + 1 );
}

int main() {
    int n;
    long long s = 0;
    fin >> n;
    for( int i = 1; i <= n; i++ ) {
        fin >> c[i].x >> c[i].y >> c[i].z;
    }
    for( int i = 2; i <= n; i++ ) {
        if( c[i].x > c[i-1].x && c[i].y > c[i-1].y && c[i].z > c[i-1].z );
        else {
            int d = dist( c[i-1], c[i] );
            s += d;
            if( dist( { c[i-1].x + 1, c[i-1].y + 1, c[i-1].z + 1 }, c[i+1] ) < dist( c[i], c[i+1] ) ) {
                c[i].x = c[i-1].x + 1;
                c[i].y = c[i-1].y + 1;
                c[i].z = c[i-1].z + 1;
            }
        }
    }
    fout << s;
    return 0;
}