Exile_Code @ 2023-11-11 17:34:47
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>
#define ull unsigned long long
#define ll long long
#define pii pair<ll ,ll>
#define ft first
#define sd second
#define pr pair
#define cy cout<<"Yes"<<endl
#define cn cout<<"No"<<endl
#define forn(i,n) for(int (i)=0;(i)<(n);(i)++)
#define forne(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define rforn(i,n) for(int (i)=(n-1);(i)>=(0);(i)--)
#define rforne(i,n) for(int (i)=(n);(i)>=(1);(i)--)
#define vv vector
const int inf = 0x004f4f4f;
const int N = 5e5 + 10;
struct edge {
int next, to, w;
};
vv<vv<pii>>tree;
vv<vv<edge>>e;
ll t;
vv<int>dmx = {1,1,-1,-1,2,2,-2,-2};
vv<int>dmy = {2,-2,2,-2,1,-1,1,-1};
vv<int>dx = {1,-1,0,0};
vv<int>dy = {0,0,-1,1};
void solve() {
int n, l, r; cin >> n >> l >> r;
vv<int>nums(n + 1);
forn(i, n+1)
cin >> nums[i];
deque<int>q;
vv<ll>f(n + 1);
forne(i, l-1)
f[i] = -inf;
ll mx = -inf;
for (int i = 0; i <= n;i++) {
while (i-r>=0&&q.size() && f[q.back()] <= f[i - l])
q.pop_back();
if(q.size()&&i - r > q.front())
q.pop_front();
if(i-l>=0)
q.push_back(i - l);
if(q.size())
f[i] = f[q.front()] + nums[i];
if (i + r > n)
mx = max(mx, f[i]);
}
cout << mx << endl;
}
int main() {
t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
by codejiahui @ 2023-11-11 17:40:36
@Exile_Code 同问
#include<iostream>
#include<deque>
using namespace std;
long long a[200010],f[200010];
struct Node{long long num,id;};
int main()
{
int n,l,r;
scanf("%d%d%d",&n,&l,&r);
for (int i = 0;i <= n;i++)
scanf("%lld",&a[i]);
for (int i = 0;i < l;i++)
f[i] = 0;
deque<Node> maxn;
for (int i = l;i <= n;i++)
{
if (!maxn.empty() && maxn.front().id < i - r)
maxn.pop_front();
Node t;
t.id = i;
t.num = f[i - l];
while(!maxn.empty() && maxn.back().num < t.num)
maxn.pop_back();
maxn.push_back(t);
f[i] = max(f[i],maxn.front().num + a[i]);
}
long long ans = -1e18;
for (int i = 1;i <= n;i++)
ans = max(ans,f[i]);
printf("%lld\n",ans);
return 0;
}
by codejiahui @ 2023-11-11 17:41:45
@Exile_Code 我是后面bug了,你是前面
by Exile_Code @ 2023-11-11 19:23:00
@codejiahui 我找到错误了,第一个while应该是i-l&&f[q.back()]<f[i-l],我写的是i-r,写错了
by Exile_Code @ 2023-11-11 19:37:45
给你改了一下,打注释的地方是修改的,现在可以过了
#include<iostream>
#include<deque>
using namespace std;
long long a[200010],f[200010];
struct Node{long long num,id;};
int main()
{
int n,l,r;
scanf("%d%d%d",&n,&l,&r);
for (int i = 0;i <= n;i++)
scanf("%lld",&a[i]);
for (int i = 1;i < l;i++)
f[i] = -9999999; //
deque<Node> maxn;
for (int i = l;i <= n;i++){
Node t;
t.id = i-l; //
t.num = f[i - l];
while(!maxn.empty() && maxn.back().num <= t.num)
maxn.pop_back();
if (!maxn.empty() && maxn.front().id < i - r)
maxn.pop_front();
maxn.push_back(t);
f[i] = maxn.front().num + a[i]; //
}
long long ans = -1e8;
for (int i = 1;i <= n;i++)
if(i+r>n) //
ans = max(ans,f[i]);
printf("%lld\n",ans);
return 0;
}
@codejiahui