B. Gifts Fixing(贪婪思维) Codeforces Round #661 (Div. 3)

原题链接:https://codeforces.com/contest/1399/problem/B

题意:有n份礼物盒,礼物盒中又分了a类和b类的数量,为了公平,让n份礼物盒中a类的数量相同,b类的数量也相同。我们可以进行三种操作来让我们完成这个任务,求最小的操作数。

解题思路:千万不要把a类和b类分离开来,因为有一个关键操作就是可以是a类和b类都减1,那么我们应该要如何处理,就是找出a类和b类各自的最小值,再用贪婪算法使得进行的操作数最小,即遍历n个礼物盒,寻找a类和b类与各自最小值之间的差值,再利用那三种操作弥补即可

AC代码:

/*
*邮箱:[email protected]
*blog:https://blog.csdn.net/hzf0701
*注:代码如有问题请私信我或在评论区留言,谢谢支持。
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>
#include<memory.h>//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉
#include<iomanip>
#include<vector>
#include<cstring>
#define scd(n) scanf("%d",&n)
#define scf(n) scanf("%f",&n)
#define scc(n) scanf("%c",&n)
#define scs(n) scanf("%s",n)
#define prd(n) printf("%d",n)
#define prf(n) printf("%f",n)
#define prc(n) printf("%c",n)
#define prs(n) printf("%s",n)
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为代码自定义代码模板***************************************//

ll a[55],b[55];
ll t,n;//t组测试数据,n个结点。
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。
	cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
	while(cin>>t){
		ll ma,mb;
		while(t--){
			ma=inf;
			mb=inf;
			cin>>n;
			rep(i,0,n-1){
				cin>>a[i];
				if(a[i]<ma){
					ma=a[i];
				}
			}
			rep(i,0,n-1){
				cin>>b[i];
				if(b[i]<mb){
					mb=b[i];
				}
			}
			ll sum=0;
			ll ta,tb;
			rep(i,0,n-1){
				ta=a[i]-ma;
				tb=b[i]-mb;
				sum+=(ta>tb?ta:tb);
			}
			cout<<sum<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/107828319