CF 439D Devu and his Brother
来源:互联网 发布:局域网ip mac扫描软件 编辑:程序博客网 时间:2024/05/16 12:30
可贪心 可三分
三分要比贪心运行时间短一些
贪心要保证 b_arry 的逆序排列对应的 a_arry 的正序排列的每个数都要小
#include <iostream>#include <algorithm>#include <cstdlib>using namespace std;const int maxn = 1e5 + 100;int a[maxn];int b[maxn];int n,m;int main(){ while(cin>>n>>m) { for(int i = 0;i < n;i ++) cin>>a[i]; for(int i = 0;i < m;i ++) cin>>b[i]; sort(a,a+n); sort(b,b+m,greater<int>()); n = min(n,m); long long ans = 0; for(int i = 0;i < n;i ++) { if(b[i] > a[i]) ans += (long long)b[i]-a[i]; } cout<<ans<<endl; } return 0;}
最近假期培训刚刚学过三分
也是练习练习
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 1e5 + 100;typedef long long LL;LL a_data[maxn];LL b_data[maxn];int n,m;LL cal(LL x){ LL ret = 0; for(int i = 0;i < n;i ++) if(x > a_data[i]) ret += (x-a_data[i]); for(int i = 0;i < m;i ++) if(x < b_data[i]) ret += (b_data[i]-x); return ret;}int main(){ while(~scanf("%d %d",&n,&m)) { LL a_min = 1000000000,b_max = 0; for(int i = 0;i < n;i ++) { scanf("%I64d",&a_data[i]); a_min = min(a_min,a_data[i]); } for(int i = 0;i < m;i ++) { scanf("%I64d",&b_data[i]); b_max = max(b_max,b_data[i]); } if(a_min >= b_max) { puts("0"); continue ; } LL ans = 1000000000; LL l = a_min,r = b_max; ans = min(cal(l),cal(r)); while(l < r) { LL mid = (l+r)/2; LL midmid = (mid+r)/2; LL tmp1 = cal(mid),tmp2 = cal(midmid); if(tmp1 > tmp2) l = mid; else r = midmid; ans = min(ans,min(tmp1,tmp2)); } printf("%I64d\n",ans); } return 0;}
0 0
- CF 439D Devu and his Brother
- 439 D. Devu and his Brother
- Codeforces 439D Devu and his Brother
- Codeforces 439D Devu and his Brother
- CodeForces 439D Devu and his Brother
- CF 439D(251D题)Devu and his Brother
- Codeforces 439D Devu and his Brother 三分
- Codeforces 439D Devu and his Brother(排序)
- Codeforces 439D Devu and his Brother【思维+三分查找】
- Codeforecs 439D Devu and his Brother 三分
- Codeforces Round #251 (Div. 2) D. Devu and his Brother
- Codeforces Round#251(Div 2)D Devu and his Brother
- Codeforces Round #251 (Div. 2) D Devu and his Brother
- Codeforces Round #251 (Div. 2)-D. Devu and his Brother
- Codeforces Round #251 Div2D 439D Devu and his Brother 二分查找STL
- Codeforces Round #251 (Div. 2) 439D Devu and his Brother(脑洞)
- Codeforces Round #251(Div. 2) 439D. Devu and his Brother 枚举
- CF 66D Petya and His Friends
- C#高级编程六十天----LINQ的概述
- H264学习方法-心历路程-资料搜集
- UVA 11997 K Smallest Sums(优先队列)
- 杭电ACM5327
- android api实现高斯模糊,且兼容低版本
- CF 439D Devu and his Brother
- codeforces244C. Checkposts
- 双向链表的插入删除操
- Sys.WebForms.PageRequestManagerServerErrorException: 在服务器上处理请求时出现未知错误解决办法
- Ubuntu 安装mysql和简单操作
- JAVA学习第十二天
- 鸟哥的linux私房菜学习笔记十一 ,命令权限
- 如何清晰地思考:近一年来业余阅读的关于思维方面的知识结构整理(附大幅思维导图)
- 6. CSS 属性选择器