IEEEXtreme 10.0 - Dog Walking
博客中的文章均为 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 N, K 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=; i<N; i++) {
int d;
cin >> d;
dogs.push_back(d);
} sort(dogs.begin(), dogs.end());
vector<int> diff;
for(int i=; i<N; i++) {
diff.push_back(dogs[i]-dogs[i-]);
}
sort(diff.begin(), diff.end());
int cost = ;
for(int i=; i<N-K; i++) {
cost += diff[i];
}
cout << cost << endl;
}
return ;
}
IEEEXtreme 10.0 - Dog Walking的更多相关文章
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Painter's Dilemma
这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
- IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
随机推荐
- Django 分页 以及自定义分页
Django提供了一个新的类来帮助你管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表.元组或其它可迭代的对象. 基本语法 1 2 3 4 5 6 7 8 9 ...
- oracle的sign()函数
sign函数 比较大小函数 sign 函数语法:sign(n) 函数说明:取数字n的符号,大于0返回1, 小于0返回-1, 等于0返回0 示例1: ),),) from dual; ) ) ) ——— ...
- 关于GCD的几个结论
设a和b的最大公约数是d,那么: 1. d是用sa+tb(s和t都是整数)能够表示的最小正整数 证明:设x=sa+tb是sa+tb能够表示出的最小正整数.首先,有d|x,证明如下: 因此有x>= ...
- poppo大根堆的原理与实现。
大根堆的定义:1 大根堆是一个大根树 2 大根堆是一个完全二叉树 所以大根堆用数组表示是连续的,不会出现空白字段. 对于大根堆的插入 对于大根堆的插入,可以在排序前确定大根堆的形状,可以确定元素5从位 ...
- lightoj 1215
lightoj 1215 Finding LCM 链接:http://www.lightoj.com/volume_showproblem.php?problem=1215 题意:已知 a, b, l ...
- [LeetCode] 31. Next Permutation ☆☆☆
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 5W次单点修改,求最长的连续上升子序列 HDU 3308
题目大意:给你n个数,m个操作. 有两种操作: 1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度. 对于每次询问,输出连续最长子序列的长度 思路:用线段树 ...
- SCI 投稿全过程信件模板一览
- Calendar 日期类介绍
Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...
- script标签中type为<script type="text/x-template">是个啥
写过一点前端的都会碰到需要使用JS字符串拼接HTML元素然后append到页面DOM树上的情况,一般的写法都是使用+号以字符串的形式拼接,如果是短点的还好,如果很长很长的话就会拼接到令人崩溃了. 比如 ...