結果

問題 No.3808 Welcome to Mid Difficulty Problems
コンテスト
ユーザー HoyHoyCharhang
提出日時 2026-11-20 22:26:30
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 247 ms / 2,000 ms
+ 774µs
コード長 2,587 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,181 ms
コンパイル使用メモリ 352,932 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2026-11-20 22:27:37
合計ジャッジ時間 8,356 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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

mt19937 mt(time(0));

int main() {
  int n, t;
  // n = 14, t = 30;
  cin >> n >> t;
  vector<int> a(n), b(n);
  rep(i,0,n) cin >> a[i] >> b[i];
  // uniform_int_distribution<int> ra(1, n), r(1, 5);
  // rep(i,0,n) {
  //   a[i] = ra(mt), b[i] = r(mt);
  // }
  // rep(i,0,n) cout << a[i] << " " << b[i] << endl;
  // function<void()> naive = [&]() {
  //   int ans = 0;
  //   rep(bit,0,1<<n) {
  //     int x = 0; ll val = 0;
  //     rep(i,0,n) {
  //       if ((bit>>i)&1) x |= (1<<a[i]), val += b[i];
  //     }
  //     if (val <= t) {
  //       if (__builtin_popcount(x)+__builtin_popcount(bit) == 18 && ans < 18) {
  //         cout << bitset<14>(bit) << endl;
  //         rep(i,0,n) if ((bit>>i)&1) {
  //           cout << a[i] << " " << b[i] << endl;
  //         }
  //       }
  //       chmax(ans, __builtin_popcount(x)+__builtin_popcount(bit));
  //     }
  //   }
  //   cout << ans << endl;
  // };
  // naive();
  vector<int> ord(n);
  iota(all(ord), 0);
  sort(all(ord), [&](int i, int j) {
    return tie(b[i], a[i]) < tie(b[j], a[j]);
  });
  int cnt = 0, val = 0;
  vector<int> x(n+1, 0);
  stack<int> stk;
  for (int i : ord) {
    // cout << a[i] << " " << b[i] << " -> ";
    if (val+b[i] <= t) {
      val += b[i];
      ++cnt;
      if (x[a[i]] == 0) {
        x[a[i]] = 1;
        ++cnt;
      } else {
        stk.emplace(b[i]);
      }
    } else {
      if (x[a[i]] == 0 && !stk.empty() && val-stk.top()+b[i] <= t) {
        val += b[i]-stk.top();
        stk.pop();
        x[a[i]] = 1;
        ++cnt;
      } else if (len(stk) >= 2 && x[a[i]] == 0) {
        int v = stk.top(); stk.pop();
        if (val-v-stk.top()+b[i] <= t) {
          val += b[i]-v-stk.top();
          x[a[i]] = 1;
          stk.pop();
        } else {
          stk.emplace(v);
        }
      }
    }
    // cout << val << " " << cnt << endl;
  }
  cout << cnt << endl;
  return 0;
}
0