dtj

crab_align_2.cc

dtj
Dec 7th, 2021 (edited)
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <vector>
  6. #include <valarray>
  7. #include <map>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.     string line;
  14.     getline(cin, line);
  15.     transform(line.begin(), line.end(), line.begin(), [](char ch) {return ch == ',' ? ' ' : ch;});
  16.     istringstream ss(line);
  17.     vector<int> crab_hpos;
  18.     copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(crab_hpos));
  19.  
  20.     valarray<int> orig(&crab_hpos[0], crab_hpos.size());
  21.     int c_begin = orig.min(), c_end = orig.max();
  22.     map<int, int> ranked;
  23.     // It's the triangle number, now.  For diff of 4, it's 10.
  24.     for (int p = c_begin; p <= c_end; ++p) {
  25.         int diff = (orig - p).apply(abs).apply([] (int n) {return (n * (n + 1)) / 2;}).sum();
  26.         ranked[diff] = p;
  27.     }
  28.     cout << "Ranked choices:" << endl;
  29.     for (auto const & rc : ranked) {
  30.         cout << rc.first << ": " << rc.second << endl;
  31.     }
  32.     cout
  33.         << "Winning position: " << ranked.begin()->second
  34.         << " using " << ranked.begin()->first << " fuel" << endl;
  35. }
  36.  
Add Comment
Please, Sign In to add comment