Created
September 20, 2016 05:43
-
-
Save jianminchen/59fa3914b92b71dce3f707c62fdf99ab to your computer and use it in GitHub Desktop.
HackerRank - Point Filtering study code #12
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 <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