Gym_102823A Array Merge(贪心)

Array Merge

time limit per test:1 seconds
memory limit per test:256 megabytes
Problem Description

Given two arrays A A A, B B B of length n and m separately, you have to merge them into only one array C C C (of length n + m n+m n+m) obeying the rule that the relative order of numbers in the same original array does not change in the new array.

After merging, please calculate the c o s t = ∑ i = 1 n + m i × C [ i ] cost=\sum_{i=1}^{n+m}i×C[i] cost=i=1n+mi×C[i] and output it.

If there are multiple costs, please output the minimum of them.

Input

The first line of input file contains an integer T T T ( 1 ≤ T ≤ 50 1≤T≤50 1T50), describing the number of test cases.

Then there are 3 × T 3×T 3×T lines, with every three lines representing a test case.

The first line of the test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 1 0 5 1≤n,m≤10^5 1n,m105) as described above.

The second line of that contains n n n integers, ith of which represents the A [ i ] A[i] A[i].

The third line of that contains m m m integers, ith of which represents the B [ i ] B[i] B[i].

The numbers in both array have range in [ 0 , 1 0 8 ] [0,10^8] [0,108].

It is guaranteed that the sum of n + m n+m n+m in all cases does not exceed 1 0 6 10^6 106.

Output

You should output exactly T T T lines. For each case, print Case d : d: d: ( d d d represents the order of test case) first and then print a number representing the minimum cost on the same line.

Sample Input

2
2 2
5 3
4 5
3 3
1 3 5
2 6 4

Sample Output

Case 1: 40
Case 2: 75

题意

有两个数组 a , b a, b a,b,长度分别为 n n n m m m。现在需要将两个数组按顺序放入一个新的数组 c c c,要求原数组内元素顺序不变。最小化 c o s t = ∑ i = 1 n + m i × C [ i ] cost=\sum_{i=1}^{n+m}i×C[i] cost=i=1n+mi×C[i]

思路

如果没有限制,一定是让大的值尽量安排的在前面。
但题目要求按数组原顺序放置元素。那对于值较大的元素,如果它前面的元素都被放入新数组了,那它一定立刻被放入新数组。所以考虑将最大的元素与其前面的那个元素进行捆绑。类似的贪心进行这种操作。

对于每个数组,先对其进行合并:
初始每个元素的值val = a[i],大小sz = 1。
利用栈对其合并,每次将当前元素放入栈顶。判断栈顶的两块能否合并。设栈顶元素长度为len1,和为sv1;栈顶下的元素长度为len2,和为sv2。两者能合并则要求 s v 1 l e n 1 > s v 2 l e n 2 \frac{sv1}{len1} > \frac{sv2}{len2} len1sv1>len2sv2

将两个数组合并后,开始进行合并。对两个数组首个为插入的元素集进行比较,判断条件类似于合并条件。依次合并,计算贡献即可。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#include<unordered_map>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
  
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1000100;
const int mod = 1000000007;
int a[maxn], b[maxn], ta[maxn], tb[maxn];
LL suma[maxn], sumb[maxn];
bool check(int l, LL suml, int r, LL sumr);

int main()
{
    int t, n, m, i, j, k, topa, topb;
    scanf("%d", &t);
    for(int id=1;id<=t;id++){
        topa = topb = ta[0] = tb[0] = 0;
        scanf("%d %d", &n, &m);
        for(i=1;i<=n;i++){
            scanf("%d", &a[i]);
            suma[i] = suma[i-1]+a[i];
        }
        for(i=1;i<=m;i++){
            scanf("%d", &b[i]);
            sumb[i] = sumb[i-1]+b[i];
        }
        for(i=1;i<=n;i++){
            ta[++topa] = i;
            while(topa>1 && check(ta[topa-1]-ta[topa-2], suma[ta[topa-1]]-suma[ta[topa-2]],
                                    ta[topa]-ta[topa-1], suma[ta[topa]]-suma[ta[topa-1]]))
                ta[--topa] = i;
        }
        for(i=1;i<=m;i++){
            tb[++topb] = i;
            while(topb>1 && check(tb[topb-1]-tb[topb-2], sumb[tb[topb-1]]-sumb[tb[topb-2]],
                                    tb[topb]-tb[topb-1], sumb[tb[topb]]-sumb[tb[topb-1]]))
                tb[--topb] = i;
        }
        LL ans = 0;
        i = j = k = 1;
        while(i<=topa || j<=topb){
            if(j > topb || i<=topa && check(tb[j]-tb[j-1], sumb[tb[j]]-sumb[tb[j-1]],
                                    ta[i]-ta[i-1], suma[ta[i]]-suma[ta[i-1]])){
                for(int now=ta[i-1]+1;now<=ta[i];now++)
                    ans += 1LL*a[now]*k++;
                i++;
            }
            else{
                for(int now=tb[j-1]+1;now<=tb[j];now++)
                    ans += 1LL*b[now]*k++;
                j++;
            }
        }
        printf("Case %d: %lld\n", id, ans);
    }
    return 0;
}

bool check(int l, LL suml, int r, LL sumr)
{
    return sumr*l >= suml*r;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值