Submission #1144095

#TimeUsernameProblemLanguageResultExecution timeMemory
1144095agrim_09Solar Storm (NOI20_solarstorm)C++20
28 / 100
696 ms258108 KiB
// #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define endl '\n'; #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define py cout << "YES" << endl; #define pn cout << "NO" << endl; #define pb push_back #define int long long typedef long double lld; #define double lld typedef long long ll; typedef unsigned long long ull; const ll inf = 1e18; const ll mod = 1e9+7; const int N = 2e5; vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; // Check for queue, priorioty_queue, stack, ordered_set solutions // stack => LIFO (whatever goes in last comes out last) // queue => FIFO (whatever goes in first comes out first) // priority_queue => Dynamic queries of minimum/maximum // ordered_set => set in vector form //[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element] // To comment multiple lines : ctrl + / // To find and replace : ctrl+H void judge(){ srand(time(NULL)); #ifndef ONLINE_JUDGE freopen("1.txt","r",stdin); freopen("2.txt","w",stdout); freopen("Error.txt", "w", stderr); #define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl; #else #define debug(x...) #endif } void usaco(string s) { freopen((s + ".in").c_str(),"r",stdin); freopen((s + ".out").c_str(),"w",stdout); } int n,s,k; vector<int>a; vector<int>vals; vector<int>nxt,extra; vector<vector<int>>adj; vector<int>pref,dp; vector<int>par; int summation(int l, int r){ r = min(r,n-1); if(l>r){return 0;} if(l==0){return pref[r];} return pref[r] - pref[l-1]; } void input(){ cin >> n >> s >> k; a.resize(n); vals.resize(n); pref.resize(n); par.resize(n+1); par[n] = -1; a[0] = 0; for(int i = 1,diff;i<n;i++){ cin >> diff; a[i] = a[i-1] + diff; } for(auto &x : vals) cin >> x; pref[0] = vals[0]; for(int i = 1;i<n;i++) pref[i] = vals[i] + pref[i-1]; } void build_adj(){ nxt.resize(n+1); extra.resize(n); dp.resize(n); for(int i = 0;i<n;i++){ auto it = upper_bound(a.begin(),a.end(),a[i]+k); nxt[i] = it - a.begin() - 1; } for(int i = 0;i<n;i++){ auto it = lower_bound(a.begin(),a.end(),a[i]-k); extra[i] = it - a.begin(); } adj.resize(n+1); nxt[n] = n; for(int u = 0;u<n;u++){ int v = nxt[nxt[u]+1]; adj[u].pb(v); adj[v].pb(u); } } struct bin_node{ int vertex; // The other stuff like max bool operator==(const bin_node &b){ return vertex==b.vertex; } }; bin_node unite(bin_node a, bin_node b){ // Order is very imp bin_node ans; ans.vertex = b.vertex; return ans; } // Root is 0 class BinaryJump{ vector<vector<int>>up; vector<int>depth; int n; vector<int>par; private: vector<vector<int>>build(vector<int>&par){ int n = par.size(); int mx = log2(s) + 2; vector<vector<int>>up(n,vector<int>(mx)); //Base Case : for(int i = 0;i<mx;i++){ up[n-1][i] = -1; } for(int i = 0;i<n-1;i++){ up[i][0] = par[i]; } // Jumps for(int j = 1;j<mx;j++){ for(int i = 0;i<n;i++){ if(i==n-1){ continue; } int parent = up[i][j-1]; if(parent==-1){ up[i][j] = -1; } else{ up[i][j] = up[parent][j-1]; } } } return up; } int kth(int node, int k){ if(k>n){ return -1; } int ans = node; while(true){ if(k==0 or ans==-1){ return ans; } int p = log2(k); ans = up[ans][p]; k -= (1<<p); } } public: BinaryJump(vector<int>&parent){ par = parent; n = par.size(); up = build(par); } int kthAncestor(int node, int k){ return kth(node,k); } }; vector<int>v; void dfs(int node, int parent){ v.pb(node); if(node!=n){ int x = v.size(); int p = max(x-s,0LL); dp[node] = summation(extra[node],nxt[v[p]]); } for(auto child : adj[node]){ if(child==parent){ continue; } par[child] = node; dfs(child,node); } v.pop_back(); } void answer(){ par[n] = -1; BinaryJump binjump(par); for(int i = 0;i<n;i++){ dp[i] = summation(extra[i],nxt[binjump.kthAncestor(i,s-1)]); } int best = 0; for(int i = 1;i<n;i++){ if(dp[i]>dp[best]){ best = i; } } vector<int>q; while(s--){ if(best==n){ break; } q.pb(best); best = par[best]; } cout << q.size() << endl; for(auto x : q){ cout << x+1 << ' '; } } signed main(){ fastio; //judge(); input(); build_adj(); //dfs(n,n); answer(); }

Compilation message (stderr)

SolarStorm.cpp: In function 'void judge()':
SolarStorm.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("1.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("2.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("Error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp: In function 'void usaco(std::string)':
SolarStorm.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((s + ".out").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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...