Submission #852954

#TimeUsernameProblemLanguageResultExecution timeMemory
852954MickeyIsAwesomeKnapsack (NOI18_knapsack)C++14
100 / 100
55 ms5384 KiB
#include <bits/stdc++.h> // Include every standard library // Common file #include <ext/pb_ds/assoc_container.hpp> // Including tree_order_statistics_node_update #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef unsigned long long int ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pld; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<pll> vll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<ld> vd; typedef vector<vd> vvd; typedef pair<ll,pll> ppll; double EPS = 1e-9; int INF = 2000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 }; int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 }; #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a)) #define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (ll(a) = (b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (ll(a) = (b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a)) #define FOREACH(a, b) for (auto&(a) : (b)) #define rep(i, n) FOR(i, 0, n) #define repn(i, n) FORN(i, 1, n) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define RESET(a, b) memset(a, b, sizeof(a)) #define ff first #define ss second #define mp make_pair #define pb push_back #define pf push_front #define tiii tuple<int,int,int> #define mt make_tuple #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define TC(t) while (t--) #define MAX 10000100 // #define mo 998244353 #define mo 1000000007 #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> // Begin DEBUG // template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '{' << p.first << ", " << p.second << '}'; } template <class T, class = decay_t<decltype(*begin(declval<T>()))>, class = enable_if_t<!is_same<T, string>::value>> ostream &operator<<(ostream &os, const T &c) { os << '['; for (auto it = c.begin(); it != c.end(); ++it) os << &", "[2 * (it == c.begin())] << *it; return os << ']'; } //support up to 5 args #define _NTH_ARG(_1, _2, _3, _4, _5, _6, N, ...) N #define _FE_0(_CALL, ...) #define _FE_1(_CALL, x) _CALL(x) #define _FE_2(_CALL, x, ...) _CALL(x) _FE_1(_CALL, __VA_ARGS__) #define _FE_3(_CALL, x, ...) _CALL(x) _FE_2(_CALL, __VA_ARGS__) #define _FE_4(_CALL, x, ...) _CALL(x) _FE_3(_CALL, __VA_ARGS__) #define _FE_5(_CALL, x, ...) _CALL(x) _FE_4(_CALL, __VA_ARGS__) #define FOR_EACH_MACRO(MACRO, ...) \ _NTH_ARG(dummy, ##__VA_ARGS__, _FE_5, _FE_4, _FE_3, _FE_2, _FE_1, _FE_0) \ (MACRO, ##__VA_ARGS__) //Change output format here #define out(x) #x " = " << x << "; " #define debug(...) \ cout << "Line " << __LINE__ << ": " FOR_EACH_MACRO(out, __VA_ARGS__) << "\n" // End DEBUG // ll add(ll a, ll b, ll c = mo) {ll res=(a+b)%c; return res;} ll mod_neg(ll a, ll b, ll c = mo) {ll res=(a-b+c)%c; return res;} ll mul(ll a, ll b, ll c = mo) {ll res=(a*b)%c; res=(res+c)%c; return res;} struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); if (hash1 != hash2) { return hash1 ^ hash2; } // If hash1 == hash2, their XOR is zero. return hash1; } }; const int MOD = 1000000007; struct mi { int v; explicit operator int() const { return v; } mi() { v = 0; } mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; } }; mi &operator+=(mi &a, mi b) { if ((a.v += b.v) >= MOD) a.v -= MOD; return a; } mi &operator-=(mi &a, mi b) { if ((a.v -= b.v) < 0) a.v += MOD; return a; } mi operator+(mi a, mi b) { return a += b; } mi operator-(mi a, mi b) { return a -= b; } mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); } mi &operator*=(mi &a, mi b) { return a = a * b; } mi pow(mi a, long long p) { assert(p >= 0); return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1); } mi inv(mi a) { assert(a.v != 0); return pow(a, MOD - 2); } mi operator/(mi a, mi b) { return a * inv(b); } mi &operator/=(mi &a, mi b) { return a = a/b; } typedef vector<mi> vm; typedef vector<vm> vvm; typedef vector<vvm> vvvm; // Clear the stringstream // ss.str(""); // ss.clear(); // Clear the stringstream state // ss.str(str2); /////////////////////////////////////////// /* Main starts here -> Mukund Krishnatrey*/ /////////////////////////////////////////// bool compf(pll p1, pll p2) { if(p1.ff!=p2.ff) return p1.ff>p2.ff; else return p1.ss>p2.ss; } bool comps(pll p1, pll p2) { if(p1.ss!=p2.ss) return p1.ss<p2.ss; else return p1.ff<p2.ff; } bool comp_len(pll p1, pll p2) { if((p1.ss-p1.ff)!=(p2.ss-p2.ff)) return ((p1.ss-p1.ff)<(p2.ss-p2.ff)); else return (p1.ff<p2.ff); } bool comp_custom(pair<pll,ll> &p1,pair<pll,ll> &p2){ if(p1.ff.ff!=p2.ff.ff) return p1.ff.ff<p2.ff.ff; else return p1.ss<p2.ss; } struct scomps2 { bool operator()(const pll &p1, const pll &p2) const{ if(p1.ss!=p2.ss) return (p1.ss>p2.ss); else return (p1.ff<p2.ff); } }; struct scomps { bool operator()(const pll &p1, const pll &p2) const{ if(p1.ff!=p2.ff) return (p1.ff<p2.ff); else return (p1.ss<p2.ss); } }; struct scomp_cur { bool operator()(pair<pll,ll> p1,pair<pll,ll> p2) const{ if(p1.ff.ff!=p2.ff.ff) return p1.ff.ff<p2.ff.ff; else return p1.ss<p2.ss; } }; bool isValid(ll &n, ll &m, ll &r, ll &c){ if(r>=0 && r<n && c>=0 && c<m) return true; else return false; } ll dirn[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int query(unsigned int a, unsigned int b){ cout<<"? "<<a<<" "<<b<<endl; int k; cin>>k; return k; } void print_ans(unsigned int &a, unsigned int &b){ cout<<"! "<<a<<" "<<b<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (fopen("sort.in", "r")) { freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout); } cout<<fixed<<setprecision(10); // ll t_t; cin>>t_t; // while(t_t--){ ll s,n; cin>>s>>n; vl v(n+1); vl w(n+1); vl k(n+1); ll a,b,c; rep(i,n){ cin>>a>>b>>c; v[i]=a; w[i]=b; k[i]=c; } vector <pll> item[s+1]; rep(i,n){ item[w[i]].pb(mp(v[i],k[i])); } FORN(i,1,s) sort(item[i].begin(), item[i].end(), compf); vl best[s+1]; FORN(i,1,s) best[i].pb(0); ll cur,ct; FORN(i,1,s){ cur=0;ct=1; for(ll j=1;j<=(s/i);j+=1){ if(cur>=item[i].size()) break; best[i].pb(item[i][cur].ff); ct+=1; if(ct>item[i][cur].ss){ ct=1;cur+=1; } } } // FORN(i,1,s) debug(i,best[i]); ll rmw; vl dp(s+1,0); rep(i,s+1){ rep(p,best[i].size()){ rep(w,s+1){ rmw=w-i; if(rmw>=0) dp[rmw]=max(dp[rmw], dp[w]+best[i][p]); } } } ll ans=-INF; rep(i,s+1) ans=max(ans,dp[i]); cout<<ans<<endl; return 0; } // ** Read the questions properly dumass // ** Always assert for sanity checks if value should be in a certain range. // ** In getting n^p, power p shouldn't be reduced by mod. So when obtaining the value of p, don't use any mod operations. // ** Instead of -1, INT_MIN, etc. use INFF and -INFF if long long values are allowed. // Consider possible values if >INT_MAX ( or < INT_MIN) don't use it. // Take care that when erasing values from vector/array/related containers, that the iterator position is gone after that. And even in map/set/multiset, that iterator has to be replaced, can't do it-- or it++ after erase. // When comparing values, for eg:-, two vectors of equal size, if every value is equal, return false or return v[0]<v[1]. If you return true, then you get segmentation fault. // Multiset value erase will remove all instances of that value, better to use a map // Try to divide problem into subcases, and deal with them independently. // If lexicographical, then definitely don't do any swaps in your code, as it will change the order // Please read constraints carefully, you might end up overcomplicating the question if not. // Always verify your solution with atleast the given examples, and some extra edge cases you can think of, before you start coding it // Have a pseduo structure of code written down, (or in your mand), before writing the same // Write code in batches, keep testing every independent function, before writing next part // Check for long long, too many mods // Always take a look at the examples if stuck, or can't come with a solution for a while, normally examples contain a hint. // <------ For finding solution to adhoc problem ------> // ** Draw lots of small cases to gain a better understanding of the problem. If you're having trouble debugging, draw more cases. If you don't know how to start with a problem, draw more cases. Whenever you don't know how to further approach a problem, you're probably massing an important observation, so draw more cases and make observations about properties of the problem. // ** Whenever you find an observation that seems useful, write it down! Writing down ideas lets you easily come back to them later, and makes sure you don't forget about ideas that could potentially be the solution. // ** Don't get stuck on any specific idea, unless you see an entire solution. // ** Try to approach the problem from a lot of different perspectives. Try to mess around with formulas or draw a visual depiction of the problem. One of the most helpful things you can do when solving ad hoc problems is to keep trying ideas until you make progress. This is something you get better at as you do more problems.

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:250:5: note: in expansion of macro 'rep'
  250 |     rep(i,n){
      |     ^~~
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:254:5: note: in expansion of macro 'rep'
  254 |     rep(i,n){
      |     ^~~
knapsack.cpp:40:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   40 | #define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
      |                              ^
knapsack.cpp:257:5: note: in expansion of macro 'FORN'
  257 |     FORN(i,1,s) sort(item[i].begin(), item[i].end(), compf);
      |     ^~~~
knapsack.cpp:40:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   40 | #define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
      |                              ^
knapsack.cpp:259:5: note: in expansion of macro 'FORN'
  259 |     FORN(i,1,s) best[i].pb(0);
      |     ^~~~
knapsack.cpp:40:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   40 | #define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
      |                              ^
knapsack.cpp:261:5: note: in expansion of macro 'FORN'
  261 |     FORN(i,1,s){
      |     ^~~~
knapsack.cpp:264:19: 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]
  264 |             if(cur>=item[i].size()) break;
      |                ~~~^~~~~~~~~~~~~~~~
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:275:5: note: in expansion of macro 'rep'
  275 |     rep(i,s+1){
      |     ^~~
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'p' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:276:9: note: in expansion of macro 'rep'
  276 |         rep(p,best[i].size()){
      |         ^~~
knapsack.cpp:39:44: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                                        ~~~~^~~~~
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:276:9: note: in expansion of macro 'rep'
  276 |         rep(p,best[i].size()){
      |         ^~~
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'w' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:277:13: note: in expansion of macro 'rep'
  277 |             rep(w,s+1){
      |             ^~~
knapsack.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
knapsack.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
knapsack.cpp:284:5: note: in expansion of macro 'rep'
  284 |     rep(i,s+1) ans=max(ans,dp[i]);
      |     ^~~
knapsack.cpp:241:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  241 |         freopen("sort.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:242:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  242 |         freopen("sort.out", "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...