题目链接:传送门

题目大意:

  DG在书店买书,从左到右第i本书价格为ai。DG从左走到右,能买就买。如果已知DG买了m本书,问他原本最多有多少钱。若无上限,输出“Richman”,若不可能买这么多书,输出“Impossible”。

  (偷偷diss队友想了个假二分)

  1 ≤ n ≤ 105,0 ≤ m ≤ n,0 ≤ ai ≤ 109

思路:

  ①:所有的价格为0的书DG必买,m -= cnt0。(这时若 m < 0,那就Impossible了)

  ②:DG从左走到右买书时,他在持有金额最多的情况下买的书只能是前面的m本书。

    反证:假设DG已经买掉了钱m-1本书,接着如果他买不起第m本书,而买了第m+1本书,那么am > am+1,而在DG买得起第m本书的时候他会更有钱,这与持有金额最多的条件矛盾。

  综上:DG最多的金额是前m本非0的书的总价格+剩下的非0的书中的价格最小值-1。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
int n,m;
ll a[maxn];
int judge(ll k)
{
int res=;
for(int i=;i<=n;i++) {
if(k>=a[i]) res++, k-=a[i];
}
return res;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m); int cnt0=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
if(a[i]==) cnt0++;
}
if(n<=m)
{
printf("Richman\n");
continue;
}
if(cnt0>m)
{
printf("Impossible\n");
continue;
} m -= cnt0;
ll ans=;
int i;
for (i = ; i <= n; i++) {
if (m == )
break;
if (a[i] == )
continue;
ans += a[i];
m--;
}
ll mn=0x3f3f3f3f;
for(;i<=n;i++) if(a[i]!=) mn=min(mn,a[i]);
printf("%lld\n",ans+mn-);
}
}
/*
444
4 2
1 2 4 8
4 0
100 99 98 97
2 2
10000 10000
5 3
0 0 0 0 1
4 1
4 1 3 2
4 2
100 99 0 0
*/

  

ZOJ4067 Books(贪心)的更多相关文章

  1. HDU 5500 Reorder the Books 贪心

    Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  2. UVa 714 Copying books 贪心+二分 最大值最小化

    题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...

  3. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. Codeforces 500C New Year Book Reading

    C. New Year Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. 【ZOJ4067】Books(贪心)

    题意:DG在书店买书,从左到右第i本书价格为ai. DG从左走到右,能买就买.如果已知DG买了m本书,问他原本最多有多少钱. 若无上限,输出“Richman”,若不可能买这么多书,输出“Impossi ...

  8. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

  9. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

随机推荐

  1. python删除指定路径的文件

    import os            import glob                        path =imgDate_listResult            for infi ...

  2. 部署Linux项目

    部署Linux项目   1● 下载软件 ftp       安装 2● 创建连接 3● java项目   gunzip –c *.gz tar –xzf *.gz       rm –rf rm -r ...

  3. linux 文件解锁

    //文件 sudo chmod 777 文件名 //文件夹内的文件 sudo chmod 777 文件夹/ * //遍历文件夹下的所有文件 sudo chmod -R 777 文件夹/ *

  4. Java的Properties类使用

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  5. oo作业总结(二)

    概述 和前三次作业相比,这几次作业最大的不同是难度的飞跃.遗憾的是在这难度的变化面前,我自己却没有做好充分的准备,错误的低估了作业难度导致给自己带来了很多不必要麻烦和损失.接下来我将对它们进行说明(度 ...

  6. HTML(二)选择器

    1.id选择器 一对一关系 <div id="only">123</div> #only{ background-color:black; } 2.clas ...

  7. selenium(七)webdriverwait,高级等待,替代sleep

    #coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from seleni ...

  8. hMailServer SSL 配置

    1.先安装 openssl , 调用如下命令,生成证书: openssl genrsa -des3 - openssl req -new -key alics.key -out alics.req o ...

  9. box-shadow 边框阴影

    box-shadow: 0 0 20px #000 inset;

  10. Java的第一个晞月自己打的程序

    1.编写一个程序,求1!+2!+…+10!的值. package xxx; public class abc { public static void main(String args[]) { in ...