2023/11/15#codeforces每日算法(1)

链接: 

Problem - 1879B - Codeforces

B. Chips on the Board

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a board of size n×n�×� (n� rows and n� colums) and two arrays of positive integers a� and b� of size n�.

Your task is to place the chips on this board so that the following condition is satisfied for every cell (i,j)(�,�):

  • there exists at least one chip in the same column or in the same row as the cell (i,j)(�,�). I. e. there exists a cell (x,y)(�,�) such that there is a chip in that cell, and either x=i�=� or y=j�=� (or both).

The cost of putting a chip in the cell (i,j)(�,�) is equal to ai+bj��+��.

For example, for n=3�=3, a=[1,4,1]�=[1,4,1] and b=[3,2,2]�=[3,2,2]. One of the possible chip placements is as follows:

White squares are empty

The total cost of that placement is (1+3)+(1+2)+(1+2)=10(1+3)+(1+2)+(1+2)=10.

Calculate the minimum possible total cost of putting chips according to the rules above.

Input

The first line contains a single integer t� (1≤t≤1041≤�≤104) — the number of test cases.

The first line of each test case contains a single integer n� (1≤n≤3⋅1051≤�≤3⋅105).

The second line contains n� integers a1,a2,…,an�1,�2,…,�� (1≤ai≤1091≤��≤109).

The third line contains n� integers b1,b2,…,bn�1,�2,…,�� (1≤bi≤1091≤��≤109).

The sum of n� over all test cases doesn't exceed 3⋅1053⋅105.

Output

For each test case, print a single integer — the minimum possible total cost of putting chips according to the rules.

Example

input

Copy

 

4

3

1 4 1

3 2 2

1

4

5

2

4 5

2 3

5

5 2 4 5 3

3 4 2 1 5

output

Copy

10
9
13
24

Note

The first test case of the example is described in the statement.

 

import java.util.*;
public class Test{
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int t=sc.nextInt();
    while(t-->0){
        int n=sc.nextInt();
        long[] arr1=new long[n];
        long[] arr2=new long[n];
        long min1=Integer.MAX_VALUE;
        long min2=Integer.MAX_VALUE;
        long sum1=0,sum2=0;
        for(int i=0;i<n;i++){
            arr1[i]=sc.nextLong();
            min1=Math.min(min1,arr1[i]);
            sum1+=arr1[i];
        }
        for(int i=0;i<n;i++){
            arr2[i]=sc.nextLong();
            min2=Math.min(min2,arr2[i]);
            sum2+=arr2[i];
        }
        sum1+=min2*n;
        sum2+=min1*n;
         if(sum1<=sum2){
        System.out.println(sum1);
        } else{
    System.out.println(sum2);
    }  
      

}


}


}

题解:

在一个 n×n 的网格里,我们设一个点(i,j) 的代价为 ai​+bj​,你要选出一些点,选取的点所在的横行或纵列会被填充,要求:

1.网格里的每一个点都被填充。

2.选择的点的代价之和最少。】

做法:

考虑贪心。

先上结论:选择 n 个点,且选择的点要么全在同一行,要么全在同一列。

接下来简单说明一下为什么。

首先,很显然,如果选的点数量大于 n,那么不会是最优的。如果选的点数量小于 n,那么不可能覆盖网格的每一个点。

然后,我们选的点都在一行或都在一列是能够让每一个网格中的点都被填充的。这也很显然。

接着,我们重新看眼怎么样的方案能够填充整个网格:要么每一行都有格子被选,要么每一列都有格子被选。

我们先考虑每行都要有格子被选如何最优。我们可以发现,只要保持每行都有格子被选,格子想放在哪一列就放在哪一列。那就可以贪心了:我们把每一个格子都放在对应花费最小的哪一列。

对于每列都要有格子被选的情况也类似,我们直接把每一个格子都放在花费最小的行上。

那我们就可以用贪心了:分找最小行和找最小列两种方法来选择格子,在选取最优的方案。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值