J - Books (2018青岛icpc)(思维)

滴答滴答---题目链接 

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

  • Check the  books from the 1st one to the -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  books and the number of books DreamGrid bought in total, indicated by .

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (, ), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.

The second line contains  non-negative integers  (), where  indicates the price of the -th book checked by DreamGrid.

It's guaranteed that the sum of  in all test cases will not exceed .

Output

For each test case output one line.

If it's impossible to buy  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.

Sample Input

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

Sample Output

6
96
Richman
Impossible

大概意思就是给T组样例,每组一个n,m代表n本书,买了其中m本。然后再给出n本书的价格。购买规则是从前往后遍历书,只要书的价格小于自己剩余的钱数,就购买,否则就跳过。问恰好能买m本拥有的钱最多是多少?若过能全买输出Richman,如果不能恰好买m本输出Impossible;

思路:

​    若n == m就Richman,否则第一遍遍历价格为0的书的数量。如果大于m就Impossible。否则就遍历剩下的不为0的图书,小于<= m - 价格为0的数量时,累加钱数,当> m - 价格为0的数量时,比较出其中的最小值。最后答案为累加的钱数+ 最小值 -1
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 1e5+10;
const int N = 0x3f3f3f3f;
long long a[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        int l=0;
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]==0)
                l++;
        }
        if(n==m)
            printf("Richman\n");
        else if(l>m)
            printf("Impossible\n");
        else
        {
            m-=l;
            long long sum=0;
            int k=0;
            for(int i=0; i<n; i++)
            {
                l=i;
                if(k==m)
                    break;
                if(a[i]!=0)
                    k++,sum+=a[i];
            }
            long long ans=N;
            for(int i=l; i<n; i++)
                if(a[i]!=0&&ans>a[i])
                    ans=a[i];
            sum+=ans;
            printf("%lld\n",sum-1);

        }

    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值