Submission #393494

# Submission time Handle Problem Language Result Execution time Memory
393494 2021-04-23T15:43:11 Z usachevd0 Aliens (IOI16_aliens) C++17
0 / 100
1 ms 204 KB
#include <bits/stdc++.h>
#ifndef DEBUG
  #include "aliens.h"
#endif

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
  return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
  return y > x ? (x = y, true) : false;
}
void debug_out() {
  cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
  cerr << ' ' << A;
  debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
  for (int i = 0; i < n; ++i) {
    cerr << a[i] << ' ';
  }
  cerr << endl;
}
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
  for (auto& x : v) {
    stream << x << ' ';
  }
  return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
  return stream << p.first << ' ' << p.second;
}

#ifdef DEBUG
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
  #define debug(...) 1337
  #define mdebug(a, n) 1337
#endif

#define double ld

const ll INF64 = 1e18;
const int maxN = 100005;

ll squared(ll a) {
  return a * a;
}

struct pt {
  int x, y;

  pt(int _x = 0, int _y = 0): x(_x), y(_y) {}

  pt operator + (const pt& p) const { return pt(x + p.x, y + p.y); }
  pt operator - (const pt& p) const { return pt(x - p.x, y - p.y); }
};
ostream& operator << (ostream& stream, const pt& p) {
  return stream << p.x << ' ' << p.y;
}

struct line {
  double k, b;
  int info;
  
  line() {}
  line(double _k, double _b, int _info): k(_k), b(_b), info(_info) {}
  
  double y(double x) {
    return k * x + b;
  }
};
double intersect_x(const line& a, const line& b) {
  return -(a.b - b.b) / (double)(a.k - b.k);
}

namespace cht {
  vector<line> ln;
  vector<double> xl;
  int ptr;

  void init() {
    ln.clear();
    xl.clear();
    ptr = 0;
  }

  void add_line(double k, double b, int info) {
    line L(k, b, info);
    while (!ln.empty()) {
      double x = intersect_x(ln.back(), L);
      if (x <= xl.back()) {
        ln.pop_back();
        xl.pop_back();
      } else {
        break;
      }
    }
    xl.push_back(ln.empty() ? -INF64 : intersect_x(ln.back(), L));
    ln.push_back(L);
  }

  pair<double, int> gt(ll x) {
    while (ptr + 1 < ln.size() && xl[ptr + 1] <= x) {
      ++ptr;
    }
    return {ln[ptr].y(x), ln[ptr].info};
  }
}

double dp[maxN];
int cnt[maxN];

ll take_photos(int n, int _m, int K, vector<int> _r, vector<int> _c) {
  vector<pt> points(n);
  for (int i = 0; i < n; ++i) {
    points[i] = {_r[i], _c[i]};
    if (points[i].x > points[i].y) {
      swap(points[i].x, points[i].y);
    }
  }
  sort(all(points), [&](const pt& P, const pt& Q) -> bool {
    if (P.x != Q.x) return P.x < Q.x;
    return P.y > Q.y;
  });
  vector<pt> a;
  for (auto P : points) {
    if (!a.empty() && P.y <= a.back().y) continue;
    while (!a.empty() && P.x == a.back().x) {
      a.pop_back();
    }
    a.push_back(P);
  }
  n = a.size();
//  for (auto p : a) debug(p);
  vector<ll> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    x[i] = a[i].x;
    y[i] = a[i].y;
  }

  vector<ll> I(n, 0);
  for (int i = 1; i < n; ++i) {
    if (x[i] > y[i - 1]) {
      I[i] = 0;
    } else {
      I[i] = squared(y[i - 1] - x[i] + 1);
    }
  }

  /*{
    ++K;
    vector<vector<ll>> dp(n + 1, vector<ll>(K + 1, INF64));
    fill(all(dp[0]), 0);
    for (int k = 1; k <= K; ++k) {
      cht::init();
      for (int i = 1; i <= n; ++i) {
        cht::add_line(-2 * x[i - 1], dp[i - 1][k - 1] + x[i - 1] * x[i - 1] - 2 * x[i - 1] - I[i - 1], -1);
        dp[i][k] = cht::gt(y[i - 1]).fi + y[i - 1] * y[i - 1] + 2 * y[i - 1] + 1;
      }
    }
    for (int k = 1; k <= K; ++k) {
      debug(k, dp[n][k]);
    }
    --K;
  }*/

  auto f = [&](double lambda) -> pair<double, int> {
    fill(dp, dp + n + 1, INF64);
    fill(cnt, cnt + n + 1, 0);
    dp[0] = 0;
    cht::init();
    for (int i = 1; i <= n; ++i) {
      cht::add_line(-2 * x[i - 1], dp[i - 1] + x[i - 1] * x[i - 1] - 2 * x[i - 1] - I[i - 1], cnt[i - 1]);
      auto res = cht::gt(y[i - 1]);
      dp[i] = res.fi + y[i - 1] * y[i - 1] + 2 * y[i - 1] + 1 + lambda;      
      cnt[i] = res.se + 1;
    }
    return {dp[n], cnt[n]};
  };

  double ul = -1;
  double ur = 1e12 + 1337;
  for (int itr = 0; itr < 120; ++itr) {
    double um = (ul + ur) * 0.5;
    if (f(um).se <= K) {
      ur = um;
    } else {
      ul = um;
    }
  }
  /*for (int u = 0; u <= 6; ++u) {
    auto res = f(u);
    debug(u, res.se, res.fi - u * res.se);
  }*/
//  cerr << ul << ' ' << ur << endl;
//  debug(ur);
//  debug(f(ur - 0.00001).se);
  auto res = f(ur);
//  debug(res.se, K);
  assert(res.se <= K);
//  mdebug(dp, n + 1);
//  mdebug(cnt, n + 1);
  return res.fi - ur * K + 0.5;
}

#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> r(n), c(n);
  for (int i = 0; i < n; ++i) cin >> r[i] >> c[i];
  cout << take_photos(n, m, k, r, c) << '\n';

  return 0;
}
#endif

Compilation message

aliens.cpp: In function 'std::pair<long double, int> cht::gt(ll)':
aliens.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |     while (ptr + 1 < ln.size() && xl[ptr + 1] <= x) {
      |            ~~~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Incorrect 1 ms 204 KB Wrong answer: output = 5, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 1
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Incorrect 1 ms 204 KB Wrong answer: output = 2, expected = 1
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Incorrect 1 ms 204 KB Wrong answer: output = 5, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Incorrect 1 ms 204 KB Wrong answer: output = 5, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Incorrect 1 ms 204 KB Wrong answer: output = 5, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Incorrect 1 ms 204 KB Wrong answer: output = 5, expected = 4
3 Halted 0 ms 0 KB -