Books 思维题

题目:

DreamGrid went to the bookshop yesterday. There are n books in the bookshop in total. Because DreamGrid is very rich, he bought the books according to the strategy below:

  • Check the n books from the 1st one to the n-th one in order.
  • For each book being checked now, if DreamGrid has enough money (not less than the book price), he'll buy the book and his money will be reduced by the price of the book.
  • In case that his money is less than the price of the book being checked now, he will skip that book.

BaoBao is curious about how rich DreamGrid is. You are asked to tell him the maximum possible amount of money DreamGrid took before buying the books, which is a non-negative integer. All he knows are the prices of the n books and the number of books DreamGrid bought in total, indicated by m.

输入:
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:The first line contains two integers n and m (1 \leq n \leq 10^{5}, 0 \leq m \leq n), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.The second line contains n non-negative integers a1,a2,......,an (0 \leq ai\leq 10^{9}), where ai indicates the price of the -th book checked by DreamGrid.It's guaranteed that the sum of n in all test cases will not exceed 10^{6}.

输出:

For each test case output one line.

If it's impossible to buy m books for any initial number of money, output "Impossible" (without quotes).

If DreamGrid may take an infinite amount of money, output "Richman" (without quotes).

In other cases, output a non-negative integer, indicating the maximum number of money he may take.

样例输入:

4
4 2
1 2 4 8
4 0
100 99 98 97
2 2
10000 10000
5 3
0 0 0 0 1

样例输出:

6
96
Richman
Impossible

一共分别三种情况(我做的时候自己分了四种),首先要知道价格为0的时候这本书是白送的。要定义个变量sum记录价格为0的个数。

第一种:如果价格为0的个数比m大,就输出Impossible;

第二种:如果你要买的书的数量等于所有的书的数量,那就输出Richman

第三种:首先要找出这些书里面有几本价格为0的书,因为价格为0的书是白送的,不要也得要,所以再从剩下的书里选择m-sum本书,最后从当前这本书的下一本书开始到第n本书,找到价格最小的那一本,加上这个价格最后减去1,因为我们要求能有的钱的最大数,如果直接加上这个最小值,那买的书的本书就是m+1了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1e6+5;
const int INF=0x7fffffff;
long long  a[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        long long  sum=0;//记录0的个数
        long long  minn=INF;
        long long num=0;//总的钱数
        scanf("%d%d",&n,&m);

        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]==0)sum++;
        }
        int k=m;//m要用很多次,所以这里定义个新的变量让他为m
        int i;
        for(i=0;i<k-sum;i++)
        {
            if(a[i]==0)//要选的书的总数不能变,所以遇到0时,k要加1
            {
                k++;
                continue;
            }
            else
                num+=a[i];
        }
        for(int j=i;j<n;j++)//求剩下的书的价格的最小值
        {
            if(a[j]!=0)
                minn=min(minn,a[j]);
        }
        if(n==m)
            printf("Richman\n");
        else if(sum>m)
            printf("Impossible\n");
        else
            printf("%lld\n",num+minn-1);//最后别忘了减1,要不书就买多了
        }
    return 0;
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值