Gifts Fixing

举报
辰chen 发表于 2022/06/17 23:20:50 2022/06/17
【摘要】 文章目录 一、B. Gifts Fixing总结 一、B. Gifts Fixing 本题链接:B. Gifts Fixing 题目: B. Gifts Fixing time ...


一、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;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

总结

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

文章来源: chen-ac.blog.csdn.net,作者:辰chen,版权归原作者所有,如需转载,请联系作者。

原文链接:chen-ac.blog.csdn.net/article/details/118978258

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。