제출 #496043

#제출 시각아이디문제언어결과실행 시간메모리
496043SinsAriesKnapsack (NOI18_knapsack)C++17
100 / 100
121 ms4988 KiB
#include<bits/stdc++.h>

using namespace std;

// type
using ll = long long;
using ld = long double;
using db = double;
using str = string;

// pairs
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

// vector
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define sor(x) sort(all(x))
#define rsz resize
#define rtn return

// binary search
#define lb lower_bound
#define ub upper_bound
template<class T> ll lwb(vector<T>& a, const T& b) { rtn lb(all(a), b) - a.begin(); }
template<class T> ll upb(vector<T>& a, const T& b) { rtn ub(all(a), b) - a.begin(); }

// pairs
#define mp make_pair
#define f first
#define s second

// loops
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define each(a,x) for (auto& a: x)
#define cont continue
#define bre break

// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s) {
    setIn(s+".inp"); setOut(s+".out");
}

const int MOD =  1e9 + 7; //998244353;
const int MX = 2e5+5;
const ll INF = 1e15;
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

// modulo, exponentiation, inverse
ll pow_mod(ll a, ll b, ll m) { ll res = 1; while(b){ res = res * (b & 1 ? a : 1) % m; a = a * a % m; b >>= 1; } rtn res; }
ll inv(ll a, ll m) { rtn pow_mod(a, m - 2, m); }

ll nxt(void){ // fast way for cin operation
    ll x; cin >> x;
    rtn x;
}

#define inf (0x3f3f3f)

ll W, N, value, weight, num, dp[2][2001];

map<ll, vpl> by_weight;

void init(void){
    cin >> W >> N;
    F0R(i, N){
        cin >> value >> weight >> num;
        by_weight[weight].pb({value, num});
    }
    memset(dp, -inf, sizeof dp);
    dp[0][0] = 0;
}

void solve(void){
    for(auto& [w, items] : by_weight){
        sort(all(items), greater<pl>());

        F0R(i, W + 1){
            dp[1][i] = dp[0][i];
            ll copies = 1;
            ll profit = 0;
            ll typeAt = 0;
            ll curUsed = 0;

            while(copies * w <= i && typeAt < sz(items)){
                profit += items[typeAt].f;

                if(dp[0][i - copies * w] != -inf)
                    dp[1][i] = max(dp[1][i], dp[0][i - copies * w] + profit);

                copies++;
                curUsed++;
                if(curUsed == items[typeAt].s){
                    curUsed = 0;
                    typeAt++;
                }
            }
        }

        swap(dp[0], dp[1]);
    }

    cout << *max_element(dp[0], dp[0] + W + 1);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
//    setIO("CodeforcesEasy");

    init();
    solve();

    rtn 0;
}

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'void solve()':
knapsack.cpp:103:45: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |             while(copies * w <= i && typeAt < sz(items)){
      |                                             ^
knapsack.cpp: In function 'void setIn(str)':
knapsack.cpp:54:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void setOut(str)':
knapsack.cpp:55:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...