Cod sursa(job #2971047)

Utilizator Super_VictorVictor Caciulan Super_Victor Data 26 ianuarie 2023 12:48:37
Problema Inundatii Scor 0
Compilator cpp-64 Status done
Runda sa_fac_schema Marime 1.52 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;
int mat[50005][5];
int main(){
    ifstream cin("inundatii.in");
    ofstream cout("inundatii.out");
    int n, s=0, aux, ok=0;
    cin>>n>>mat[1][1]>>mat[1][2]>>mat[1][3];
    for(int i=2; i<=n; i++)
    {
        cin>>mat[i][1]>>mat[i][2]>>mat[i][3];
        if(mat[i][1]<=mat[i-1][1])
        {
            s+=(mat[i-1][1]-mat[i][1]+1);
            aux=mat[i][1];
            mat[i][1]=(mat[i-1][1]+mat[i][1])/2+1;
            mat[i-1][1]=(mat[i-1][1]+aux)/2;
        }
        if(mat[i][2]<=mat[i-1][2])
        {
            s+=(mat[i-1][2]-mat[i][2]+1);
            aux=mat[i][2];
            mat[i][2]=(mat[i-1][2]+mat[i][2])/2+1;
            mat[i-1][2]=(mat[i-1][2]+aux)/2;
        }
        if(mat[i][3]<=mat[i-1][3])
        {
            s+=(mat[i-1][3]-mat[i][3]+1);
            aux=mat[i][3];
            mat[i][3]=(mat[i-1][3]+mat[i][3])/2+1;
            mat[i-1][3]=(mat[i-1][3]+aux)/2;
        }
        int j=i-1;
        while(j>=2)
        {
            ok=0;
            if(mat[j][1]<=mat[j-1][1])
            {
                s+=mat[j-1][1]-mat[j][1]+1;
                ok=1;
            }
            if(mat[j][2]<=mat[j-1][2])
            {
                s+=mat[j-1][2]-mat[j][2]+1;
                ok=1;
            }
            if(mat[j][3]<=mat[j-1][3])
            {
                s+=mat[j-1][3]-mat[j][3]+1;
                ok=1;
            }
            if(ok==0) break;
            j--;
        }
    }
    cout<<s;
    return 0;
}