H - Hotel Rewards(优先队列思维题)

7893 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个房子必须买,k+1个房子以后的找一个最大的是被送的,但是代码一直改不出来,后来知道了要用优先队列,从大到小排,从最后一个数开始入队,当入队的那个房子是k+1的倍数的时候,就取队头元素,这个就是可以免费送的那几个房子里最大的那一个,然后出队
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#include<queue>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e5+10;
using namespace std;
int a[MAX];
int main()
{
    int n,k,topp;
    ll sum;
    priority_queue<int ,vector<int >,less<int> >q;
    while(cin>>n>>k)
    {
        sum=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=n;i>=1;i--)
        {
            q.push(a[i]);
            if(i%(k+1)==0)
           {
                   q.pop();
           }
        }
           while(q.size()>0)
           {
               topp=q.top();
               sum+=topp;
               q.pop();
           }

        printf("%lld\n",sum);
    }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值