Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 20, 2016 05:43
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/59fa3914b92b71dce3f707c62fdf99ab to your computer and use it in GitHub Desktop.
Save jianminchen/59fa3914b92b71dce3f707c62fdf99ab to your computer and use it in GitHub Desktop.
HackerRank - Point Filtering study code #12
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
#include <tuple>
#include <iomanip>
using namespace std;
struct Item {
size_t id;
double x, y, z;
Item() : id(0), x(0), y(0), z(0) {}
Item(size_t id, double x, double y, double z) : id(id), x(x), y(y), z(z) {}
};
ostream& operator <<(ostream& os, const Item& item)
{ return os << item.id << " " << item.x << " " << item.y << " " << item.z; }
int main() {
string s;
size_t n, b;
getline(cin >> n >> b, s);
vector<Item> mainList;
for (size_t i = 0; i < n; ++i) {
size_t id;
double x, y, z;
getline(cin >> id >> x >> y >> z, s);
mainList.emplace_back(id, x, y, z);
}
map<size_t, Item> bucket;
sort(mainList.begin(), mainList.end(),
[](const Item& a, const Item&b) { return a.z < b.z; });
for (auto it = mainList.end()-b; it != mainList.end(); ++it)
bucket[it->id] = *it;
mainList.resize(mainList.size() - b);
string cmd;
size_t k;
while (cin >> cmd >> k) {
if (cmd == "F" || cmd == "f") {
if (bucket.count(k)) {
const Item& item = bucket[k];
cout << k << " = (" << fixed << setprecision(3) << item.x << "," << item.y << "," << item.z << ")" << endl;
}
else
cout << "Point doesn't exist in the bucket." << endl;
}
else if (cmd == "R" || cmd == "r") {
if (bucket.count(k)) {
if (mainList.size()) {
bucket.erase(k);
const Item& item = mainList.back();
bucket[item.id] = item;
mainList.resize(mainList.size() - 1);
cout << "Point id " << k << " removed." << endl;
}
else
cout << "No more points can be deleted." << endl;
}
else
cout << "Point doesn't exist in the bucket." << endl;
}
getline(cin, s);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment