H - Hotel Rewards(优先队列)

You are planning to spend your holidays touring Europe, staying each night in a different city for N
consecutive nights. You have already chosen the hotel you want to stay in for each city, so you know
the price Pi of the room you’ll be staying at during the i-th night of your holidays, for i = 1, . . . , N.
You will book your accommodation through a website that has a very convenient rewards program,
which works as follows. After staying for a night in a hotel you booked through this website you are
awarded one point, and at any time you can exchange K of these points in your account for a free night
in any hotel (which will however not give you another point).
For example, consider the case with N = 6 and K = 2 where the prices for the rooms are P1 = 10,
P2 = 3, P3 = 12, P4 = 15, P5 = 12 and P6 = 18. After paying for the first four nights you would have
four points in your account, which you could exchange to stay for free the remaining two nights, paying
a total of P1 + P2 + P3 + P4 = 40 for your accommodation. However, if after the first three nights
you use two of the three points you earned to stay the fourth night for free, then you can pay for the
fifth night and use the final two points to get the sixth one for free. In this case, the total cost of your
accommodation is P1 + P2 + P3 + P5 = 37, so this option is actually more convenient.
You want to make a program to find out what the minimum possible cost for your holidays’ accommodation
is. You can safely assume that all hotels you want to stay always will have a room available
for you, and that the order of the cities you are going to visit cannot be altered.
Input
The input file contains several test cases, each of them as described below.
The first line of input contains two integers N and K, representing the total number of nights your
holidays will last, and the number of points you need in order to get a free night (1 ≤ N, K ≤ 105
). The
second line contains N integers P1, P2, . . . , PN , representing the price of the rooms you will be staying
at during your holidays (1 ≤ Pi ≤ 104
for i = 1, 2, . . . , N).
Output
For each test case, output a line with one integer representing the minimum cost of your accommodation
for all of your holidays.
Sample Input
6 2
10 3 12 15 12 18
6 1
10 3 12 15 12 18
5 5
1 2 3 4 5
Sample Output
37
25
15

题意:有n个房子,每买k个房子就会有一个免费的,问你花最少金钱数。
思路:从后往前遍历求解,因为我们可以考虑优先队列,由大到小排,然后从后向前遍历
从最后一个数开始入队,当入队的那个房子是k+1的倍数的时候,就取队头元素,这个就是可以免费送的那几个房子里最大的那一个,然后出队
code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
const int mod = 1000000009;
typedef long long LL;
using namespace std;
int main()
{
    priority_queue<LL,vector<LL>,less<LL> > q;
    LL n,k,sum=0,a[100010];
    while(cin>>n>>k)
    {
        sum=0;
        for(LL i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        for(LL i=n; i>=1; i--)
        {
            q.push(a[i]);
            if(i%(k+1)==0)
                q.pop();
        }
        while(q.size()>0)
        {
            sum+=q.top();
            q.pop();
        }
        cout<<sum<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值