• Codeforces 1399B


    B. Gifts Fixing

    time limit per test1 second
    memory limit per test256 megabytes
    input standard input
    output standard 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≤109), 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≤109), 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
    inputCopy
    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
    outputCopy
    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, so a=[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].

    题目大意:

    给出 t 组样例,每组样例输入一个n,然后再输入两行,每行 n 个数,代表你现在有n 份礼物 ,要分给n 个小朋友,第一行代表第 i 份礼物的糖果数,第 2 行代表第 i 份礼物的橙子数,每个小朋友分到的礼物必须一样多,求获得n份相同礼物的最小操作数,操作如下:

    • 吃掉一个糖果
    • 吃掉一个橘子
    • 从其中一份礼物中吃掉一个糖果和橘子

    解题思路:

    既然要每份都想等,操作只有减的操作没有加的操作,那么每一行取最小,然后该行所有数量都减到最小即可,利用一下第三个操作,如果需要吃掉3个糖果和5个橙子,那么一共操作5次即可,也就是说,每次只要 ans += max(a[i] - ma, b[i] - mb) 即可,ma mb 均为每行最小数。

    Code:

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    const int N = 150;
    int a[N], b[N];
    int main()
    {
    	ios::sync_with_stdio(false);
    	int t;
    	cin >> t;
    	while (t--)
    	{
    		int n, ma = 0x3f3f3f3f, mb = 0x3f3f3f3f;
    		cin >> n;
    		for (int i = 1; i <= n; i ++)
    		{
    			cin >> a[i];
    			ma = min(ma, a[i]);
    		}
    		for (int i = 1; i <= n; i ++)
    		{
    			cin >> b[i];
    			mb = min(mb, b[i]);
    		}
    		ll ans = 0;
    		for (int i = 1; i <= n; i ++)
    		{
    			int x = a[i] - ma;
    			int y = b[i] - mb;
    			ans += max(x, y);
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    noip的一些模板(参考了神牛的博客)
    NOIP算法总结与复习
    算是一份学习计划
    RMQ-ST算法的理解与实现(C++)
    SharePoint2010母版页想要的定制
    Memoization
    mutable and immutable
    Sqlite
    PyCharm Change Font Size
    Sublime Text添加gcc编译器
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294157.html
Copyright © 2020-2023  润新知