Submission #393492

# Submission time Handle Problem Language Result Execution time Memory
393492 2021-04-23T15:35:48 Z usachevd0 Aliens (IOI16_aliens) C++17
4 / 100
3 ms 332 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


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);
    }
  }

  /*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]);
      dp[i][k] = cht::gt(y[i - 1]) + y[i - 1] * y[i - 1] + 2 * y[i - 1] + 1;
    }
  }*/

  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);
  }
  debug(ur);*/
  auto res = f(ur);
  assert(res.se <= K);
  return res.fi - ur * res.se + 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<double, int> cht::gt(ll)':
aliens.cpp:117:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     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 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Correct 1 ms 204 KB Correct answer: answer = 52
6 Correct 1 ms 204 KB Correct answer: answer = 210
7 Correct 1 ms 204 KB Correct answer: answer = 88
8 Correct 1 ms 204 KB Correct answer: answer = 7696
9 Correct 1 ms 204 KB Correct answer: answer = 1
10 Correct 1 ms 204 KB Correct answer: answer = 2374
11 Correct 1 ms 204 KB Correct answer: answer = 9502
12 Correct 1 ms 204 KB Correct answer: answer = 49
13 Correct 1 ms 204 KB Correct answer: answer = 151
14 Correct 1 ms 204 KB Correct answer: answer = 7550
15 Correct 1 ms 204 KB Correct answer: answer = 7220
16 Correct 1 ms 204 KB Correct answer: answer = 7550
17 Correct 1 ms 204 KB Correct answer: answer = 10000
18 Correct 1 ms 204 KB Correct answer: answer = 10000
19 Correct 1 ms 204 KB Correct answer: answer = 624
20 Correct 1 ms 204 KB Correct answer: answer = 10000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 1
2 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 1
4 Correct 1 ms 204 KB Correct answer: answer = 5
5 Correct 1 ms 204 KB Correct answer: answer = 41
6 Correct 1 ms 204 KB Correct answer: answer = 71923
7 Correct 2 ms 204 KB Correct answer: answer = 77137
8 Incorrect 3 ms 332 KB Wrong answer: output = 771, expected = 764
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Correct 1 ms 204 KB Correct answer: answer = 52
6 Correct 1 ms 204 KB Correct answer: answer = 210
7 Correct 1 ms 204 KB Correct answer: answer = 88
8 Correct 1 ms 204 KB Correct answer: answer = 7696
9 Correct 1 ms 204 KB Correct answer: answer = 1
10 Correct 1 ms 204 KB Correct answer: answer = 2374
11 Correct 1 ms 204 KB Correct answer: answer = 9502
12 Correct 1 ms 204 KB Correct answer: answer = 49
13 Correct 1 ms 204 KB Correct answer: answer = 151
14 Correct 1 ms 204 KB Correct answer: answer = 7550
15 Correct 1 ms 204 KB Correct answer: answer = 7220
16 Correct 1 ms 204 KB Correct answer: answer = 7550
17 Correct 1 ms 204 KB Correct answer: answer = 10000
18 Correct 1 ms 204 KB Correct answer: answer = 10000
19 Correct 1 ms 204 KB Correct answer: answer = 624
20 Correct 1 ms 204 KB Correct answer: answer = 10000
21 Correct 1 ms 204 KB Correct answer: answer = 1
22 Correct 1 ms 204 KB Correct answer: answer = 4
23 Correct 1 ms 204 KB Correct answer: answer = 1
24 Correct 1 ms 204 KB Correct answer: answer = 5
25 Correct 1 ms 204 KB Correct answer: answer = 41
26 Correct 1 ms 204 KB Correct answer: answer = 71923
27 Correct 2 ms 204 KB Correct answer: answer = 77137
28 Incorrect 3 ms 332 KB Wrong answer: output = 771, expected = 764
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Correct 1 ms 204 KB Correct answer: answer = 52
6 Correct 1 ms 204 KB Correct answer: answer = 210
7 Correct 1 ms 204 KB Correct answer: answer = 88
8 Correct 1 ms 204 KB Correct answer: answer = 7696
9 Correct 1 ms 204 KB Correct answer: answer = 1
10 Correct 1 ms 204 KB Correct answer: answer = 2374
11 Correct 1 ms 204 KB Correct answer: answer = 9502
12 Correct 1 ms 204 KB Correct answer: answer = 49
13 Correct 1 ms 204 KB Correct answer: answer = 151
14 Correct 1 ms 204 KB Correct answer: answer = 7550
15 Correct 1 ms 204 KB Correct answer: answer = 7220
16 Correct 1 ms 204 KB Correct answer: answer = 7550
17 Correct 1 ms 204 KB Correct answer: answer = 10000
18 Correct 1 ms 204 KB Correct answer: answer = 10000
19 Correct 1 ms 204 KB Correct answer: answer = 624
20 Correct 1 ms 204 KB Correct answer: answer = 10000
21 Correct 1 ms 204 KB Correct answer: answer = 1
22 Correct 1 ms 204 KB Correct answer: answer = 4
23 Correct 1 ms 204 KB Correct answer: answer = 1
24 Correct 1 ms 204 KB Correct answer: answer = 5
25 Correct 1 ms 204 KB Correct answer: answer = 41
26 Correct 1 ms 204 KB Correct answer: answer = 71923
27 Correct 2 ms 204 KB Correct answer: answer = 77137
28 Incorrect 3 ms 332 KB Wrong answer: output = 771, expected = 764
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Correct 1 ms 204 KB Correct answer: answer = 52
6 Correct 1 ms 204 KB Correct answer: answer = 210
7 Correct 1 ms 204 KB Correct answer: answer = 88
8 Correct 1 ms 204 KB Correct answer: answer = 7696
9 Correct 1 ms 204 KB Correct answer: answer = 1
10 Correct 1 ms 204 KB Correct answer: answer = 2374
11 Correct 1 ms 204 KB Correct answer: answer = 9502
12 Correct 1 ms 204 KB Correct answer: answer = 49
13 Correct 1 ms 204 KB Correct answer: answer = 151
14 Correct 1 ms 204 KB Correct answer: answer = 7550
15 Correct 1 ms 204 KB Correct answer: answer = 7220
16 Correct 1 ms 204 KB Correct answer: answer = 7550
17 Correct 1 ms 204 KB Correct answer: answer = 10000
18 Correct 1 ms 204 KB Correct answer: answer = 10000
19 Correct 1 ms 204 KB Correct answer: answer = 624
20 Correct 1 ms 204 KB Correct answer: answer = 10000
21 Correct 1 ms 204 KB Correct answer: answer = 1
22 Correct 1 ms 204 KB Correct answer: answer = 4
23 Correct 1 ms 204 KB Correct answer: answer = 1
24 Correct 1 ms 204 KB Correct answer: answer = 5
25 Correct 1 ms 204 KB Correct answer: answer = 41
26 Correct 1 ms 204 KB Correct answer: answer = 71923
27 Correct 2 ms 204 KB Correct answer: answer = 77137
28 Incorrect 3 ms 332 KB Wrong answer: output = 771, expected = 764
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 1 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 204 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Correct 1 ms 204 KB Correct answer: answer = 52
6 Correct 1 ms 204 KB Correct answer: answer = 210
7 Correct 1 ms 204 KB Correct answer: answer = 88
8 Correct 1 ms 204 KB Correct answer: answer = 7696
9 Correct 1 ms 204 KB Correct answer: answer = 1
10 Correct 1 ms 204 KB Correct answer: answer = 2374
11 Correct 1 ms 204 KB Correct answer: answer = 9502
12 Correct 1 ms 204 KB Correct answer: answer = 49
13 Correct 1 ms 204 KB Correct answer: answer = 151
14 Correct 1 ms 204 KB Correct answer: answer = 7550
15 Correct 1 ms 204 KB Correct answer: answer = 7220
16 Correct 1 ms 204 KB Correct answer: answer = 7550
17 Correct 1 ms 204 KB Correct answer: answer = 10000
18 Correct 1 ms 204 KB Correct answer: answer = 10000
19 Correct 1 ms 204 KB Correct answer: answer = 624
20 Correct 1 ms 204 KB Correct answer: answer = 10000
21 Correct 1 ms 204 KB Correct answer: answer = 1
22 Correct 1 ms 204 KB Correct answer: answer = 4
23 Correct 1 ms 204 KB Correct answer: answer = 1
24 Correct 1 ms 204 KB Correct answer: answer = 5
25 Correct 1 ms 204 KB Correct answer: answer = 41
26 Correct 1 ms 204 KB Correct answer: answer = 71923
27 Correct 2 ms 204 KB Correct answer: answer = 77137
28 Incorrect 3 ms 332 KB Wrong answer: output = 771, expected = 764
29 Halted 0 ms 0 KB -