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
全部评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
11-08 00:11
复旦大学 深度学习 点赞 评论 收藏
分享
11-27 20:53
门头沟学院 C++ 拿到offer以后我就:先在年前完成大论文并且好好准备考公,年后提前到公司实习,偷项目丰富简历准备春招并继续考公,最后在几个ssp和公务员中幸福的纠结
点赞 评论 收藏
分享