Submission #446377

# Submission time Handle Problem Language Result Execution time Memory
446377 2021-07-21T18:57:41 Z Bungmint Knapsack (NOI18_knapsack) C++17
Compilation error
0 ms 0 KB
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pair<int, int>>;
using pl = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pl>;
using ld = long double;

#define all(v) (v).begin(), (v).end()
#define ar array
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound

template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

void setIO(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;

struct Product{
	int v, w, k;
};

void solve()
{
	int s, n;
	cin >> s >> n;
	vi dp(s+1, -1), nxt(s+1, -1);
	vector<Product> a(n);
	for (int i=0;i<n;++i) cin >> a[i].v >> a[i].w >> a[i].k;
	dp[0] = 0;
	for (int i=1;i<=n;++i){
		Product p = a[i-1];
		for (int j=0;j<p.w;++j){
			stack<pi> m1, m2;
			for (int k=0;k*p.w+j<=s;++k){
				if (k>=p.k+1){
					if (m1.empty()) {
					    while (!m2.empty()) {
					        int element = m2.top().first;
					        m2.pop();
					        int minimum = m1.empty() ? element : max(element, m1.top().second);
					        m1.push({element, minimum});
					    }
					}
					int remove_element = m1.top().first;
					m1.pop();
				}
				nxt[j+k*p.w] = dp[j+k*p.w];
				int x = -INF;
				if (!m2.empty()&&!m1.empty()) x = max(m1.top().se, m2.top().se);
				else if (!m1.empty()) x = max(x, m1.top().se);
				else if (!m2.empty()) x = max(x, m2.top().se);
				if (x!=-INF) nxt[j+k*p.w] = max(nxt[j+k*p.w], x+k*p.v), dbg(x, j+k*p.w, i, k*p.v);
				
				if (dp[j+k*p.w]==-1) (m2.empty()? m2.push({-INF,-INF}):m2.push({-INF, m2.top().se}));
				else (m2.empty()? m2.push({dp[j+k*p.w]-k*p.v, dp[j+k*p.w]-k*p.v}):m2.push({dp[j+k*p.w]-k*p.v, max(dp[j+k*p.w]-k*p.v, m2.top().se)}));
			}
		}
		dbg(nxt);
		dp = nxt;
	}
	int res = 0;
	for (int i=0;i<=s;++i) res = max(res, dp[i]);
	cout << res << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

Compilation message

knapsack.cpp: In function 'void solve()':
knapsack.cpp:110:10: warning: unused variable 'remove_element' [-Wunused-variable]
  110 |      int remove_element = m1.top().first;
      |          ^~~~~~~~~~~~~~
knapsack.cpp:118:86: error: expected primary-expression before ';' token
  118 |     if (x!=-INF) nxt[j+k*p.w] = max(nxt[j+k*p.w], x+k*p.v), dbg(x, j+k*p.w, i, k*p.v);
      |                                                                                      ^
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~