Assign banana to monkeys(贪心系列)

Problem description:
There are N Monkeys and N bananas are placed in a straight line. Each monkey want to have a banana, if two monkeys want to own the same banana, there will be a fight! A monkey can stay at his position, move one step right from x to x + 1, or move one step left from x to x -1. Any of these moves consumes 1 second. Assign monkeys to banana so that not monkey fight each other and the time when the last monkey gets a banana is minimized.

Input:
The input contain two arrays of int. The first array is the positions of monkeys. The second array is the positions of bananas.

Output:
The output is a int, which is the time(in seconds) it takes when all bananas are assigned to monkeys.

Sample input:
1 3 6
2 4 6
Sample output:
1
Sample explanation:
Assign monkey at position 1 to banana at position 2. (1 second)
Assign monkey at position 3 to banana at position 4. (1 second)
Assign monkey at position 6 to banana at position 6. (0 second)
Overall time is max(1, 1, 0) = 1 second.

#include<bits/stdc++.h>
using namespace std;
int main(){
    vector<int>M,B;
    int num;
    while(scanf("%d",&num))
    {   
    
        M.push_back(num);
            if(cin.get()=='\n')
            break;
    }
    sort(M.begin(),M.end());
    int i;
    while(scanf("%d",&i)){
        
        B.push_back(i);
        if(cin.get()=='\n')
            break;
        
    }
     
    sort(B.begin(),B.end());
    int res=0;
    for(int j=0;j<B.size();j++){
        if(abs(M[j]-B[j])>res)
        res=abs(M[j]-B[j]);
    }
    cout<<res<<endl;

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值