三分查找的一些细节

来源:互联网 发布:asp微商城源码 编辑:程序博客网 时间:2024/06/14 11:14

I - Devu and his Brother
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 439D
Description
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and b by their father. The array a is given to Devu and b to his brother.

As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother’s array b.

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu’s condition so that the brothers can play peacefully without fighting.

Input
The first line contains two space-separated integers n, m(1 ≤ n, m ≤ 105). The second line will contain n space-separated integers representing content of the array a(1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the array b(1 ≤ bi ≤ 109).

Output
You need to output a single integer representing the minimum number of operations needed to satisfy Devu’s condition.

Sample Input
Input
2 2
2 3
3 5
Output
3
Input
3 2
1 2 3
3 4
Output
4
Input
3 2
4 5 6
1 2
Output
0
Hint
In example 1, you can increase a1 by 1 and decrease b2 by 1 and then again decrease b2 by 1. Now array a will be [3; 3] and array b will also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu’s condition are 3.

In example 3, you don’t need to do any operation, Devu’s condition is already satisfied.

思路:这里写图片描述
所以使用三分法。

代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;LL n,m,a[100000+10],b[100000+10];LL step(LL x){    LL ans=0;    for(LL i=0;i<n;i++)        if(a[i] < x)            ans+=x-a[i];    for(LL i=0;i<m;i++)        if(b[i] > x)            ans+=b[i]-x;    return ans;}void work(){    LL l=1,r=INT_MAX,m=0,mm,time=0;    while(l+1<r)    {        m=(r - l) / 2 + l;        mm=(r - m) / 2 + m;        if(step(m)<=step(mm)) r=mm;        else l=m;    }    printf("%I64d\n",min(step(l),step(r)));}int main(){    #ifdef LOCAL        freopen("in.txt","rb",stdin);    #endif    while(scanf("%I64d%I64d",&n,&m)!=EOF)    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        for(LL i=0;i<n;i++) scanf("%I64d",&a[i]);        for(LL i=0;i<m;i++) scanf("%I64d",&b[i]);        work();    }    return 0;}

其中,

void work(){    LL l=1,r=INT_MAX,m=0,mm,time=0;    while(l+1<r)    {        m=(r - l) / 2 + l;        mm=(r - m) / 2 + m;        if(step(m)<=step(mm)) r=mm;        else l=m;    }    printf("%I64d\n",min(step(l),step(r)));}

代码中的,<=的位置非常重要。一定要注意!

0 0
原创粉丝点击