ZOJ 4067 Books (2018icpc青岛J) (贪心)
题意
给你一个长度为n的数组,代表每一个物品的价格。你有一个初始钱数\(x\),采用以下方法贪心:
从\(1\)到\(n\)扫一遍,如果\(x\)不比\(a[i]\)小,就买下它,买不起就跳过。
给你最终买的物品数量m,求\(x\)的最大值。
思路
我队的贪心菜的一笔,果然我也有很大的责任。。
首先如果最后全部买完,那么刚开始的钱一定是无穷多的。
我们先假设数组里没有0,那么通过题意中的贪心方法,最后买的一定是前m个。
证明:
当\(m=0\)的时候,显然成立。
假设当\(m=i\)的时候成立,即已经连续买了前\(i\)个。
买第\(i+1\)个的时候,如果不买\(a[i+1]\),而买了\(a[j]\),\(j>i+1\):
①当\(a[i+1]\leq a[j]\)时,由于题意中的贪心,如果买了\(a[j]\),对于\(j>i+1\),且\(a[j]\geq a[i+1]\)时,\(a[i+1]\)是必买的,所以不成立
②当\(a[i+1]>a[j]\)时,由于两个位置对m的贡献都为1,为使答案最大,还是要选最大的\(a[i+1]\)
综上所述,此时要买\(a[i+1]\)
再综上所述,最后买的一定是前m个
所以,没有0的时候,我们直接买前m个即可
当数组里是有0时,因为永远可以买0,所以要从m里减去0的个数,然后对剩下的数做上述算法
当然如果0的个数本身就超过\(m\),就是\(impossible\)的。
代码
注释提供了一些假算法的插点
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std;typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;const db eps = 1e-6;
const int mod = 1e9+7;
const int maxn = 3e6+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);int n,m;
ll a[maxn];
int cnt;
ll sum[maxn];
multiset<ll>s;
int main() {int t;scanf("%d", &t);while(t--){cnt=0;scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++){scanf("%lld", &a[i]);if(!a[i])cnt++;}if(n==m){printf("Richman\n");continue;}if(cnt>m){printf("Impossible\n");continue;}ll ans=0;m-=cnt;int p = 0;int num=0;for(int i = 1; i <= n&&num<m; i++){if(a[i]){ans+=a[i];p=max(p,i);num++;}}//printf(" %d\n",p);ll mi = 0x7f7f7f7f7f7f7f7f;for(int i = p+1; i <= n; i++){if(a[i])mi=min(mi,a[i]);}if(mi==0x7f7f7f7f7f7f7f7f){printf("Richman\n");continue;}printf("%lld\n",mi-1+ans);}return 0;
}
/*
2
7 6
4 1 0 3 0 0 1
6 5
4 5 0 3 0 0*/
转载于:https://www.cnblogs.com/wrjlinkkkkkk/p/11560174.html
ZOJ 4067 Books (2018icpc青岛J) (贪心)相关推荐
- 2018ICPC青岛区域赛 zoj4062 Plants vs. Zombies
2018ICPC青岛区域赛 zoj4062 Plants vs. Zombies BaoBao and DreamGrid are playing the game Plants vs. Zombie ...
- 2018icpc青岛Books 思维
题意: DreamGrid went to the bookshop yesterday. There are books in the bookshop in total. Because Drea ...
- 后缀数组 + Hash + 二分 or Hash + 二分 + 双指针 求 LCP ---- 2017icpc 青岛 J Suffix (假题!!)
题目链接 题目大意: 就是给你n个串每个串取一个后缀,要求把串拼起来要求字典序最小!! sum_length_of_n≤5e5sum\_length\_of\_n\leq 5e5sum_length_ ...
- 【ZOJ - 3778】Talented Chef(贪心)
题干: As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same ti ...
- 【ZOJ - 2968 】Difference Game (贪心,思维模拟)
题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...
- NOIP 好题推荐(DP+搜索+图论)POJ ZOJ
NOIP好题推荐(DP+搜索+图论)POJ ZOJ 1370 Gossiping (数论->模线性方程有无解的判断)+(图论->DFS) 1090 Chain ->格雷码和二进制码 ...
- codeforces229 D. Towers(dp+贪心)
D. Towers 设计dp: fif_ifi表示考虑到iii时满足题意的最小代价. 考虑最后一步,也就是最后一组合并即fi=fj+{aj+1+⋯+ai}f_i=f_j+\{a_{j+1}+\dot ...
- 独木舟上的旅行(贪心)
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总 ...
- PKUWC2020游记与题面整理
游记 很久之前 jxp玄学卡线THU和PKU都没有进,默哀. 我校似乎只剩hyw神仙和我了啊--希望好运 12.19 晚上到了北京,住在熟悉的地方--中关新园.我大概在这里考过PKUSC2018和PK ...
- 2016区域赛前冲刺训练
UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...
最新文章
- 接入层的流程和非接入层
- 又双叒有兄弟因为 YYYY-MM-dd 被叫去加班了...
- resize函数_Excel VBA解读(134): 使用Excel函数提高自定义函数的效率
- QT学习笔记(-): 利用QHttp进行http下载(1)
- Java类的继承(将来填坑)
- 我们工作到底为了什么(这篇文章很重要)----强烈推荐
- 我看ITIL在中国(六):如何建立有中国特色的IT运维管理平台【二】
- Miller_Rabin算法
- 内向的人怎样改善人际关系(二)
- hadoop day 6
- 坐标轨迹计算_工业机器人工具坐标系和用户坐标系的区别与联系
- Unity4.3 2D新特性
- 对于谷歌应用传统的自动语音识别(ASR)系统的解析
- 少样本学习系列(一)【Metrics-Based Methods】
- Path.Combine 合并两个路径字符串,会出现的问题
- FLASH寿命测试!
- TinyMce富文本编辑器配置说明
- 中断驱动的自行车码表
- asp.net word操作
- OpenStack手动分布式部署Keystone【Queens版】