Books (思维题)

题目链接:https://cn.vjudge.net/contest/268424#problem/F

题目描述:

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

题目大意:假如用count1统计价格为0的书。 

三种情况:

1) n=m时输出,Richman(自己是从题解那里知道的n=m时,是Richman。如果是自己当场做题,这种情况就能把自己撂在那里)

2)count1>m时,输出Impossible

                      说第三种情况之前,首先要明白这m本书是如何取得,从第一本书开始,依次向后买书,只要自己的钱够,就买,                不管后边的书是不是比它便宜,如果钱不够,就跳过这本书,买下边的。直到买够m本书为止。问他买够m本书,最                    多可以拥有的钱。

                     接下来说,价格为0的情况,因为要讨论第三种情况所以这里count1<m,价格为0,就相当于不用花钱呗,免费拿。                  所以一定要用m减去价格为0的本数,在从前边开始找m-count1本书就够了,

                     假如对于例子:      7  3

                                                    1  2  3  4 0 1  3

                  不考虑为0的情况,直接要前三本书,但是就算买了这三本依然要往后走的,好了,到了0这了,不用花钱直接送,                       这下就坏了吧,这样就有4本书了,不符合题意的。(当时栽在了这里)

3)遍历不为0的书

第三种情况是最复杂的那个。

首先,先用m减去价格为0的书,即,m-count1。接下来再从前往后遍历m-count1本不为0的书用sum来统计它们的和,找到这几个数据之后,再从这些数据后面找出一个最小值x,最后输出的总钱数就是:sum+x-1。

代码:

#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f3f
const int maxn=100010;
using namespace std;
int main()
{
    int T,n,m;
    long long int a[maxn];
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
        int count1=0;
        long long int sum=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);//注意这里用的是long long 
        }
        for(int i=1;i<=n;i++)
          if(a[i]==0)
            count1++;//统计不为0的个数
        int k=m;//给k赋值为m,因为下方统计除0外,且找到m-count1个不为0的数外
        int i;//这里是方便下方找最后一个x值,因为那个范围w了n多次
        for(i=1;i<=k-count1;i++)//这里寻找m-count1个不为0的数,因为碰到0是要跳过的,但总寻找的个数又不能改变,
            //所以每跳过一个,范围都要再增加1,如果直接在m身上改变,会影响其他部分,所以用另一个变量来代替它
        {
            if(a[i]==0)
            {
                k++;
                continue;
            }
            else
            sum+=a[i];
        }
        long long int x=INF;
        for(int j=i;j<=n;j++)/*范围是错点*/
        {                    //之前自己一直认为范围的初始值就是m-count1+1,但是上方最后的界限是在变化的,所以这里也会受到影响,因为这里是在找够m个值之后,再在后边寻找一个最小值
            if(a[j]!=0)
                x=min(x,a[j]);
        }

         if(count1>m)printf("Impossible\n");
          else if(m==n)printf("Richman\n");
          else
                    printf("%lld\n",sum+x-1);//注意这里要减1,刚好买不到m+1本
        }

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值