forked from aseembrahma/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaroFriends.cpp
155 lines (139 loc) · 4.19 KB
/
TaroFriends.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <sstream>
#include <queue>
#include <string>
#include <cmath>
#include <set>
#include <limits>
#include <numeric>
#include <bitset>
#define DEBUG 0
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
template <typename K, typename V>
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() ) {
return defval;
}
else {
return it->second;
}
}
template<typename T>
void print_vector(const vector<T> &v) {
cerr << " { ";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
cerr << *it << ", ";
}
cerr << " } " << endl;
}
class TaroFriends {
public:
int getNumber(vector <int> coordinates, int X) {
sort(coordinates.begin(), coordinates.end());
int low = coordinates[0];;
int high = coordinates[coordinates.size() - 1];
int key = low + ((high - low) / 2);
#if DEBUG
cout << "low is " << low << endl;
cout << "high is " << high << endl;
cout << "key is " << key << endl;
#endif
int elem_before_midpoint, elem_after_midpoint;
{
int lo = 0, hi = coordinates.size() - 1, mid;
while ((hi - lo) > 1) {
mid = (lo + hi) / 2;
if (coordinates[mid] < key) {
lo = mid;
}
else if (coordinates[mid] > key) {
hi = mid;
}
else {
// we have to make a choice here..i think it doesn't matter which
// one we go with
lo = mid - 1;
hi = mid;
break;
}
}
elem_before_midpoint = coordinates[lo];
elem_after_midpoint = coordinates[hi];
}
#if DEBUG
cout << "elem before midpoint " << elem_before_midpoint << endl;
cout << "elem after midpoint " << elem_after_midpoint << endl;
#endif
int elem_leftmost = min(coordinates[0] + X, elem_after_midpoint - X),
elem_rightmost = max(coordinates[coordinates.size() - 1] - X,
elem_before_midpoint + X);
#if DEBUG
cout << "elem leftmost " << elem_leftmost << endl;
cout << "elem rightmost " << elem_rightmost << endl;
#endif
return (elem_rightmost - elem_leftmost);
}
bool runTest(vector <int> coordinates, int X, int solution) {
int output = getNumber(coordinates, X);
if (output != solution)
{
cerr << "Failed for " << X << endl;
print_vector(coordinates);
cerr << "Expected: " << solution << endl;
cerr << "Found instead:" << output << endl;
}
}
};
int main(int argc, char* argv[]) {
TaroFriends test;
{
int coordinates[] = {4, 7, -7};
vector<int> coordinates_vec(coordinates,
coordinates + ARR_SIZE(coordinates));
test.runTest(coordinates_vec, 5, 4);
}
{
int coordinates[] = {-100000000, 100000000};
vector<int> coordinates_vec(coordinates,
coordinates + ARR_SIZE(coordinates));
test.runTest(coordinates_vec, 100000000, 0);
}
{
int coordinates[] = {3, 7, 4, 6, -10, 7, 10, 9, -5};
vector<int> coordinates_vec(coordinates,
coordinates + ARR_SIZE(coordinates));
test.runTest(coordinates_vec, 7, 7);
}
{
int coordinates[] = {-4, 0, 4, 0};
vector<int> coordinates_vec(coordinates,
coordinates + ARR_SIZE(coordinates));
test.runTest(coordinates_vec, 4, 4);
}
{
int coordinates[] = {7};
vector<int> coordinates_vec(coordinates,
coordinates + ARR_SIZE(coordinates));
test.runTest(coordinates_vec, 0, 0);
}
}