mirror of
https://github.com/mx42/codingame.git
synced 2026-01-14 05:39:51 +01:00
Adding medium/hard puzzles & challenges...
This commit is contained in:
61
puzzles/medium/stock-exchange-losses.cpp
Normal file
61
puzzles/medium/stock-exchange-losses.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// https://www.codingame.com/ide/puzzle/stock-exchange-losses
|
||||
// Xavier Morel - 2016-03-16
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Auto-generated code below aims at helping you parse
|
||||
* the standard input according to the problem statement.
|
||||
**/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n; cin.ignore();
|
||||
vector<int> history;
|
||||
int last_high = 0;
|
||||
int last_low = 0;
|
||||
int prev = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int v;
|
||||
cin >> v; cin.ignore();
|
||||
|
||||
if (i == 0) {
|
||||
last_high = v;
|
||||
last_low = v;
|
||||
continue;
|
||||
} else {
|
||||
if (v > last_high) {
|
||||
if (last_high != last_low) {
|
||||
history.push_back(last_high);
|
||||
history.push_back(last_low);
|
||||
}
|
||||
last_high = v;
|
||||
last_low = v;
|
||||
} else if (v < last_low) {
|
||||
last_low = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (last_high != last_low) {
|
||||
history.push_back(last_high);
|
||||
history.push_back(last_low);
|
||||
}
|
||||
|
||||
int max_loss = 0;
|
||||
for (vector<int>::iterator it = history.begin(); it != history.end(); it++) {
|
||||
int loss = *min_element(it, history.end()) - *it;
|
||||
if (loss < max_loss) {
|
||||
max_loss = loss;
|
||||
}
|
||||
}
|
||||
|
||||
cout << max_loss << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user