题目描述
The lectures are over, the assignments complete and even those pesky teaching assistants have nothing left to criticize about your coding project. Time to play some video games! As always, your procrastinating self has perfect timing: Cold Weather Entertainment just released Überwatch, a competitive first person video game!
Sadly, you aren’t very good at these kind of games. However, Überwatch offers more than just skill based gameplay. In Überwatch you can defeat all opponents in view with a single button press using your ultimate attack. The drawback of this attack is that it has to charge over time before it is ready to use. When it is fully charged you can use it at any time of your choosing.
After its use it immediately begins to charge again.
With this knowledge you quickly decide on a strategy:
• Hide from your opponents and wait for your ultimate attack to charge.
• Wait for the right moment.
• Defeat all opponents in view with your ultimate attack.
• Repeat.
After the game your teammates congratulate you on your substantial contribution. But you wonder: How many opponents could you have defeated with optimal timing?
The game is observed over n time slices. The ultimate attack is initially not charged and requires m time slices to charge. This first possible use of the ultimate attack is therefore in the (m+1)-th time slice. If the ultimate attack is used in the i-th time slice, it immediately begins charging again and is ready to be fired in the (i + m)-th time slice.
输入
The input consists of:
• one line with two integers n and m, where
– n (1 ≤ n ≤ 300 000) is the game duration;
– m (1 ≤ m ≤ 10) is the time needed to charge the ultimate attack in time slices.
• one line with n integers xi (0 ≤ xi ≤ 32) describing the number of opponents in view during a time slice in order.
输出
The input consists of:
• one line with two integers n and m, where
– n (1 ≤ n ≤ 300 000) is the game duration;
– m (1 ≤ m ≤ 10) is the time needed to charge the ultimate attack in time slices.
• one line with n integers xi (0 ≤ xi ≤ 32) describing the number of opponents in view during a time slice in order.
样例输入
4 2
1 1 1 1
样例输出
1
题目大意:给你两个时间,一个是总得时间,每一分钟时间下都有一些对手,但你可以通过按下按钮来击败他们,但你需要时间充电,在充电过程中不能按按钮,这个过程是循环的。问你最多能击败多少对手。
思路分析:
很明显这是一个dp问题,我们来分析一下
你不能在少于m时间内按按钮,所以当前击败的对手是前一次dp[i-1]转移过来的或者是第i-m个dp[i-m]加上当前这个a[i]
即为:dp[i-1]和dp[i-m]+a[i] 中的大的那个。
题目有点小坑,就是第一次的时候是m+1这个点开始按,以后就是过m时间就直接可以按了。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int a[350000];
int dp[350000];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int maxx=0;
memset(dp,0,sizeof(dp));
for(int i=m+1;i<=n;i++){
dp[i]=max(dp[i-1],dp[i-m]+a[i]);
}
printf("%d\n",dp[n]);
return 0;
}