X: Lab
Description
Susan, your friend, is working in a biology lab. She is working in a very strict lab and she wants her boss to see her at work in the lab. However, doing experiments is very draining, so for every day she spends in the lab she needs at least two days away to recover, e.g. if she runs her experiment on Monday, the next soonest day for her next experiment is Thursday of the same week. Based on the visiting history, given n days, she knows the probability, P = {p1, p2, …, pn}, where pi is the probability that her boss will visit the lab on the ith day. She asks you to help her find the best schedule that she should perform her experiments to maximize the total sum of the probability based on the given history. Susan can perform her experiment on as many days as she wishes so long as she rests in between.
Input
The first line of input contains a single number, T, the number of test case. 0 < T <= 20. For each test case, it has two lines. The first line is a single number, n, the number of days you have the history. The second line contains n numbers, the probability of her advisor visiting on each day from day 1 to day n, 0 < n <= 1000. For each projection, the number is between 0 and 1. The probability values are double value and separated by a single space.
Output
For each test case, output a single number the maximum probability that can be gained based on the given projection. The number should be rounded to the nearest one decimal point. For the case of x.x5, we accept all methods of rounding.
Sample Input
2
5
0.2 0.11 0.5 1.0 0.0
9
0.0 0.2 0.12 0.4 0.01 0.9 0.9 0.8 1.0
Sample Output
1.2
2.1
裸的DP,转移方程为dp[i]=max{dp[i-1],dp[i-2],dp[i-3]+val[i]}
然后优化为dp[i]=max(dp[i-1],dp[i-3]+val[i]),可以思考一下为什么可以这样做
#include <bits/stdc++.h>
#define N 101000
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
double dp[N];
int main()
{
ios::sync_with_stdio(false);
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i=0;i<n;++i){
cin>>dp[i];
}
dp[1]=max(dp[0],dp[1]);
dp[2]=max(dp[2],dp[1]);
for(int i=3;i<n;++i){
dp[i]=max(dp[i-3]+dp[i],dp[i-1]);
}
cout<<fixed<<setprecision(1)<<dp[n-1]<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1833
User: CSUzick
Language: C++
Result: AC
Time:4 ms
Memory:2476 kb
**********************************************************************/