K for the Price of One (Hard Version)

This is the hard version of this problem. The only difference is the constraint on — the number of gifts in the offer. In this version: .

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer " of goods for the price of one" is held in store.

Using this offer, Vasya can buy exactly of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by — the number of coins it costs. Initially, Vasya has coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

Vasya can buy one good with the index if he currently has enough coins (i.e ). After buying this good, the number of Vasya’s coins will decrease by , (i.e it becomes ).
Vasya can buy a good with the index , and also choose exactly goods, the price of which does not exceed , if he currently has enough coins (i.e ). Thus, he buys all these goods, and his number of coins decreases by (i.e it becomes ).
Please note that each good can be bought no more than once.

For example, if the store now has goods worth , respectively, , and Vasya has coins, then he can buy goods. A good with the index will be bought by Vasya without using the offer and he will pay coins. Goods with the indices and Vasya will buy using the offer and he will pay coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

Input
The first line contains one integer () — the number of test cases in the test.

The next lines contain a description of test cases.

The first line of each test case contains three integers (, , ) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains integers () — the prices of goods.

It is guaranteed that the sum of for all test cases does not exceed .

Output
For each test case in a separate line print one integer — the maximum number of goods that Vasya can buy.
这一题由于英语理解的原因,我将它看成了简单的贪心算法改了许多遍,还是
WA经过指点,这一题用dp;式子为dp[i]=dp[i-k]+a[i];在k个商品之前,未满足条件买的商品最优价格为各商品的累计价格,k个商品(包括k)第i个商品满足活动条件的总价格等于第i-k+1~i个商品的价格加上i-k前商品的总价格

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int r[1000000],dp[1000000];
int main()
{
    int y;
    cin>>y;
    while(y--){
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&r[i]);
    }
    sort(r+1,r+n+1);
    dp[1]=r[1];int y=0;
    for(int i=1;i<=n;++i)
    {
        dp[i]=dp[i-1]+r[i];
        if(i>=k)
            dp[i]=dp[i-k]+r[i];
        if(dp[i]<=m)
            y=max(y,i);
        }
        printf("%d\n",y);
}
}
To price discrete-monitored barrier options in Python, we can use the Monte Carlo simulation method. Here are the steps to do so: 1. Define the parameters of the option, such as the strike price, barrier level, expiration date, and risk-free interest rate. 2. Generate a large number of random stock price paths using a geometric Brownian motion model. 3. For each stock price path, check if the barrier is breached at any of the monitoring dates. If the barrier is breached, the option expires worthless. Otherwise, the option pays off according to its payoff function (e.g. European call or put option). 4. Calculate the average payoff of all the simulated paths to obtain the option price. To compare the price of the discrete-monitored barrier option with the continuous-time version, we can repeat the above steps but with a different model for the stock price. Instead of using geometric Brownian motion, we can use a continuous-time model such as Black-Scholes. We can then compare the prices obtained from the two models. Here is an example Python code for pricing a discrete-monitored barrier option using Monte Carlo simulation: ```python import numpy as np from scipy.stats import norm # Option parameters s0 = 100 k = 110 b = 120 T = 1 r = 0.05 sigma = 0.2 n = 12 # Number of monitoring dates per year # Monte Carlo simulation parameters num_paths = 100000 num_steps = int(T * n) # Generate stock price paths dt = T / num_steps drift = (r - 0.5 * sigma**2) * dt volatility = sigma * np.sqrt(dt) z = np.random.normal(size=(num_paths, num_steps)) s = s0 * np.exp(np.cumsum(drift + volatility * z, axis=1)) # Check barrier breach at monitoring dates is_breached = np.any(s > b, axis=1) # Calculate option payoff payoff = np.maximum(0, s[:, -1] - k) * (1 - is_breached) # Discounted expected payoff discount_factor = np.exp(-r * T) option_price = discount_factor * np.mean(payoff) print("Discrete-monitored barrier option price:", option_price) ``` To price the continuous-time version using Black-Scholes, we can use the following code: ```python from scipy.stats import norm d1 = (np.log(s0/k) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T)) d2 = d1 - sigma * np.sqrt(T) n1 = norm.cdf(d1) n2 = norm.cdf(d2) call_price = s0 * n1 - k * np.exp(-r * T) * n2 put_price = k * np.exp(-r * T) * (1 - n2) - s0 * (1 - n1) print("Continuous-time Black-Scholes call price:", call_price) print("Continuous-time Black-Scholes put price:", put_price) ``` We can then compare the prices obtained from the two models to see how much difference the discrete monitoring makes.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值