Created
November 21, 2016 00:13
-
-
Save jianminchen/bae3b7c5c3326213d1ff800aef47ebc8 to your computer and use it in GitHub Desktop.
HackerRank - Minimum Cost - study code - C++ using Set -
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
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <set> | |
using namespace std; | |
int main() { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
int n; | |
cin >> n; | |
set < long long int > S; | |
long long int t = 5e18; | |
while(n--) | |
{ | |
long long int x; | |
cin >> x; | |
if(S.upper_bound(x) != S.end()) | |
t = min(t, *S.upper_bound(x) - x); | |
S.insert(x); | |
} | |
cout << t << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment