cheater2k

POI XIII - Warehouse

Nov 18th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5 + 5;
  5. const long long inf = 9e18;
  6.  
  7. int n, x[N], y[N], t[N];
  8. long long ans[2][2], coor[2][2];
  9.  
  10. long long calc(int c, vector < pair<int,int> > &vec) {
  11.     long long ret = 0;
  12.     for (int i = 0; i < vec.size(); ++i) {
  13.         if (ret <= inf - 1LL * abs(c - vec[i].first) * vec[i].second)
  14.             ret += 1LL * abs(c - vec[i].first) * vec[i].second;
  15.         else return inf;
  16.     }
  17.     return ret;
  18. }
  19.  
  20. void solve(int type) { // [%2=0, %2=1]
  21.     vector < pair<int, int> > vec;
  22.     if (type == 0) { // X
  23.         for (int i = 1; i <= n; ++i) vec.push_back(make_pair(x[i], t[i]));
  24.     } else { // Y
  25.         for (int i = 1; i <= n; ++i) vec.push_back(make_pair(y[i], t[i]));
  26.     }
  27.     sort(vec.begin(), vec.end());
  28.  
  29.     long long L = 0, R = 0;
  30.     for (auto &it : vec) R += it.second;
  31.     vector<int> cand;
  32.     cand.push_back(vec[0].first);
  33.     for (int i = 0; i < vec.size(); ++i) {
  34.         L += vec[i].second; R -= vec[i].second;
  35.         if (R < L) {
  36.             cand.push_back(vec[i].first);
  37.             if (i > 0) cand.push_back(vec[i-1].first);
  38.             break;
  39.         }  
  40.     }
  41.  
  42.     for (int c : cand) {
  43.         for (int v = -1; v <= 1; ++v) {
  44.             long long res = calc(c + v, vec);
  45.             if (ans[type][(c + v) & 1] > res)
  46.                 ans[type][(c + v) & 1] = res, coor[type][(c + v) & 1] = c + v;
  47.         }
  48.     }
  49. }
  50.  
  51. int main() {
  52.     ios_base::sync_with_stdio(false); cin.tie(0);
  53.     cin >> n;
  54.     for (int i = 1; i <= n; ++i) {
  55.         int X, Y; cin >> X >> Y;
  56.         x[i] = X - Y;
  57.         y[i] = X + Y;
  58.         cin >> t[i];
  59.     }
  60.     for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) ans[i][j] = inf;
  61.     solve(0);
  62.     solve(1);
  63.  
  64.     long long mn = inf, ansx = 0, ansy = 0;
  65.     for (int i = 0; i < 2; ++i) {
  66.         if (mn > ans[0][i] + ans[1][i]) {
  67.             mn = ans[0][i] + ans[1][i];
  68.             ansx = coor[0][i];
  69.             ansy = coor[1][i];
  70.         }
  71.     }
  72.  
  73.     cout << (ansx + ansy) / 2 << ' ' << (ansy - ansx) / 2 << '\n';
  74. }
Advertisement
Add Comment
Please, Sign In to add comment