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
6
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中应该至少保留一项。
#include<cstdio> #include<iostream> #include<cmath> #include<stdlib.h> #include<vector> #include<cstring> #include<map> #include<algorithm> #include<string.h> #define M(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f using namespace std; int n,m; int a[],b[]; int main() { while(scanf("%d%d",&n,&m)==) { for(int i = ;i<n;i++) scanf("%d",&a[i]); for(int i = ;i<m;i++) scanf("%d",&b[i]); sort(a,a+n); sort(b,b+m); long long sum = ; long long ans = ; if(a[n-]<b[m-]) { for(int i = ;i<n;i++) sum+=a[i]; for(int i = ;i<m-;i++) { if(sum>b[i]) ans+=b[i]; else ans+=sum; } ans+=sum; } else { for(int i = ;i<m;i++) sum+=b[i]; for(int i = ;i<n-;i++) { if(sum>a[i]) ans+=a[i]; else ans+=sum; } ans+=sum; } printf("%I64d\n",ans); } return ; }
CF memsql Start[c]UP 2.0 B的更多相关文章
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- 【CF MEMSQL 3.0 A. Declined Finalists】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 E. Desk Disorder】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 D. Third Month Insanity】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 C. Pie Rules】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CF MEMSQL 3.0 B. Lazy Security Guard】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)
http://codeforces.com/contest/452/problem/B B. 4-point polyline time limit per test 2 seconds memo ...
- MemSQL Start[c]UP 2.0 - Round 2 - Online Round
搞到凌晨4点一个没出,要gg了. A. Golden System http://codeforces.com/contest/458/problem/A #include<cstdio> ...
- MemSQL Start[c]UP 2.0 - Round 1
A. Eevee http://codeforces.com/contest/452/problem/A 字符串水题 #include<cstdio> #include<cstrin ...
随机推荐
- Neutron 理解 (2): 使用 Open vSwitch + VLAN 组网 [Netruon Open vSwitch + VLAN Virutal Network]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- VIM 常用命令
1.当vi打开时默认为命令模式,要转入输入模式,需要按a或者i键. 命令模式下: :wq 保存并且退出 :w 只保存不推出 :q 不保存退出 :q! 不保存强制退出 :wq! 保存并强制退 ...
- Log4j
[1]从零开始 a). 新建Java Project>>新建package>>新建java类: b). import jar包(一个就够),这里我用的是log4j-1.2.14 ...
- jdbc java数据库连接 1)jdbc入门
之前操作数据 1)通过mysql的客户端工具,登录数据库服务器 (mysql -u root -p 密码) 2)编写sql语句 3)发送sql语句到数据库服务器执行 什么是jdbc? 使用jav ...
- MySQL数据类型-decimal详解
from:http://www.linuxidc.com/Linux/2013-07/88032.htm 1.首先,对于精度比较高的东西,比如money,我会用decimal类型,不会考虑float, ...
- iOS面试
1.进程.线程的区别?2.“三次握手”是什么?具体细节,连接释放时需要几次“握手”,说出大概过程.3.TCP.UDP协议的区别?计算机网络分几层,以及TCP.Http协议各自工作在哪一层及相关细节.4 ...
- 【转】error LNK2019: 无法解析的外部符号 "__declspec(dllimport)
生成DLL文件的字符集是Unicode而生成exe文件的字符集为默认的ASCII. 只要统一字符集即可解决问题: VS2005的c++项目默认字符集是使用 Unicode 字符集,在项目属性-> ...
- jeecg表单页面控件权限设置(请先看官方教程,如果能看懂就不用看这里了)
只是把看了官方教程后,觉得不清楚地方补充说明一下: 1. 2. 3. 4.用"jeecgDemoController.do?addorupdate"这个路径测试,不出意外现在应该可 ...
- 第一篇puppet
1,什么是puppet puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppe ...
- Asp.Net通过HttpModule实现URL重写
首先总结一下为什么要对URL进行Rewrite,比如我可以把/Default.aspx?param=3替换成/Home/Default/3(类似mvc). 一.缩短url,隐藏实际路径提高安全性; 二 ...