AtCoder Beginner Contest 160 E - Red and Green Apples

整理的算法模板:ACM算法模板总结(分类详细版)

Problem Statement

You are going to eat XX red apples and YY green apples.
You have AA red apples of deliciousness p1,p2,…,pAp1,p2,…,pA, BB green apples of deliciousness q1,q2,…,qBq1,q2,…,qB, and CC colorless apples of deliciousness r1,r2,…,rCr1,r2,…,rC.
Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.
From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.
Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

  • 1≤X≤A≤1051≤X≤A≤105
  • 1≤Y≤B≤1051≤Y≤B≤105
  • 1≤C≤1051≤C≤105
  • 1≤pi≤1091≤pi≤109
  • 1≤qi≤1091≤qi≤109
  • 1≤ri≤1091≤ri≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

XX YY AA BB CC
p1p1 p2p2 ...... pApA
q1q1 q2q2 ...... qBqB
r1r1 r2r2 ...... rCrC

Output

Print the maximum possible sum of the deliciousness of the eaten apples.


Sample Input

1 2 2 2 1
2 4
5 1
3

Sample Output 

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

  • Eat the 22-nd red apple.
  • Eat the 11-st green apple.
  • Paint the 11-st colorless apple green and eat it.

Sample Input 

2 2 2 2 2
8 6
9 1
2 1

Sample Output 

25


Sample Input 

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 

74

(题意搞清楚,刚开始把题意看成无颜色苹果可以获得相应颜色苹果的美味值了,以为是dp;qaq...)

  • 题意:给你 a 的红色的苹果,b个绿色的苹果,c个无颜色的苹果,并且给出每个苹果的味道值;其中每个无颜色苹果可以变成红色或者绿色苹果,要求选择x个红苹果,y个绿苹果,使得美味值的和最大
  • 思路:取红苹果中前x大的苹果,绿苹果中前y大的苹果,然后放到无颜色的苹果当中,一共c+x+y个苹果,然后在这些苹果里面取前x+y大的苹果即可;
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N=3*100005;
int aa[N],bb[N],cc[N];

int main() 
{
	int x,y,a,b,c;
	cin >>x>>y>>a>>b>>c; 
	for(int i=0;i<a;i++) cin >> aa[i];
	for(int i=0;i<b;i++) cin >> bb[i];
	for(int i=0;i<c;i++) cin >> cc[i];
	sort(aa,aa+a,greater<int>());
	sort(bb,bb+b,greater<int>());

	for(int i=0;i<x;i++) cc[c+i]=aa[i];
	for(int i=0;i<y;i++) cc[c+x+i]=bb[i];

	sort(cc,cc+c+x+y,greater<int>());
	ll ans=0;
	for(int i=0;i<x+y;i++) ans+=cc[i];
	cout <<ans;
}
发布了231 篇原创文章 · 获赞 60 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_43872728/article/details/105175090
今日推荐