KouMoSir @ 2023-12-23 14:03:53
下方是WA代码
#include<iostream>
#include<deque>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 10, M = -0x7ffffffffffffff;
ll n, l, r, ans, a[N], f[N], pre[N];
deque<vector<ll>> que;
int main() {
ans = -0x7fffffff;
cin >> n >> l >> r;
for (int i = 0; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n + l; i++)pre[i] = M;
for (int i = 0; i <= n + l; i++) {
if (i > n) {
ans = max(ans, pre[i]);
continue;
}
while (!que.empty() && que.front()[1] < i + l - r)que.pop_front();
while (!que.empty() && que.back()[0] <= pre[i])que.pop_back();
vector<ll> tmp{ pre[i],i };
que.push_back(tmp);
pre[i + l] = que.front()[0] + a[i + l];
//cout << i << " ";
//for (auto j : que)cout << "{" << j[0] << " " << j[1] << "} ";
//cout << '\n';
}
cout << ans << '\n';
}
WA了sub8,9
修改为
const ll N=2e5+10;
const ll N=4e5+10;
就能够ac了。
只是我觉得这个问题应该是数组下标访问越界的原因,为什么是WA而不是RE呢?
by immix @ 2023-12-23 14:09:01
因为下标越界是未定义行为,程序可以做出任何行为
by emo_male_god @ 2023-12-23 14:09:58
@KouMoSir 有时候如果你数组开的大小只比使用的大小小一点点,就可能WA或者TLE,差很多就会RE,还要注意,可能一个数组开小了,但是按照地址的计算,用到了其他数组
by HxDlBbCo877 @ 2024-01-17 20:53:03
因为数组是申请了一块连续的内存,越界的时候访问的是未定义的内存块。然后系统会认为这是和数组一样类型的东西,把它解释成了奇奇怪怪的值,就wa了
by LLLgoyour @ 2024-01-29 08:22:51
看完题解用java写了一遍PriorityQueue解法:
import java.io.*;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static final int[] f = new int[200010];
static final int[] a = new int[200010];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int l = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= n + 1; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
int ans = 0;
PriorityQueue<Integer> q1 = new PriorityQueue<>((A, B) -> (B - A));
PriorityQueue<Integer> q2 = new PriorityQueue<>((A, B) -> (B - A));
for (int i = 1; i <= l; i++) {
q2.add(a[i]);
}
for (int i = l; i <= n; i++) {
q1.add(f[i - l]);
if (i - r - 1 >= l) {
q2.add(f[i - r - 1]);
}
while (!q2.isEmpty() && q1.peek().equals(q2.peek())) {
q1.poll();
q2.poll();
}
f[i] = q1.peek() + a[i];
}
for (int i = n - r + 1; i <= n; i++) {
ans = Math.max(ans, f[i]);
}
System.out.println(ans);
}
}
然后90分。。case #1,#2 (subtask)和#5没过,难道是同样的问题?