Submission #607513


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

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

  int N,C; cin >> N >> C;

  if (false and C<M) { // 部分解答 2
    for (int i = 0; i <= C; ++i) {
      table[i] = - 1e9;
    }
    table[0] = 0;
    for (int i = 0; i < N; ++i) {
      int v, c;  cin >> v >> c;
      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;
  }

  { // 部分解答 2
    for (int i = 0; i < M; ++i) {
      table[i] = 1e9;
    }
    table[0] = 0;
    Integer V=0;
    for (int i = 0; i < N; ++i) {
      int v, c;  cin >> v >> c;
      V+=v;
      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;
    for (int k = 0; k <= V; ++k) {
      if (table[k] <= C) ans = max(ans, k);
    }
    cout << ans << endl;
    return 0;
  }

  return 0;
}

Submission Info

Submission Time
Task D - ナップサック問題
User cympfh
Language C++11 (GCC 4.9.2)
Score 0
Code Size 2769 Byte
Status
Exec Time 350 ms
Memory 2468 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 0 / 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 0 / 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 28 ms 2460 KB
subtask00_sample_2.txt 281 ms 2460 KB
subtask00_sample_3.txt 275 ms 2464 KB
subtask00_sample_4.txt 33 ms 2464 KB
subtask01_0.txt 280 ms 2460 KB
subtask01_1.txt 280 ms 2344 KB
subtask01_10.txt 281 ms 2460 KB
subtask01_11.txt 286 ms 2340 KB
subtask01_12.txt 286 ms 2340 KB
subtask01_13.txt 286 ms 2464 KB
subtask01_14.txt 286 ms 2468 KB
subtask01_2.txt 286 ms 2460 KB
subtask01_3.txt 285 ms 2336 KB
subtask01_4.txt 291 ms 2348 KB
subtask01_5.txt 300 ms 2340 KB
subtask01_6.txt 41 ms 2460 KB
subtask01_7.txt 299 ms 2336 KB
subtask01_8.txt 300 ms 2352 KB
subtask01_9.txt 293 ms 2332 KB
subtask02_0.txt 327 ms 2332 KB
subtask02_1.txt 338 ms 2340 KB
subtask02_10.txt 323 ms 2328 KB
subtask02_11.txt 326 ms 2456 KB
subtask02_12.txt 350 ms 2336 KB
subtask02_13.txt 320 ms 2460 KB
subtask02_14.txt 320 ms 2336 KB
subtask02_2.txt 316 ms 2332 KB
subtask02_3.txt 319 ms 2432 KB
subtask02_4.txt 319 ms 2332 KB
subtask02_5.txt 318 ms 2340 KB
subtask02_6.txt 317 ms 2460 KB
subtask02_7.txt 316 ms 2336 KB
subtask02_8.txt 320 ms 2340 KB
subtask02_9.txt 320 ms 2336 KB
subtask03_0.txt 119 ms 2344 KB
subtask03_1.txt 118 ms 2336 KB
subtask03_10.txt 119 ms 2336 KB
subtask03_11.txt 119 ms 2456 KB
subtask03_2.txt 118 ms 2336 KB
subtask03_3.txt 119 ms 2328 KB
subtask03_4.txt 118 ms 2332 KB
subtask03_5.txt 118 ms 2464 KB
subtask03_6.txt 118 ms 2460 KB
subtask03_7.txt 118 ms 2344 KB
subtask03_8.txt 116 ms 2344 KB
subtask03_9.txt 118 ms 2344 KB