The 2018 ACM-ICPC Asia Qingdao Regional Contest -J

版权声明:随意转载 https://blog.csdn.net/nuiniu/article/details/84110409

题目链接:传送门

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 n and m (1<=n<=1e5,0<=m<=n ), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.

The second line contains  non-negative integers a1,a2,....an (0<=ai<=1e9), 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 1e6.

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).

扫描二维码关注公众号,回复: 5536254 查看本文章

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

这一题,有点意思,一开始我以为就是简单的贪心,然后排个序,submit,wa,再改再wa,然后和队友去讨论,发现了解决问题的重点:You are asked to tell him the maximum possible amount of money DreamGrid took before buying the books, which is a non-negative integer.

emmm,具体思路:分类讨论

1.当n==m,就是DreamGrid买下了所有的书,cout<<Richman

2.当m<n,这时候要分成两种情况,如下:

(1).当0元的书大于m时,cout<<Impossible

(2).当0元的书(z)小于等于m时,又根据题目意思要我们求出DreamGrid的最多的钱,综前所述意思就是除了0元的书,后面的m-z本书都是直接按顺序买使得DreamGrid的钱最多。(emmm,不会表达,仔细看我划横线的句子,多揣测揣测,还是看不懂可以直接留言问我)

code:

#include<stdio.h>
#include<algorithm>

using namespace std;

typedef long long ll;

const int MAX=1e6+6;

ll p[MAX];

int main()
{
	ll T,n,m;
	ll i,j;
	
	scanf("%lld",&T);
	
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		
		int num=0;
		
		for(i=0;i<n;i++)
		{
			scanf("%lld",&p[i]);
			if(p[i]==0) ++num;
		}
			
		if(m==n) printf("Richman\n");
		else if(num>m) printf("Impossible\n");
		else if(num==m)
		{
			sort(p,p+n);
			printf("%lld\n",p[m]-1);
		}
		else
		{
			ll sum=0;
			
			for(j=0,i=0;j<m-num;i++)
			{
				if(p[i]!=0)
				{
					sum+=p[i];
					j++;
				}
			}
			
			sort(p+i,p+n);
			
			for(j=i;j<n;j++)
			{
				if(p[j]!=0)
				{
					sum+=p[j];
					break;
				}
			}

			printf("%lld\n",sum-1);
		}	
	}
	
	return 0;
}

只会写if()...;else...;的垃圾博主,谁有更好的写法吗,求链接。

猜你喜欢

转载自blog.csdn.net/nuiniu/article/details/84110409