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

    The collector’s puzzle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 944    Accepted Submission(s): 210


    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
     

    总是望着曾经的空间发呆,那些说好不分开的朋友不在了,转身,陌路。 熟悉的,安静了, 安静的,离开了, 离开的,陌生了, 陌生的,消失了, 消失的,陌路了。快哭了

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int a[10005],b[10005];
    int main()
    {
        int N,M,t;
        while(~scanf("%d%d",&N,&M))
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            for(int i=0; i<N; ++i)
            {
                scanf("%d",&t);
                ++a[t];
            }
            for(int i=0; i<M; ++i)
            {
                scanf("%d",&t);
                ++b[t];
            }
            long long tol=0;
            for(int i=0; i<10005; ++i)
                if(a[i]>0)
                {
                    int j;
                    for(j=0;; ++j)
                    {
                        if(i-j>0&&b[i-j]>0)break;
                        if(i+j<10005&&b[i+j]>0)break;
                    }
                    tol=tol+a[i]*j;
                }
            printf("%lld
    ",tol);
        }
        return 0;
    }
    


    @执念  "@☆但求“❤”安★ 下次我们做的一定会更好。。。。吐舌头

    为什么这次的题目是英文的。。。。QAQ...哭

    ------------------- 这是千千的个人网站哦! https://www.dreamwings.cn -------------------
  • 相关阅读:
    二分法模板
    二分答案模板
    51nod 1010 只包含因子2 3 5的数
    三次握手和四次挥手(面试必问)
    TCP协议和UDP协议
    纯CSS3画出小黄人并实现动画效果
    正则表达式里字符串”不包含”匹配技巧
    12个C语言面试题,涉及指针、进程、运算、结构体、函数、内存,看看你能做出几个!
    使用jTopo给Html5 Canva中绘制的元素添加鼠标事件_html5教程技巧
    程序猿们,快用Emoji表情写代码吧
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989699.html
Copyright © 2011-2022 走看看