当前位置: 代码迷 >> 综合 >> Gifts Fixing
  详细解决方案

Gifts Fixing

热度:74   发布时间:2023-11-22 14:11:53.0

文章目录

  • 一、B. Gifts Fixing
  • 总结


一、B. Gifts Fixing

本题链接:B. Gifts Fixing

题目

B. Gifts Fixing

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You have n gifts and you want to give all of them to children. Of course, you don’t want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of ai candies and bi oranges.

During one move, you can choose some gift 1≤i≤n and do one of the following operations:

eat exactly one candy from this gift (decrease ai by one);
eat exactly one orange from this gift (decrease bi by one);
eat exactly one candy and exactly one orange from this gift (decrease both ai and bi by one).
Of course, you can not eat a candy or orange if it’s not present in the gift (so neither ai nor bi can become less than zero).

As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a1=a2=?=an and b1=b2=?=bn (and ai equals bi is not necessary).

Your task is to find the minimum number of moves required to equalize all the given gifts.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤50) — the number of gifts. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤1e9), where ai is the number of candies in the i-th gift. The third line of the test case contains n integers b1,b2,…,bn (1≤bi≤1e9), where bi is the number of oranges in the i-th gift.

Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.

Example
input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
output
6
16
0
4999999995
7

Note
In the first test case of the example, we can perform the following sequence of moves:

choose the first gift and eat one orange from it, soa=[3,5,6] and b=[2,2,3];
choose the second gift and eat one candy from it, so a=[3,4,6] and b=[2,2,3];
choose the second gift and eat one candy from it, so a=[3,3,6] and b=[2,2,3];
choose the third gift and eat one candy and one orange from it, so a=[3,3,5] and b=[2,2,2];
choose the third gift and eat one candy from it, so a=[3,3,4] and b=[2,2,2];
choose the third gift and eat one candy from it, so a=[3,3,3] and b=[2,2,2].

本博客给出本题截图
在这里插入图片描述
在这里插入图片描述

题意:两个数组,每次可以让任意一个数组的任意一个元素-1,也可以让两个数组相同位置同时-1,求按照如上操作,最少操作几次可以让a1 = a2 = ... = an,并且让b1 = b2 = ... = bn

AC代码

#include <cstdio>
#include <algorithm>using namespace std;typedef long long LL;const int N = 1010, INF = 0x3f3f3f3f;int a[N], b[N], sub[N];
int p, q;
int p1, q1;int main()
{
    int t;scanf("%d", &t);while (t -- ){
    int n;scanf("%d", &n);p = q = INF;for (int i = 0; i < n; i ++ ) {
    scanf("%d", &a[i]);if (q > a[i]){
    q = a[i];q1 = i;}}for (int i = 0; i < n; i ++ ){
    scanf("%d", &b[i]);if (p > b[i]){
    p = b[i];p1 = i;}}for (int i = 0; i < n; i ++ )sub[i] = max(a[i] - a[q1], b[i] - b[p1]);LL res = 0;for (int i = 0; i < n; i ++ ){
    if (i == q1)res += b[i] - b[p1];else if (i == p1)res += a[i] - a[q1];else res += sub[i];}printf("%lld\n", res);}return 0;
}

总结

本题会爆int,注意开成long long