跳到正文

题解

题解:P2676 [USACO07DEC] Bookshelf B

ouxiyao约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
题目
P2676

版本

  1. v1当前

传送门

P2676 [USACO07DEC] Bookshelf B

既然要牛越少越好,那么肯定先让大的去叠,排个序,然后模拟往上叠的过程,直到大于 kk。 完整代码:

C++
#include<iostream>
#include<algorithm>
using namespace std;
long long a[5000050],b[5000500];//十年OI一场空,___________________。
int main(){
    ios::sync_with_stdio(false);
	int n;
    long long k;
	cin>>n>>k;
	for(int i = 1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n,greater<int>());//反着排序。
	long long sum = 0;
    for(int i = 1;i<=n;i++){
        if(sum+a[i]>k){//够高了立马暂停。
            cout<<i;
            return 0;
        }
        sum+=a[i];//否则继续往上叠。
    }
    cout<<n;
	return 0;
}
原文