Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 20, 2016 05:17
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/652c78955e9999e3ab5759fb9358f69e to your computer and use it in GitHub Desktop.
Save jianminchen/652c78955e9999e3ab5759fb9358f69e to your computer and use it in GitHub Desktop.
HackerRank - Point Filtering - study code #11
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> II;
struct Point {
int k;
double x, y, z;
Point() {};
Point(int k, double x, double y, double z): k(k), x(x), y(y), z(z) {};
bool operator < (const Point &P) const {
return z < P.z;
}
bool operator > (const Point &P) const {
return z > P.z;
}
};
int main() {
#ifdef LOCAL
freopen("Data.inp", "r", stdin);
freopen("Data.out", "w", stdout);
#endif
int n, b; cin >> n >> b;
set<Point> S;
priority_queue<Point> pq;
map<int, Point> Z;
for (int i = 1; i <= n; ++i) {
int k; double x, y, z; cin >> k >> x >> y >> z;
pq.push(Point(k, x, y, z));
Z[k] = Point(k, x, y, z);
}
while (S.size() < b) {
S.insert(pq.top());
pq.pop();
}
string st;
while (cin >> st) {
int k; cin >> k;
if (toupper(st[0]) == 'F') {
if (S.count(Z[k]))
cout << k << " = (" << fixed << setprecision(3) << Z[k].x << "," << Z[k].y << "," << Z[k].z << ")\n";
else
cout << "Point doesn't exist in the bucket.\n";
}
else {
if (S.count(Z[k])) {
if (pq.size()) {
S.erase(Z[k]);
cout << "Point id " << k << " removed." << "\n";
S.insert(pq.top()); pq.pop();
}
else cout << "No more points can be deleted.\n";
}
else
cout << "Point doesn't exist in the bucket.\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment