結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー chineristACchineristAC
提出日時 2022-04-24 14:29:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,764 bytes
コンパイル時間 5,821 ms
コンパイル使用メモリ 222,696 KB
実行使用メモリ 56,636 KB
最終ジャッジ日時 2023-09-09 19:46:06
合計ジャッジ時間 24,900 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 WA -
testcase_29 AC 696 ms
12,444 KB
testcase_30 AC 704 ms
12,412 KB
testcase_31 AC 653 ms
12,564 KB
testcase_32 AC 478 ms
12,456 KB
testcase_33 AC 785 ms
12,480 KB
testcase_34 AC 684 ms
12,448 KB
testcase_35 AC 422 ms
10,384 KB
testcase_36 AC 592 ms
12,520 KB
testcase_37 AC 620 ms
12,408 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//TLE Θ(N^3)

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

//using mint = modint998244353;
#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define append push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<endl;
}

ll lpow(int n,int k){
  ll res = 1;
  ll e = n;
  while (k){
    if (k&1){
      res *= e;
    }
    e = e * e;
    k >>= 1;
  }
  return res;
}

ll dp[2501][2501];
pair<int,ll> deq[2501];
ll INF = 1e17;

int l = 0, r = 0;

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);


  int N;
  cin>>N;
  rep(i,N+1){
      rep(j,N+1){
          dp[i][j] = -INF;
      }
  }
  dp[0][0] = 0;
  vec<pair<int,ll>> item;
  rep(i,N){
      int c;
      ll v;
      cin>>c>>v;
      item.append({c,v});
  }

  rep(i,2501){
      deq[i] = {-1,-INF};
  }

  

  rep(i,N){
      auto [c,v] = item[i];
      for (int w_start=0;w_start<i+1;w_start++){
          for (int k_start=0;k_start<N+1;k_start++){
              for (int k=k_start;k<N+1;k++){
                  int w = w_start + (k-k_start) * (i+1);
                  if (N < w){
                      break;
                  }
                  if (l!=r && deq[l].first < k-c){
                      deq[l] = {-1,-INF};
                      l++;
                  }
                  ll tmp = dp[w][k] - k * v;
                  while (l!=r && deq[l].second <= tmp){
                      deq[r] = {-1,-INF};
                      r--;
                  }
                  deq[r] = {k,tmp};
                  r++;
                  dp[w][k] = v*k + deq[l].second;
              }
              for (int j=l;j<r;j++){
                  deq[j] = {-1,-INF};
              }
              l = 0,r = 0;
          }
      }

      for (int w_start=i+1;w_start<N+1;w_start++){
          for (int k_start=0;k_start<1;k_start++){
              for (int k=k_start;k<N+1;k++){
                  int w = w_start + (k-k_start) * (i+1);
                  if (N < w){
                      break;
                  }
                  if (l!=r && deq[l].first < k-c){
                      deq[l] = {-1,-INF};
                      l++;
                  }
                  ll tmp = dp[w][k] - k * v;
                  while (l!=r && deq[l].second <= tmp){
                      deq[r] = {-1,-INF};
                      r--;
                  }
                  deq[r] = {k,tmp};
                  r++;
                  dp[w][k] = v*k + deq[l].second;
              }
              for (int j=l;j<r;j++){
                  deq[j] = {-1,-INF};
              }
              l = 0,r = 0;
          }
      }

  }

  

  for (int k=1;k<N+1;k++){
      ll res = -INF;
      for (int w=0;w<=N;w++){
          chmax(res,dp[w][k]);
      }
      cout<<res<<endl;
  }


  
}
0