Submission #608191


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define loop for(;;)
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;
#define all(v) begin(v),end(v)
#define pb push_back
#define inf (1e9)
#define eps (1e-9)
using Integer = long long;
using Real = long double;
const Real PI = acosl(-1);
using P = pair<int, int>;

template<class S, class T> inline
ostream& operator<<(ostream&os, pair<S,T> p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template<class T, class U> inline
ostream& operator<<(ostream&os, tuple<T,U> t) {
  return os << '('
    << get<0>(t) << ", "
    << get<1>(t) << ')';
}

template<class S, class T, class U> inline
ostream& operator<<(ostream&os, tuple<S,T,U> t) {
  return os << '('
    << get<0>(t) << ", "
    << get<1>(t) << ", "
    << get<2>(t) << ')';
}

template<class T> inline
ostream& operator<<(ostream&os, set<T> v) {
  os << "(set";
  for (T item: v) os << ' ' << item;
  os << ")";
  return os;
}

template<class T> inline
ostream& operator<<(ostream&os, vector<T> v) {
  if (v.size() == 0) { return os << "(empty)"; }
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

template<class T> inline
istream& operator>>(istream&is, vector<T>&v) {
  rep (i, v.size()) is >> v[i];
  return is;
}

//           ^   >  v   <
int dx[] = { -1, 0, 1,  0 };
int dy[] = {  0, 1, 0, -1 };

using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vb = vector<bool>;
const string A = "Takahashi";
const string B = "Aoki";

Integer brute(int N, int C, vector<int>&vs, vector<int>&cs,
    int i=0,
    Integer vsum=0,
    Integer csum=0) {
  if (i >= N) {
    return vsum;
  }
  if (csum+cs[i]<=C) {
    Integer a = brute(N,C,vs,cs,i+1, vsum+vs[i], csum+cs[i]);
    Integer b = brute(N,C,vs,cs,i+1,vsum,csum);
    return max(a,b);
  } else {
    return brute(N,C,vs,cs,i+1,vsum,csum);
  }
}

constexpr int M = 200*1000+10;
Integer table[M];
int main() {

  int N, C; cin >> N >> C;
  vector<int> vs, cs;
  Integer vsum=0, csum=0;
  rep (i, N) {
    int v, c; cin >> v >> c;
    vsum += v;
    csum += c;
    vs.push_back(v);
    cs.push_back(c);
  }

  if (csum <= C) {
    cout << vsum << endl;
    return 0;
  }

  if (csum < M) { // DP respect to cost
    trace(2);
    rep(i, M) table[i] = - 2e9;
    table[0] = 0;
    for (int i = 0; i < N; ++i) {
      int c=cs[i];
      int v=vs[i];
      for (int k = C; k >= 0; --k) {
        if (c+k > C) continue;
        table[c+k] = max(table[c+k], table[k] + v);
      }
    }

    Integer ans = table[0];
    for (int k = 0; k <= C; ++k) {
      ans = max(ans, table[k]);
    }
    cout << ans << endl;
    return 0;
  }

  if (vsum < M) { // DP respect to value
    trace(3);
    rep(i,M) table[i] = 2e9;
    table[0] = 0;
    for (int i = 0; i < N; ++i) {
      int c=cs[i];
      int v=vs[i];
      for (int k = M; k >= 0; --k) {
        if (v+k >=M)continue;
        table[v+k] = min(table[v+k], table[k] + c);
      }
    }

    int ans = 0;
    rep (k,M){
      if (table[k] <= C) ans = max(ans, k);
    }
    cout << ans << endl;
    return 0;
  }

  if (N<=30) {
    cout << brute(N, C, vs, cs);
    cout << endl;
  }

  return 1;
}

Submission Info

Submission Time
Task D - ナップサック問題
User cympfh
Language C++11 (GCC 4.9.2)
Score 66
Code Size 3414 Byte
Status
Exec Time 122 ms
Memory 2460 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask00_sample_1.txt, subtask00_sample_2.txt, subtask00_sample_3.txt, subtask00_sample_4.txt
Subtask1 0 / 34 subtask01_0.txt, subtask01_1.txt, subtask01_10.txt, subtask01_11.txt, subtask01_12.txt, subtask01_13.txt, subtask01_14.txt, subtask01_2.txt, subtask01_3.txt, subtask01_4.txt, subtask01_5.txt, subtask01_6.txt, subtask01_7.txt, subtask01_8.txt, subtask01_9.txt, subtask00_sample_1.txt, subtask00_sample_2.txt, subtask00_sample_3.txt, subtask00_sample_4.txt
Subtask2 33 / 33 subtask02_0.txt, subtask02_1.txt, subtask02_10.txt, subtask02_11.txt, subtask02_12.txt, subtask02_13.txt, subtask02_14.txt, subtask02_2.txt, subtask02_3.txt, subtask02_4.txt, subtask02_5.txt, subtask02_6.txt, subtask02_7.txt, subtask02_8.txt, subtask02_9.txt, subtask00_sample_1.txt, subtask00_sample_3.txt
Subtask3 33 / 33 subtask03_0.txt, subtask03_1.txt, subtask03_10.txt, subtask03_11.txt, subtask03_2.txt, subtask03_3.txt, subtask03_4.txt, subtask03_5.txt, subtask03_6.txt, subtask03_7.txt, subtask03_8.txt, subtask03_9.txt, subtask00_sample_1.txt, subtask00_sample_4.txt
Case Name Status Exec Time Memory
subtask00_sample_1.txt 29 ms 2460 KB
subtask00_sample_2.txt 27 ms 732 KB
subtask00_sample_3.txt 27 ms 2336 KB
subtask00_sample_4.txt 34 ms 2268 KB
subtask01_0.txt 25 ms 804 KB
subtask01_1.txt 26 ms 800 KB
subtask01_10.txt 26 ms 920 KB
subtask01_11.txt 26 ms 796 KB
subtask01_12.txt 26 ms 812 KB
subtask01_13.txt 26 ms 796 KB
subtask01_14.txt 26 ms 924 KB
subtask01_2.txt 26 ms 804 KB
subtask01_3.txt 26 ms 800 KB
subtask01_4.txt 25 ms 920 KB
subtask01_5.txt 26 ms 728 KB
subtask01_6.txt 43 ms 2344 KB
subtask01_7.txt 26 ms 796 KB
subtask01_8.txt 26 ms 916 KB
subtask01_9.txt 26 ms 796 KB
subtask02_0.txt 81 ms 2348 KB
subtask02_1.txt 37 ms 2344 KB
subtask02_10.txt 38 ms 2264 KB
subtask02_11.txt 46 ms 2268 KB
subtask02_12.txt 51 ms 2324 KB
subtask02_13.txt 26 ms 920 KB
subtask02_14.txt 33 ms 2360 KB
subtask02_2.txt 70 ms 2456 KB
subtask02_3.txt 37 ms 2328 KB
subtask02_4.txt 71 ms 2456 KB
subtask02_5.txt 33 ms 2268 KB
subtask02_6.txt 61 ms 2460 KB
subtask02_7.txt 49 ms 2336 KB
subtask02_8.txt 38 ms 2344 KB
subtask02_9.txt 64 ms 2268 KB
subtask03_0.txt 119 ms 2456 KB
subtask03_1.txt 122 ms 2456 KB
subtask03_10.txt 120 ms 2340 KB
subtask03_11.txt 119 ms 2452 KB
subtask03_2.txt 120 ms 2456 KB
subtask03_3.txt 120 ms 2452 KB
subtask03_4.txt 120 ms 2268 KB
subtask03_5.txt 118 ms 2456 KB
subtask03_6.txt 120 ms 2460 KB
subtask03_7.txt 118 ms 2336 KB
subtask03_8.txt 119 ms 2336 KB
subtask03_9.txt 120 ms 2456 KB