Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 21, 2016 00:33
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/9098f545f0b750fe1cb4acd6a2c0b1b9 to your computer and use it in GitHub Desktop.
Save jianminchen/9098f545f0b750fe1cb4acd6a2c0b1b9 to your computer and use it in GitHub Desktop.
Minimum Cost - HackerRank - woman codesprint#2 - study C++ set class
#include <bits/stdc++.h>
using namespace std;
#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair
#define mt make_tuple
typedef pair< int, int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;
int const N = 200005;
int n;
ll a[N];
int main() {
scanf("%d", &n);
for(int i = 0;i < n;i++) {
scanf("%lld", &a[i]);
}
set< ll > q;
set< ll > ::iterator fnd;
ll res = inf64;
for(int i = n - 1;i >= 0;i--) {
fnd = q.lower_bound(a[i]);
if(fnd != q.begin()) {
fnd--;
res = min(res, a[i] - *fnd);
}
q.insert(a[i]);
}
printf("%lld\n", res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment