题目传送
题意:
给你a,b俩个数组,数组中只有0,1,2三种元素,a中有x1个0,y1个1,z1个2。b中有x2个0,y2个1,z2个2。现在你可以任意排列俩个数组,然后对应位进行如下操作合成c数组
问最大的c数组的和是多少
思路:
我们先分析,哪些对答案有贡献。
2 对 0 ,2 * 0 = 0,无贡献
2 对 1 ,2 * 1 = 2,贡献2
2 对 2 , 贡献0
1 对 0 , 贡献0
1 对 1 , 贡献0
0对任何数也是0
所以不难发现,对答案有贡献的只有2和1这一对
所以我们贪心匹配:
1.先把b数组中的2去匹配a数组中的0和2,如果匹配完后,b数组还有剩余的2,那么没办法,只能对答案有负贡献,也就是剩下的2的数量*-2
2.如果b数组匹配后剩余的2的数量为0,那么我们则可以利用a数组的2的数量对答案有正贡献,这时候的贡献则为:a数组的2的数量与b数组1的数量的最小值 * 2
AC代码
#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >> t;
while(t--)
{
ll x1,y1,z1,x2,y2,z2;
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
if(z2 >= x1)
{
z2 -= x1;
if(z2 >= z1) {z2 -= z1;z1 = 0;}
else {z1 -= z2;z2 = 0;}
}
else z2 = 0;
z2 > 0 ? cout << -(z2*2) << endl : cout << (min(z1,y2)*2) << endl;
}
}