Cod sursa(job #1726600)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 8 iulie 2016 14:33:28
Problema Operatii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <math.h>

using namespace std;
#define ll long long
#define llu long long unsigned
#define pb push_back
#define mp make_pair

string problemName = "operatii";
string inFile = problemName+".in";
string outFile = problemName+".out";
ifstream fin(inFile.c_str());
ofstream fout(outFile.c_str());

int main(){
    int n,i,mn,x,mx,y,ds;
    llu sol;
    char type;
    fin>>n;
    mn = 1e9;
    mx = 0;
    sol = 0;
    ds = 0;
    y = 0;
    for(i = 1;i <= n;i++){
        fin>>x;
        if(y == 0){
            y = x;
        }
        if(x == 0){
            sol += mx;
            mn = 1e9;
            mx = 0;
            ds = 0;
            type = 'n';
            continue;
        }else{
            x -= ds;
            if(x > y){
                if(type == 'a'){
                    mn = min(x, mn);
                    mx = max(x, mx);
                }else if(type == 'd'){
                    ds += mn;
                    sol += mx;
                    x -= ds;
                    mn = x;
                    mx = x;
                    type = 'n';
                }else{
                    type = 'a';
                    mn = min(x, mn);
                    mx = max(x, mx);
                }
            }else if(x < y){
                if(type == 'd'){
                    mn = min(x, mn);
                    mx = max(x, mx);
                }else if(type == 'a'){
                    ds += mn;
                    sol += mx;
                    x -= ds;
                    mn = x;
                    mx = x;
                    type = 'n';
                }else{
                    type = 'd';
                    mn = min(x, mn);
                    mx = max(x, mx);
                }
            }else{
                mn = min(x, mn);
                mx = max(x, mx);
            }
        }
        y = x;
    }
    sol += mx;
    fout<<sol;
    return 0;
}