結果

問題 No.2546 Many Arithmetic Sequences
ユーザー MisukiMisuki
提出日時 2023-12-31 19:45:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 3,707 bytes
コンパイル時間 3,007 ms
コンパイル使用メモリ 226,948 KB
実行使用メモリ 21,380 KB
最終ジャッジ日時 2023-12-31 19:45:39
合計ジャッジ時間 8,249 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 55 ms
10,376 KB
testcase_04 AC 117 ms
13,716 KB
testcase_05 AC 91 ms
10,952 KB
testcase_06 AC 20 ms
6,676 KB
testcase_07 AC 44 ms
7,048 KB
testcase_08 AC 14 ms
6,676 KB
testcase_09 AC 46 ms
9,064 KB
testcase_10 AC 85 ms
14,440 KB
testcase_11 AC 93 ms
16,364 KB
testcase_12 AC 38 ms
7,692 KB
testcase_13 AC 75 ms
11,600 KB
testcase_14 AC 80 ms
14,488 KB
testcase_15 AC 24 ms
6,676 KB
testcase_16 AC 76 ms
14,480 KB
testcase_17 AC 107 ms
13,272 KB
testcase_18 AC 58 ms
9,536 KB
testcase_19 AC 67 ms
10,328 KB
testcase_20 AC 41 ms
8,160 KB
testcase_21 AC 50 ms
6,752 KB
testcase_22 AC 83 ms
11,432 KB
testcase_23 AC 156 ms
21,380 KB
testcase_24 AC 158 ms
21,380 KB
testcase_25 AC 161 ms
21,380 KB
testcase_26 AC 159 ms
21,380 KB
testcase_27 AC 159 ms
21,380 KB
testcase_28 AC 145 ms
21,380 KB
testcase_29 AC 146 ms
21,380 KB
testcase_30 AC 146 ms
21,380 KB
testcase_31 AC 147 ms
21,380 KB
testcase_32 AC 147 ms
21,380 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 127 ms
21,380 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 107 ms
21,380 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}

using frac = array<int, 2>;
bool operator<=(frac a, frac b) {
  return a[0] * b[1] <= a[1] * b[0];
}

frac inter(frac a, frac b) {
  if (a[0] > b[0])
    return {b[1] - a[1], a[0] - b[0]};
  else if (a[0] < b[0])
    return {a[1] - b[1], b[0] - a[0]};
  else
    assert(false);
}

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, m; cin >> n >> m;
  vector<int> a(n), d(n);
  for(int i = 0; i < n; i++)
    cin >> a[i] >> d[i];

  if (R::min(d) >= 0) {
    int ans = LLONG_MIN;
    for(int i = 0; i < n; i++)
      ans = max(ans, m * a[i] + m * (m - 1) / 2 * d[i]);
    cout << ans << '\n';
    return 0;
  }

  vector<int> s(1, 0);
  {
    priority_queue<array<int, 2>> pq;
    vector<int> k(n);
    for(int i = 0; i < n; i++) {
      if (d[i] < 0) {
        pq.push({a[i], i});
        k[i] = 1;
      }
    }
    for(int i = 0; i < m; i++) {
      auto [val, id] = pq.top(); pq.pop();
      s.emplace_back(val);
      pq.push({a[id] + k[id] * d[id], id});
      k[id]++;
    }
    partial_sum(s.begin(), s.end(), s.begin());
  }

  if (R::max(d) < 0) {
    cout << s[m] << '\n';
    return 0;
  }

  vector<array<int, 2>> l;
  for(int i = 0; i < n; i++)
    if (d[i] >= 0)
      l.push_back({d[i], 2 * a[i] - d[i]});
  R::sort(l);

  deque<array<int, 2>> cht;
  for(int i = 1; i <= ssize(l); i++) {
    if (i == ssize(l) or l[i - 1][0] != l[i][0]) {
      while(ssize(cht) >= 2 and inter(l[i - 1], end(cht)[-2]) <= inter(end(cht)[-2], end(cht)[-1]))
        cht.pop_back();
      cht.push_back(l[i - 1]);
    }
  }

  int ans = LLONG_MIN;
  for(int k = 0; k <= m; k++) {
    //while(ssize(cht) >= 2 and inter(cht[0], cht[1]) <= frac{k, 1})
    while(ssize(cht) >= 2 and inter(cht[0], cht[1])[0] <= inter(cht[0], cht[1])[1] * k)
      cht.pop_front();
    int eval = k * cht.front()[0] + cht.front()[1];
    ans = max(ans, eval * k / 2 + s[m - k]);
  }

  cout << ans << '\n';

  return 0;
}
0