zoukankan      html  css  js  c++  java
  • 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle

    Problem Description
    There is a collector who own many valuable jewels. He has a problem about how to store them. There are M special boxes. Each box has a value. And each of the N jewels has a value too. 
    The collector wants to store every jewel in one of the boxs while minimizing the sum of difference value. 
    The difference value between a jewel and a box is: |a[i] - b[j]|, a[i] indicates the value of i-th jewel, b[j] indicates the value of j-th box.
    Note that a box can store more than one jewel.
    Now the collector turns to you for helping him to compute the minimal sum of differences.
    Input
    There are multiple test cases.
    For each case, the first line has two integers N, M (1<=N, M<=100000).
    The second line has N integers, indicating the N jewels’ values.
    The third line have M integers, indicating the M boxes’ values.
    Each value is no more than 10000.
    Output
    Print one integer, indicating the minimal sum of differences.
    Sample Input
    4 4
    1 2 3 4
    4 3 2 1
    4 4
    1 2 3 4
    1 1 1 1
    Sample Output
    0
    6

     我们先排序,然后以第二个数组为基础,第一组的数据去查他们在第二组的位置

    lower_bound
    一共两种情况,要么刚刚好找到,一个在左边
    比如
    2 3 4 5 7 查找3 返回2
    查找 6 返回5 我们不知道它离左边还是右边差值小,可以都考虑进去
    查找8 返回最后一个位置
     
    #include<stdio.h>
    //#include<bits/stdc++.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<sstream>
    #include<set>
    #include<queue>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    #define MAXN (100000+10)
    #define MAXM (100000)
    #define inf 0x3fffffff
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define ULL unsigned long long
    using namespace std;
    int a[MAXN],b[MAXN];
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            int sum=0;
            int i,j;
            for(i=1;i<=n;i++)
            {
                cin>>a[i];
            }
            for(i=1;i<=m;i++)
            {
                cin>>b[i];
            }
            sort(a+1,a+n+1);
            sort(b+1,b+m+1);
            for(i=1;i<=n;i++)
            {
                int mid=inf;
                int ans=lower_bound(b+1,b+m+1,a[i])-b;
             //   cout<<ans<<endl;
                if(ans<=m)
                {
                    mid=min(mid,abs(a[i]-b[ans]));
                }
                if(ans>1)
                {
                    mid=min(mid,abs(a[i]-b[ans-1]));
                }
                sum+=mid;
            }
            cout<<sum<<endl;
        }
        return 0;
    }
  • 相关阅读:
    SVN 初级教程
    572 node包管理工具
    571 node的events模块
    570 node内置模块fs
    569 node内置模块path
    568 node之JavaScript模块化:exports,module.exports,import,对象的引用赋值,require查找规则,export、import关键字,CommonJS的加载过程,ES Module加载过程
    567 node概述:Node程序传递参数,常见的全局对象,特殊的全局对象
    566 手写37个 原生JavaScript 系列汇总(含promise A+)
    565 手写promise源码
    564 函数的防抖和节流
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5525962.html
Copyright © 2011-2022 走看看