[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
农夫 John 余下了 m 批干草无法处理,他准备要开一个拍卖会去出售他的干草。现在有 n 个顾客,每个顾客的报价是 ai。现在 John
要确定一个单价,所有报价大于等于单价的顾客将会买到 1批干草(m 批甘草不用全卖完),总共获得的金钱作为收益。那么问题来了,如何设定单价,使得收益最大。
输入格式
第一行两个整数 m,n分别表示 m 批干草和 n 个顾客。
第二行 n个整数,ai 表示第 i 个顾客的报价。
数据范围:1≤n,m≤1000,1≤ai≤10000。
输出格式
两个用空格分隔的整数,分别表示单价和总收益。
如果有多个相等的最大收益,选取单价最小的那个。
AC代码如下:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std;
int s[1009]; int main() { int n,m; scanf("%d %d",&m,&n); for(int i =
1;i<=n;i++) scanf("%d",&s[i]); sort(s+1,s+n+1,greater<int>()); int pr = 0; int
sum = 0; for(int i = 1;i<=n&&i<=m;i++) { if(sum<=s[i]*i) { pr = s[i]; sum =
s[i]*i; } } printf("%d %d\n",pr,sum); }
代码解析(题解):
按照题目的意思进行枚举就可以
我们先排序(从大到小),这样做的好处:
以i顾客的报价ai为单价,收益为ai * i 。这样在枚举的过程中写的代码就更加舒服与简洁!!!
sort(s+1,s+n+1,greater<int>()); int pr = 0; int sum = 0; for(int i =
1;i<=n&&i<=m;i++) { if(sum<=s[i]*i) { pr = s[i]; sum = s[i]*i; } }
注意 : if(sum<=s[i]*i)
因为题目的要求是:如果有多个相等的最大收益,选取单价最小的那个。
最后输出单价 与 收益 就ok了!
最后,感谢您的阅读!!!
备战蓝桥杯!!!
从今天开始,大一的我要再开一个专栏——备战蓝桥杯
希望大家可以多多支持!
同时也希望 备战蓝桥杯的同志们,金榜题名!!!
我不去想是否能够成功,既然选择了远方,便只顾风雨兼程!