M.Sayed
BAN USERstring answer(string s) {
bool flg = 0; // this determines if the first 1 from right is found.
for(int i = s.size() - 1; i >= 0; --i) {
if(flg) s[i] = s[i] == '0' ? '1' : '0';
else flg = s[i] == '1';
}
for(int i = 0; i < (s.size() / 2); ++i) {
swap(s[i], s[s.size() - i - 1]);
}
return s;
}
void swap(char &a, char &b) {
char tmp = a;
a = b;
b = tmp;
}
1 billion is a large number to store it in an array So i thought about solution and I found this one out Using Binary Search Tree.
While reading the input I will insert the current number(distance) to the tree.
when the size of the tree is larger than 1 million I will delete the largest one and insert the current element(only if the current element is smaller than the largest one in the tree) otherwise ignore this number.
Finally the tree has the the nearest 1 million stars and I can print them.
while(cin >> n ) {
if(NumberOfElementsInTheTree > int(1e6)) {
if(n > TheLargestElementInTheTree) continue;
else Delete(TheLargestElementInTheTree);
}
Insert(n);
}
- M.Sayed February 03, 2016