Last active
September 2, 2016 01:34
-
-
Save rogerioagjr/d392630366306e94705da93f6d5a8872 to your computer and use it in GitHub Desktop.
Caverna de Ordinskaya - F2PJ - OBI 2016
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Caverna de Ordinskaya - F2PJ - OBI 2016 | |
// Rogério Júnior | |
// Complexidade: O(N) | |
#include "iostream" | |
#include "algorithm" // min e max | |
using namespace std; | |
int main(){ | |
// declaro as variávei que vou usar | |
long long n, m, resp=0, ant=0; | |
// leio os valores de N e M | |
cin >> n >> m; | |
// para cada medição feita | |
for(int i=0;i<n;i++){ | |
// leio o valor medido | |
long long a; | |
cin >> a; | |
// x é o maior valor possível e y o menor | |
long long x=max(a,m-a), y=min(a,m-a); | |
// se possível, uso y | |
if(y>=ant){ | |
resp+=y; // adiciono y ao comprimento total | |
ant=y; // e atualizo o valor de ant | |
} | |
// se não, uso x | |
if(y<ant and x>=ant){ | |
resp+=x; // adiciono x ao comprimento total | |
ant=x; // e atualizo o valor de ant | |
} | |
// se nem x for possível, é impossível | |
if(x<ant){ | |
resp=-1; // a respsota será -1 | |
break; // e dou um break no loop | |
} | |
} | |
// por fim, imprimo a resposta | |
cout << resp << "\n"; | |
// e retorno zero | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment