洛谷 AT_abc358_d [ABC358D] Souvenirs 题解

题目翻译

AtCoder 乐园出售 N N N 种纪念品盒子,标号依次为 1 ∼ N 1\sim N 1N,盒子 i i i 的价格为 A i A_i Ai 元,有 A i A_i Ai 个糖果。

高桥要从中买 M M M 个盒子,然后分别给每个人各一个盒子,第 i i i 个人的盒子中至少要有 B i B_i Bi 个糖果。

请问高桥是否能买到符合要求的 M M M 个盒子,如果能就输出高桥所需要的最小金额,如果不能就输出 -1

题目分析

为了让金额最小,所以给第 i i i 个人买的盒子中的糖果数量得是大于等于 B i B_i Bi 的数中,最小的一个。如果 A A A B B B 都是升序序列,再设 A A A 中大于等于 B i B_i Bi 的数中的最小的一个是 A j A_j Aj,那么 A A A 中大于等于 B i + 1 B_{i+1} Bi+1 的数中最小的一个数的下标绝对大于 j j j。所以可以先将 A A A B B B 进行升序排序,然后一个个寻找。需要注意的是,再续找的过程中还要判断是否有可行的方案。

Code

#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int n,m,a[200000],b[200000],ans=0;
	cin>>n>>m;
	for(int i=0;i<n;++i) cin>>a[i];
	for(int i=0;i<m;++i) cin>>b[i];
	sort(a,a+n);
	sort(b,b+m);
	for(int i=0;i<m;++i)
		for(static int j=0;j<n;++j) {
			if(n-j<m-i||(a[j]<b[i]&&n-j==m-i)) {//如果剩余的盒子数量不够就输出-1
				cout<<-1;
				return 0;
			}
			if(a[j]>=b[i]) {
				ans+=a[j];//累加最小金额
				++j;//这里还要将下标加1
				break;
			}
		}
	cout<<ans;
	return 0;
}
  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I'd be happy to help you plan your trip to Guilin! Here are some tips to make the most out of your 3-day vacation. Day 1: - Start the day early and hike up to the top of the iconic Elephant Trunk Hill. Take in the stunning views of the Li River and the city below. - Next, head over to the Seven Star Park. This beautiful park boasts limestone caves, lush gardens and the Seven Star Cave, which is a must-visit attraction. - Head to the Reed Flute Cave to witness the beautiful natural limestone formations and the dazzling multicolored lights. - End your day with a leisurely stroll around the picturesque Ronghu Lake to soak up the tranquil atmosphere and enjoy the local delicacies. Day 2: - Begin your day by exploring the historic Fubo Hill, a gorgeous hill with a rich cultural and historical significance. - Visit the Guilin Art Museum to appreciate the stunning calligraphy and art exhibitions. - Next up, take a relaxing bamboo raft ride down the Li River and enjoy the breathtaking scenic views. - Head over to the Xiangshan Scenic Area to watch the sunset over the beautiful karst peaks and the stunning skyline of Guilin. Day 3: - Start your day by exploring the spectacular Longji Rice Terraces, located about 2 hours away from Guilin. Take in the panoramic views and marvel at the beautiful rice terraces. - Next, head to the ancient town of Xingping. This charming town is located on the banks of the Li River and is a great place to get a glimpse of traditional Chinese architecture and cultural heritage. - End your day with a visit to the picturesque Yanjiang Road, a pedestrian street filled with local souvenirs, street food, and boutiques. As for your request on what I said earlier, I happen to know a great joke! Why did the tomato turn red? Because it saw the salad dressing! ;)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值