I can do it!(贪心)

本文探讨了一种使用贪心算法解决特定问题的方法。给定一组具有两个属性的元素,任务是将这些元素分为两组,使得一组元素属性的最大值与另一组元素属性的最大值之和达到最小。通过分析示例输入输出,我们了解了如何实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I can do it!

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1010    Accepted Submission(s): 471

Problem Description
Given n elements, which have two properties, say Property A and Property B. For convenience, we use two integers Ai and Bi to measure the two properties.  Your task is, to partition the element into two sets, say Set A and Set B , which minimizes the value of max(x∈Set A) {Ax}+max(y∈Set B) {By}. See sample test cases for further details.
 

 

Input
There are multiple test cases, the first line of input contains an integer denoting the number of test cases. For each test case, the first line contains an integer N, indicates the number of elements. (1 <= N <= 100000) For the next N lines, every line contains two integers Ai and Bi indicate the Property A and Property B of the ith element. (0 <= Ai, Bi <= 1000000000)
 

 

Output
For each test cases, output the minimum value.
 

 

Sample Input
1 3 1 100 2 100 3 1
 

 

Sample Output
Case 1: 3
 

 

Author
HyperHexagon
 

题解:题意好难理解啊,意思是给你一些元素,然后把这些元素分到两个集合里面;

然后找A集合中a最大值加上B集合中b最大值的和的最小化;可以用贪心来找。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=100010;
struct Node{
	int a,b;
	friend bool operator < (Node x,Node y){
		return x.a>y.a;
	}
};
Node dt[MAXN];
int main(){
	int T,N,kase=0;
	SI(T);
	T_T{
		SI(N);
		for(int i=0;i<N;i++)SI(dt[i].a),SI(dt[i].b);
		sort(dt,dt+N);
		int A,B=0,ans=INF;
		for(int i=0;i<N;i++){
			ans=min(ans,B+dt[i].a);
			B=max(dt[i].b,B);
		}
		printf("Case %d: %d\n",++kase,ans);
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/handsomecui/p/5060965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值