牛客网暑期ACM多校训练营(第二场)A- run

链接:https://www.nowcoder.com/acm/contest/140/D
来源:牛客网
 

题目描述

White Cloud has built n stores numbered from 1 to n.
White Rabbit wants to visit these stores in the order from 1 to n.
The store numbered i has a price a[i] representing that White Rabbit can spend a[i] dollars to buy a product or sell a product to get a[i] dollars when it is in the i-th store.
The product is too heavy so that White Rabbit can only take one product at the same time.
White Rabbit wants to know the maximum profit after visiting all stores.
Also, White Rabbit wants to know the minimum number of transactions while geting the maximum profit.
Notice that White Rabbit has infinite money initially.

输入描述:

The first line contains an integer T(0<T<=5), denoting the number of test cases.
In each test case, there is one integer n(0<n<=100000) in the first line,denoting the number of stores.
For the next line, There are n integers in range [0,2147483648), denoting a[1..n].

输出描述:

For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.

示例1

输入

1
5
9 10 7 6 8

输出

3 4

题意:

在一条直线上,你一次只能移动1步或者K步,但是不能连续移动K步,

当他走L到R步之间的方案数,

思路:

直接上DP,开二维DP数组,dp[i][0]表示走到第i步,dp[i][1]表示跑到第i步。

dp[i][0]=dp[i-1][0]+dp[i-1][1];

dp[i][1]=dp[i-k][0]%mod;

然后记录前缀和即可求解

add[i]=add[i-1]+dp[i][0]+dp[i][1];

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 1e5+86;

const int mod = 1000000007;

long long dp[maxn][2];
long long add[maxn];

int main()
{
    int q,k;
    int l,r;
    long long ans;
    scanf("%d %d",&q,&k);
    dp[0][0]=1;
    for (int i = 1; i < maxn ; ++i)
    {
        dp[i][0]=(dp[i-1][0]+dp[i-1][1]);
        if(i>=k)
        {
            dp[i][1]=dp[i-k][0]%mod;
        }
        add[i]=add[i-1]+dp[i][0]+dp[i][1];
    }
    for (int i = 0; i < q; ++i)
    {
        scanf("%d %d",&l,&r);
        ans=(add[r]-add[l-1])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leper_gnome/article/details/81173458