CF memsql Start[c]UP 2.0 B

CF memsql Start[c]UP 2.0 B

B. Distributed Join

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic.

Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B.

In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.

Input

First line contains two integer numbers, m and n (1 ≤ m, n ≤ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≤ ai ≤ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≤ bi ≤ 109).

Output

Print one integer — minimal number of copy operations.

Sample test(s)

input

2 2 
2 6 
3 100 

output

11 

input

2 3 
10 10 
1 1 1 

output

Note

In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11operations

In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2·3 = 6 copy operations.

 

简单贪心,题意不是很好理解。。

大致题意:

A有m个分支,每个分支有ai组,B有n个分支,每个分支有bi组,要将A与B合并(A的每个分支都包含B的全部,或B的每个分支都包含A的全部),使得总花费最小。

贪心策略:将a数组升序排列,b数组升序排列,求出a的和ma,b的和mb。一共就两种情况,要么将A合并到B,要要么将B合并到A,取两者的小值即可。如果是将B合并到A中,我们就将a数组遍历一下,a[i]>=mb就保留a[i],将mb合并到a[i]中,花费为mb,否则,将a[i]合并到A的其他组中,花费为a[i],注意一点如果是a的最后一项必须保留,哪怕是比mb小,也要保留下来,将mb合并进来,因为是将B合并到A,A中应该至少保留一项。

  1 #include<cstdio>
  2 
  3 #include<iostream>
  4 
  5 #include<cmath>
  6 
  7 #include<stdlib.h>
  8 
  9 #include<vector>
 10 
 11 #include<cstring>
 12 
 13 #include<map>
 14 
 15 #include<algorithm>
 16 
 17 #include<string.h>
 18 
 19 #define M(a,b) memset(a,b,sizeof(a))
 20 
 21 #define INF 0x3f3f3f3f
 22 
 23 
 24 
 25 using namespace std;
 26 
 27 
 28 
 29 int n,m;
 30 
 31 int a[100005],b[100005];
 32 
 33 
 34 
 35 int main()
 36 
 37 {
 38 
 39    while(scanf("%d%d",&n,&m)==2)
 40 
 41    {
 42 
 43        for(int i = 0;i<n;i++)
 44 
 45            scanf("%d",&a[i]);
 46 
 47        for(int i = 0;i<m;i++)
 48 
 49            scanf("%d",&b[i]);
 50 
 51        sort(a,a+n);
 52 
 53        sort(b,b+m);
 54 
 55        long long sum = 0;
 56 
 57        long long ans = 0;
 58 
 59        if(a[n-1]<b[m-1])
 60 
 61         {
 62 
 63             for(int i = 0;i<n;i++)
 64 
 65               sum+=a[i];
 66 
 67             for(int i = 0;i<m-1;i++)
 68 
 69             {
 70 
 71                 if(sum>b[i])
 72 
 73                 ans+=b[i];
 74 
 75                 else
 76 
 77                 ans+=sum;
 78 
 79             }
 80 
 81             ans+=sum;
 82 
 83         }
 84 
 85        else
 86 
 87        {
 88 
 89             for(int i = 0;i<m;i++)
 90 
 91                 sum+=b[i];
 92 
 93             for(int i = 0;i<n-1;i++)
 94 
 95             {
 96 
 97                 if(sum>a[i])
 98 
 99                 ans+=a[i];
100 
101                 else
102 
103                 ans+=sum;
104 
105             }
106 
107             ans+=sum;
108 
109        }
110 
111         printf("%I64d\n",ans);
112 
113    }
114 
115    return 0;
116 
117 }

 

posted @ 2014-10-19 19:47  haohaooo  阅读(192)  评论(0编辑  收藏  举报