求解求解,为什么最后3个测试点内存爆了....有没有好哥哥
查看原帖
求解求解,为什么最后3个测试点内存爆了....有没有好哥哥
150750
sakura、楼主2022/4/2 16:41

代码如下,数组也没有开大呀


/*
 * https://www.luogu.com.cn/problem/P5638
 * */
import java.util.*;
public class Main {
	static long n, k, res;
	static long[] sum = new long[1000001];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextLong();
		k = sc.nextLong();
		for (int i = 1; i < n; i++) {
			long num = sc.nextLong();
			sum[i] = sum[i - 1] + num;
		}
		if (k > n-1) {
			System.out.println(0);
			return;
		}//传送器的值大于目标城市,直接返回0
		for (int i = 0; i < n; i++) {
			res = Math.max(res, sum[(int) (i + k)] - sum[i]);
		}//得出可以节省的最大距离
		res = sum[(int)n-1] - res;//使用最后一位的和减去节省的最大值,即为答案
		System.out.println(res);
		sc.close();
	}
}	
2022/4/2 16:41
加载中...