Submission #1144090

#TimeUsernameProblemLanguageResultExecution timeMemory
1144090agrim_09Solar Storm (NOI20_solarstorm)C++20
100 / 100
377 ms195204 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);
    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); par.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);
    }
}

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(){
    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...