AcWing 每日一题 2022/5/3【2040. 礼物】

AcWing 每日一题 2022/5/3【2040. 礼物】

农夫约翰想给他的 N 头奶牛购买礼物,但是他的预算只有 B 元。

奶牛 i 希望获得的礼物的价格为 Pi,运输成本为 Si,也就是说约翰要帮奶牛 i 买礼物,共需花费 Pi+Si 元钱。

约翰有一张特殊的优惠券,如果使用该优惠券来订购一份礼物,那么该礼物的价格会变为只有正常价格的一半。

如果约翰用该优惠券给奶牛 i 买礼物,那么他只需要支付 Pi/2+Si 元钱。

方便起见,Pi 一定是偶数。

请帮助约翰确定他最多可以给多少头奶牛购买礼物。

输入格式
第一行包含两个整数 N 和 B。

接下来 N 行,每行包含两个整数 Pi 和 Si。

输出格式
输出约翰可以购买礼物的奶牛最大数量。

数据范围

1≤N≤1000,
1≤B≤109,
0≤Pi,Si≤109

输入样例:

5 24
4 2
2 0
8 1
6 3
12 5

输出样例:

4

样例解释
一种最佳方案是约翰给前 4 头奶牛购买礼物,在给第 3 头奶牛购买礼物时使用优惠券。

花费为 (4+2)+(2+0)+(4+1)+(6+3)=22。

题目分析

对于每一件物品有一个价格和一个运输价格,我们有且只有一张优惠券,而优惠券仅可对原价格进行半价优惠,我们要就算在预算范围内,最多可以购买多少个礼物
误区:直接进行排序,优先选择最低价格的礼物,同时判断对超出预算的第一个礼物进行优惠处理是否可以继续购买
样例
2 170
10 100
20 50
正解:由于数据范围只有 1000 直接进行暴力枚举即可,枚举对每一个礼物进行优惠,所能购买的最大数量
时间复杂度:O(106)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<sstream>

#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1010;
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;

int gcd(int a, int b){
    
    return b ? gcd(b, a % b) : a;}

ll n, b;
ll p[N], s[N];

ll get(int u)
{
    
    
	vector<ll> v;
	for(int i = 0; i < n; i ++ )
	{
    
    
		if(u == i) v.push_back(p[i] / 2 + s[i]);
		else v.push_back(p[i] + s[i]);
	}
	sort(v.begin(), v.end());
	ll res = 0;
	ll t = b;
	for(int i = 0; i < n; i ++ )
	{
    
    
		t -= v[i];
		if(t >= 0) res ++ ;
	}
	return res;
}

int main()
{
    
    
	cin >> n >> b;
	for(int i = 0; i < n; i ++ )
	{
    
    
		cin >> p[i] >> s[i];
	}
	ll ans = -INF;
	for(int i = 0; i < n; i ++ )
	{
    
    
		ans = max(ans, get(i));
	}
	cout << ans << endl;
	return 0;
}




猜你喜欢

转载自blog.csdn.net/qq_52354698/article/details/124574972