// #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,d,k;
set<int>s;
vector<int>dp;
vector<bool>vis;
vector<int>a,vals;
vector<int>adj,extra, prefix;
int curr_sum = 0;
int summation(int l, int r){
if(l>r){
return 0;
}
if(l==0){
return prefix[r];
}
return prefix[r] - prefix[l-1];
}
void build_adj(){
adj.resize(n), extra.resize(n);
vector<int>pref(n); pref[0] = 0;
for(int i = 1;i<n;i++) pref[i] = pref[i-1] + a[i-1];
for(int i = 1;i<n;i++){
auto it = upper_bound(pref.begin(),pref.end(),pref[i-1]+k);
adj[i-1] = it - pref.begin();
}
extra[0] = 0;
for(int i = 1;i<n;i++){
auto it = lower_bound(pref.begin(),pref.end(),pref[i]-k);
int idx = it - pref.begin();
for(int j = idx;j<i;j++){
extra[i] += vals[j];
}
}
adj[n-1] = n;
prefix.resize(n);
prefix[0] = vals[0];
for(int i = 1;i<n;i++) prefix[i] = prefix[i-1] + vals[i];
}
void dfs(int node){
if(adj[node]<0){
return;
}
if(node==n or (s.size()!=0 and vis[*s.begin()])){
if(s.size()==0){
return;
}
dp[*s.begin()] =extra[*s.begin()] + curr_sum;
s.clear(), curr_sum = 0;
return;
}
vis[node] = true;
curr_sum += summation(node,adj[node]-1);
s.insert(node);
if(s.size()==d){
int idx = *s.begin();
dp[idx] = extra[idx] + curr_sum;
curr_sum -= summation(idx,adj[idx]-1);
s.erase(s.begin());
}
dfs(adj[node]);
}
signed main(){
fastio; //judge();
cin >> n >> d >> k;
a.resize(n-1), vals.resize(n+1), vis.resize(n,false), dp.resize(n,0);
extra.resize(n);
for(auto &x : a) cin >> x;
for(auto &x : vals) cin >> x;
build_adj();
for(int i = 0;i<n;i++){
dfs(i);
}
int best = 0;
for(int i = 1;i<n;i++){
if(dp[i]>dp[best]){
best = i;
}
}
vector<int>answer;
while(d--){
if(best==n){
break;
}
answer.pb(best);
best = adj[best];
}
cout << answer.size() << endl;
for(auto x : answer){
cout << x + 1 << ' ';
}
}
컴파일 시 표준 에러 (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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |