Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 21, 2016 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/bae3b7c5c3326213d1ff800aef47ebc8 to your computer and use it in GitHub Desktop.
Save jianminchen/bae3b7c5c3326213d1ff800aef47ebc8 to your computer and use it in GitHub Desktop.
HackerRank - Minimum Cost - study code - C++ using Set -
#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