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;
    }

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhangjinlei321/article/details/84034416

智能推荐

runloop处理scrollview滚动时定时器失效的问题及runloop与timer的关系_scrollview runloopmode-程序员宅基地

文章浏览阅读260次。static int count = 0;NSTimer *timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) { NSLog(@"%d",++count); }]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];由于NSTi._scrollview runloopmode

java task接口_Java并发异步编程,原来十个接口的活现在只需要一个接口就搞定...-程序员宅基地

文章浏览阅读227次。什么?对你没有听错,也没有看错 ..多线程并发执行任务,取结果归集~~ 不再忧愁….引言先来看一些APP的获取数据,诸如此类,一个页面获取N多个,多达10个左右的一个用户行为数据,比如:点赞数,发布文章数,点赞数,消息数,关注数,收藏数,粉丝数,卡券数,红包数……….. 真的是多~ 我们看些图:平时要10+接口的去获取数据(因为当你10+个查询写一起,那估计到半分钟才能响应了),一个页面上N多接口...

xarray 使用教程 - 未完待续-程序员宅基地

文章浏览阅读9.3k次,点赞14次,收藏96次。xarray一些常见用法,推荐用xarray处理nc数据~

redis中bitmaps使用介绍-程序员宅基地

文章浏览阅读1.9k次。redis中有两个主要用于统计的数据结构bitmaps和hyperloglogs。bitmaps:下面是官网对于bitmaps介绍的中文翻译Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type. Since strings are

分离浮点数_接收用户输入的一个浮点数,输出这个浮点数的小数部分-程序员宅基地

文章浏览阅读241次。问题描述:编写一个程序,其功能为:从键盘上输入一个浮点数(小数点后有三位数),然后分别输出该数的整数部分和小数部分。样例输入:123.456样例输出:123 456#include<stdio.h>int main(){ float a; scanf("%f",&a); int b=a; int c; c=a*1000-b*1000; printf("%..._接收用户输入的一个浮点数,输出这个浮点数的小数部分

【原创】Java基础之Freemarker(1)模板加载及清空机制-程序员宅基地

文章浏览阅读189次。一 freemarker加载模版机制freemarker中的配置项template_update_delay表明模版的缓存时间,单位是s,超过缓存时间则从磁盘加载最新的模版,具体细节如下:1)freemarker中获取模版的方法在Configuration中:2)Configuration的getTemplate方法直接代理给TemplateCache:3)Templa..._freemarker 刷新缓存

随便推点

QtUI一二三_ui_dialog.h-程序员宅基地

文章浏览阅读2.4k次。QtUI一二三大多数人使用Qt并不使用Qt的UI,因为有一部分人是使用VS编写Qt,还有一部分人,认为UI设计太过复杂,不如手写来的酸爽,但对于像我这种Qt初学者来说手写太难,不得不先画UI来完成初期的学习。以下是我个人对QtUI的一些简单的理解。ui_Dialog.h我们使用Qt Creator的设计师设计了一个名为Dialog.ui的文件,在Qt Creator后台自动调用了一_ui_dialog.h

ACM--解析HTML--HDOJ 1088--Write a simple HTML Browser--水-程序员宅基地

文章浏览阅读1.9k次。HDOJ题目地址:传送门Write a simple HTML BrowserTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10556 Accepted Submission(s): 3050

npm yarn相关的操作_yarn 能用npmregistry-程序员宅基地

文章浏览阅读132次。淘宝 npm 地址:http://npm.taobao.org/如何使用有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法。以淘宝npm镜像举例:1.临时使用npm --registry https://registry.npm.taobao.org install express12.持久使用npm config set regis..._yarn 能用npmregistry

深度强化学习系列(1): 深度强化学习概述_用一张图描述强化学习方法-程序员宅基地

文章浏览阅读2.4w次,点赞20次,收藏231次。深度强化学习及其在自动驾驶中的应用( DRL &amp; ADS )专栏系列文章规划DRL&amp;ADS系列之(1): 强化学习概述DRL&amp;ADS系列之(2): 深度强化学习及算法讲解DRL&amp;ADS系列之(3): ADS软硬件分析及DRL在Torcs中的应用 概述机器学习是人工智能的一个分支,在近30多年已发展为一门多领域交叉学科,涉及概率论、统计学..._用一张图描述强化学习方法

在排查性能过程中如遇到cpu的wa高时该如何做(一)_cpu wa-程序员宅基地

文章浏览阅读3.6k次,点赞2次,收藏16次。在排查性能过程中如遇到cpu的wa高时该如何做(一)1、引言谈到性能基本上离不开cpu。但对于性能只处于了解压测没有真正调优过的其实也不会关注太多。所以如何做下一步,基本上知道的更少。高手就另说了,如果你已经知道排查问题,到了调优阶段,恭喜你不用看本篇了,你已经是高手了。所以我也以我的理解与经验来进行一步步操作说明。其实也是很简单的,就是多操作实践就好了。2、常用的命令或工具top命令虽然网上有很多说明,但为了更好的记忆,与快速的理解下文中提到相关的操作含义,我在此重复说明一下。top“_cpu wa

使用ping测试MTU值_使用ping检测mtu值-程序员宅基地

文章浏览阅读2.7k次。MTU:MTU是Maximum Transmission Unit的缩写;意思是网络上传送的最大数据包。MTU的单位是字节。大部分网络设备的MTU都是1500。把本机的MTU设成比网关的MTU小或相同,就可以减少丢包。如果本机的MTU比网关的MTU大,大的数据包就会被拆开来传送,这样会产生很多数据包碎片,增加丢包率;如果检测到网关的MTU值是1500,从1400到1472之间多试几次,就能找到..._使用ping检测mtu值

推荐文章

热门文章

相关标签