codeforces1379C 贪心+二分+思维

本文详细解析了1379C2000题目的解题思路,通过对比完全背包算法,指出此题实际为一道简单的贪心算法题目。介绍了如何通过对a数组进行枚举,结合b数组特性,得出最优解的实现方法。

1379C 2000的题吓了我一跳
水题一个,起码在2000里面是个水题
题意:给你n次操作,m组数据,其中m组数据是这样的,有一个是第一次a数组,第二次及以上b数粗,如果你的操作是对i第一次那么ans+=a[i],如果是第二次及以上那么是ans+=b[i],问你如何操作才能使得ans取得最大值。
思路:一开始我的想法是完全背包。。。因为你第二次是无限取b,前提条件是必须先取一次a。然而,然而,这就是一道简单的贪心,因为你最后得到的答案一定是取多次a以及对一个b取多次,所以只需要对a进行枚举,然后就可以得出ans,具体实现看代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define per(i, a, n) for(int i = n-1; i >= a; i--)
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);
#define fclose fclose(stdin);fclose(stdout);
#define PI 3.14159265358979323846
const int inf = 1e9;
const ll onf = 1e18;
const int maxn = 1e5+10;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}
pair<ll, ll> a[maxn];
ll sum[maxn], b[maxn];
inline void cf(){
	ll t = read();
	while(t--){
		ll m=read(), n = read();
		for(int i = 0; i < n; i++) a[i].first=read(),a[i].second=read();
		sort(a, a+n);
		ll ans = 0;
		sum[0] = a[0].first;b[0]=a[0].first;
		for(int i = 1; i < n; i++){
			sum[i] = a[i].first+sum[i-1];
			b[i]=a[i].first;
		}
		for(int i = 0; i < n; i++){
			int x = lower_bound(b, b+n, a[i].second)-b;
			// printf("%d\n", x);
			if(m>n-x){
				if(x<=i){
					ans = max(ans, sum[n-1]-sum[x-1]+a[i].second*(m-n+x));
				}
				else ans = max(ans, sum[n-1]-sum[x-1]+a[i].first+a[i].second*(m-n+x-1));
				// printf("%lld\n", ans);
			}else {
				ans = max(ans, sum[n-1]-sum[n-m-1]);
				// printf("%lld\n", ans);
			}
		}
		printf("%lld\n", ans);
	}
	return ;
}
signed main(){
	cf();
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值