-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircustower.cpp
95 lines (84 loc) · 2.32 KB
/
circustower.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// circustower.cpp
// xcode
//
// Created by Gokul Nadathur on 8/27/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <utility>
#include <vector>
#include <climits>
#include <iostream>
using namespace std;
typedef pair<int, int> HtWt;
bool fillLevel(int level, vector<HtWt>::iterator& it, HtWt& lastMax, vector<vector<HtWt>>& res, vector<HtWt>& inp)
{
int needed = level;
int htm = INT_MIN, wtm = INT_MIN;
while (it != inp.end() && needed) {
if (it->first >= lastMax.first && it->second >= lastMax.second) {
res[level].emplace_back(*it);
htm = max(htm, it->first);
wtm = max(wtm, it->second);
needed--;
++it;
} else {
break;
}
}
lastMax.first = htm;
lastMax.second = wtm;
return needed == 0;
}
vector<HtWt> longestTower(vector<HtWt>& inp)
{
HtWt m(INT_MAX, INT_MAX);
vector<vector<HtWt>> res(inp.size(), vector<HtWt>());
m = inp[0];
auto found = false;
for (auto& e : inp) {
if (e.first < m.first && e.second < m.second) {
found = true;
m = e;
}
}
if (!found) {
return {};
}
for (auto& e : inp) {
e.first -= m.first;
e.second -= m.second;
}
std::sort(inp.begin(), inp.end(),
[] (HtWt a, HtWt b) {
return (a.first * a.first + a.second * a.second <
b.first * b.first + b.second * b.second);
});
for (auto& e : inp) {
e.first += m.first;
e.second += m.second;
}
int level = 1;
HtWt lastMax(INT_MIN, INT_MIN);
auto it = inp.begin();
while (it != inp.end()) {
if (fillLevel(level, it, lastMax, res, inp) == false) {
break;
}
level++;
}
vector<HtWt> longest;
level = 1;
for (auto it = res.begin() + 1; it != res.end() && it->size() == level; ++it, ++level) {
longest.insert(longest.end(), it->begin(), it->end());
}
return longest;
}
int main(int argc, char** argv)
{
vector<HtWt> inp = { HtWt(65, 100), HtWt(70, 150), HtWt(56, 90), HtWt(75, 190), HtWt(60, 95), HtWt(68, 110) };
auto outp = longestTower(inp);
for (auto& o : outp) {
cout << o.first << "," << o.second << endl;
}
}