Browse Source

Beach bars exercise, new commons.

master
Nicolas Hafner 2 years ago
parent
commit
2be319b210
3 changed files with 44 additions and 34 deletions
  1. +14
    -29
      common.cpp
  2. BIN
      week2/beach_bars/beach_bars
  3. +30
    -5
      week2/beach_bars/beach_bars.cpp

+ 14
- 29
common.cpp View File

@ -15,42 +15,27 @@ typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Exact_predicates_exact_constructions_kernel K2;
typedef CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K3;
template<typename T> void parse(std::string string, T result[], size_t size){
std::stringstream ss(string);
std::string item;
for(size_t i=0; std::getline(ss, item, ' ') && i<size; ++i)
result[i] = (T)std::stol(item);
}
template<typename T> void read(T result[], size_t size){
std::string line;
std::getline(std::cin, line);
parse(line, result, size);
template<typename T> std::vector<T> read(size_t size){
std::vector<T> result(size);
for(size_t i=0; i<size; ++i){
std::cin >> result[i];
}
return result;
}
template<typename T> T read1(){
T num;
read(&num, 1);
std::cin >> num;
return num;
}
void parse_strings(std::string string, std::string result[], size_t size){
std::stringstream ss(string);
for(size_t i=0; std::getline(ss, result[i], ' ') && i<size; ++i);
}
void get_strings(std::string result[], size_t size){
std::string line;
std::getline(std::cin, line);
parse_strings(line, result, size);
}
std::string get_string(){
std::string line;
std::getline(std::cin, line);
return line;
template<typename T> void print(T array, size_t size){
if(0 < size){
std::cout << array[0];
for(size_t i=1; i<size; ++i)
std::cout << ' ' << array[i];
}
std::cout << '\n';
}
template<typename FT> double floor_to_double(const FT& x){

BIN
week2/beach_bars/beach_bars View File


+ 30
- 5
week2/beach_bars/beach_bars.cpp View File

@ -1,9 +1,34 @@
#include "../../common.cpp"
int burning_coins(int input[], int count){
return 0;
}
int main(){
return run_tests(parse_single_line, burning_coins);
setup();
int t; std::cin >> t;
for(; 0 < t; t--){
int n; std::cin >> n;
auto p = read<int>(n);
std::sort(p.begin(), p.end());
std::vector<int> best;
int maxnum = 0; int maxdist;
for(int i=0,j=0; j<n; ++j){
while(200 < p[j]-p[i])
++i;
int point = p[i]+(p[j]-p[i])/2;
int dist = std::max(point-p[i], p[j]-point);
if(maxnum < j-i || (maxnum == j-i && dist < maxdist)){
maxnum = j-i;
maxdist = dist;
best.clear();
}
if(maxnum == j-i && dist == maxdist){
best.push_back(point);
if(maxdist == std::max((point+1)-p[i], p[j]-(point+1)))
best.push_back(point+1);
}
}
std::cout << maxnum+1 << ' ' << maxdist << '\n';
print(best, best.size());
}
return 0;
}

Loading…
Cancel
Save