結果

問題 No.3761 Moonlit Battle
コンテスト
ユーザー HoyHoyCharhang
提出日時 2026-10-09 23:58:37
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 352 ms / 2,000 ms
+ 958µs
コード長 2,242 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,161 ms
コンパイル使用メモリ 355,308 KB
実行使用メモリ 35,524 KB
最終ジャッジ日時 2026-10-09 23:58:47
合計ジャッジ時間 7,653 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,g,n) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using P = pair<int,int>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}

void jikken(int n, int d, vector<ll> a, vector<ll> b) {
  ll ret = 1000000000;
  int mx = *max_element(all(b));
  rep(k,0,mx+1) {
    ll val = k;
    rep(i,0,n) val += 1LL*a[i]*dup(max(b[i]-k, 0LL), d);
    ret = min(ret, val);
  }
  cout << ret << endl;
}

int main() {
  int n; ll d;
  cin >> n >> d;
  map<ll,ll> mp;
  rep(i,0,n) {
    ll a, b;
    cin >> a >> b;
    mp[b] += a;
  }
  vector<ll> a, b;
  for (auto [v, cnt] : mp) {
    a.eb(cnt), b.eb(v);
  }
  n = len(a);
  // jikken(n, d, a, b);
  reverse(all(a)), reverse(all(b));
  // rep(i,0,n) cout << a[i] << " " << b[i] << endl;
  ll s = 0, ans = 0;
  int m = n;
  rep(i,0,n) {
    if (s+a[i] <= d) s += a[i];
    else {
      ans = b[i];
      rep(j,0,i) {
        b[j] -= b[i];
      }
      m = i;
      break;
    }
  }
  // cout << m << endl;
  if (m == 0) {
    cout << ans << endl;
    return 0;
  }
  // rep(i,0,m) {
  //   cout << a[i] << " " << b[i] << endl;
  // }
  vector<ll> vals = {0, d-1};
  rep(i,0,m) {
    vals.eb(b[i]%d);
  }
  sort(all(vals));
  vals.erase(unique(all(vals)), vals.end());
  int l = len(vals);
  vector<ll> imos(l);
  rep(i,0,m) {
    int idx = lower_bound(all(vals), b[i]%d)-vals.begin();
    imos[0] += a[i]*dup(b[i], d);
    if (idx != 0) imos[idx] -= a[i];
  }
  rep(i,0,l-1) imos[i+1] += imos[i];
  rep(i,0,l) vals[i] += imos[i];
  cout << ans+*min_element(all(vals)) << endl;
  // rep(k,0,d) {
  //   ll val = k+ans;
  //   rep(i,0,m) val += 1LL*a[i]*dup(max(b[i]-k, 0LL), d);
  //   cout << val << " ";
  // }
  // cout << endl;
  return 0;
}
0