int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
if (arr.length == 1){
System.out.println(arr[0]);
continue;
}
int k = in.nextInt();
int[] dp = new int[arr.length];
Deque<Integer> deque = new LinkedList<>();
for (int i = 0; i < arr.length; i++) {
if (!deque.isEmpty() && i - deque.peekFirst() > k) {
deque.pollFirst();
}
dp[i] = (deque.isEmpty() ? 0 : dp[deque.peekFirst()]) + arr[i];
while (!deque.isEmpty() && dp[i] >= dp[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
}
System.out.println(dp[dp.length-1]);
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
if (arr.length == 1){
System.out.println(arr[0]);
continue;
}
int k = in.nextInt();
int[] dp = new int[arr.length];
Deque<Integer> deque = new LinkedList<>();
for (int i = 0; i < arr.length; i++) {
if (!deque.isEmpty() && i - deque.peekFirst() > k) {
deque.pollFirst();
}
dp[i] = (deque.isEmpty() ? 0 : dp[deque.peekFirst()]) + arr[i];
while (!deque.isEmpty() && dp[i] >= dp[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
}
System.out.println(dp[dp.length-1]);
点赞 6 评论 2
全部评论
相关推荐
10-14 17:38
门头沟学院 软件测试 点赞 评论 收藏
分享