• IEEEXtreme 10.0


    博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

    Xtreme 10.0 - Dog Walking

    题目来源 第10届IEEE极限编程大赛

    https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dog-walking

    Your friend, Alice, is starting a dog walking business. She already has K dog walkers employed, and today there are N dogs that need to be walked. Each dog walker can walk multiple dogs at the same time, so the dogs will be arranged into K nonempty groups, and each group will then be walked by a single dog walker. However, smaller dogs can be aggressive towards larger dogs, and that makes it harder to walk them together.

    More formally, if the smallest dog in a group has size a, and the largest dog in the group has size b, then the range of the group is defined as b-a. In particular, the range of a group consisting of a single dog is 0. The smaller the range of a group is, the easier it is to walk that particular group. Hence Alice would like to distribute the dogs among the dog walkers so that the sum of ranges of the groups is minimized. Also, since she doesn't want any of the dog walkers to feel left out, she makes sure each dog walker gets to walk at least one dog.

    Given NK and the sizes of the dogs, can you help Alice determine what is the minimum sum of ranges over the Kgroups if the dogs are arranged optimally?

    Input Format

    The first line of input contains t, 1 ≤ t ≤ 5, which gives the number of test cases.

    Each test case starts with a line containing two integers N, the number of dogs, and K, the number of employees, separated by a single space. Then N lines follow, one for each dog, containing an integer x representing the size of the corresponding dog.

    Constraints

    1 ≤ K ≤ N ≤ 105, 0 ≤ x ≤ 109

    Output Format

    For each test case, you should output, on a line by itself, the minimum sum of ranges over the K groups if the dogs are arranged optimally.

    Sample Input

    2
    4 2
    3
    5
    1
    1
    5 4
    30
    40
    20
    41
    50
    

    Sample Output

    2
    1
    

    Explanation

    In the first test case there are four dogs: one of size 3, one of size 5, and two of size 1. There are two dog walkers, and we want to distribute the dogs among them. One optimal way to do this is to make one dog walker walk the dogs of size 3 and 5, and the other dog walker walk the two dogs of size 1. Then the first group has range 5-3=2, while the second group has range 1-1=0, giving a total of 2+0=2.

    In the second test case there are dogs of size 30, 40, 20, 41 and 50, and four dog walkers. There are so many dog walkers that we can ask all but one of them to walk a single dog. We will make the last dog walker walk the dogs of size 40 and 41, which gives a range of 41-40=1. All other groups have range 0, so the total is 1.

    题目解析

    这题是一个贪心算法的题目。

    最初N条狗各自归为1组,然后选择代价最低(大小最接近)的两条狗合并,合并N-K次。

    算法:

    1) 对N条狗的大小从小到大dogs排序

    2) 计算相邻两条狗大小的差距保存到数组diff中

    3) 对diff从小到大排序

    4) 对diff数组前N-K个数求和

    程序

    C++

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int T;
        cin >> T;
        while(T--) {
            int N, K;
            cin >> N >> K;
            vector<int> dogs;
            for(int i=0; i<N; i++) {
                int d;
                cin >> d;
                dogs.push_back(d);
            }
            
            sort(dogs.begin(), dogs.end());
            vector<int> diff;
            for(int i=1; i<N; i++) {
                diff.push_back(dogs[i]-dogs[i-1]);
            }
            sort(diff.begin(), diff.end());
            int cost = 0;
            for(int i=0; i<N-K; i++) {
                cost += diff[i];
            }
            cout << cost << endl;
        }
        return 0;
    }
  • 相关阅读:
    SpringBoot Maven项目 Helloworld 测试
    Oracle client安装教程
    quartz定时任务时间设置
    maven导出项目依赖的jar包
    Java 集合类
    Webservice客户端动态调用服务端功能方法
    使用Eclipse自带的Axis1插件生成Web Service服务端客户端
    SpringBatch Sample (五)(复合格式文件的读、多文件的写)
    Mysql性能分析
    设置nginx中文件上传的大小限制度
  • 原文地址:https://www.cnblogs.com/meelo/p/6055507.html
Copyright © 2020-2023  润新知