CSU1833-Lab-简单DP

本文介绍了一个实验调度的问题,即如何在考虑到实验后的恢复周期的前提下,安排实验以最大化老板视察的概率总和。通过动态规划的方法,给出了具体的算法实现,并提供了C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
**********************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值