BACKUP - Backup Files

no tags 

You run an IT company that backs up computer data for large offices. Backing up data is not fun, and so you design your system so that the different offices can back up each others' data while you sit at home and play computer games instead.

The offices are all situated along a single street. You decide to pair up the offices, and for each pair of offices you run a network cable between the two buildings so that they can back up each others' data.

However, network cables are expensive. Your local telecommunications company will only give you k network cables, which means you can only arrange backups for k pairs of offices (2k offices in total). No office may belong to more than one pair (that is, these 2k offices must all be different). Furthermore, the telecommunications company charges by the kilometre. This means that you need to choose these k pairs of offices so that you use as little cable as possible. In other words, you need to choose the pairs so that, when the distances between the two offices in each pair are added together, the total distance is as small as possible.

As an example, suppose you had five clients with offices on a street as illustrated below. These offices are situated 1 km, 3 km, 4 km, 6km and 12km from the beginning of the street. The telecommunications company will only provide you with k = 2 cables.

The best pairing in this example is created by linking the first and second offices together, and linking the third and fourth offices together. This uses k = 2 cables as required, where the first cable has length 3km - 1km = 2 km, and the second cable has length 6km - 4km = 2 km. This pairing requires a total of 4km of network cables, which is the smallest total possible.

Input

Multiple test cases, the number of them will be given at the very first line.

For each test case:

The first line of input will contain the integers n and k, representing the number of offices on the street (2 <= n <= 100 000) and the number of available network cables (1 <= k <= n/2).

The following n lines will each contain a single integer (0 <= s <= 1 000 000 000), representing the distance of each office from the beginning of the street. These integers will appear in sorted order from smallest to largest. No two offices will share the same location.

Output

Output should consist of a single positive integer, giving the smallest total length of network cable required to join 2k distinct offices into k pairs.

Example

Input:
1
5 2
1
3
4
6
12 Output:
4 Explanation

The sample input above represents the example scenario described earlier.

Warning: large input/output data,be careful with certain languages

Blue Mary's Note: test data has been modified on Dec. 5, 2007. All the solutions have been rejudged.

  这题有个性质,就是你要匹配的任意两对点的连线不能重合, 否则就不是最优的了。利用这个性质去贪心解题。

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using std::priority_queue;
using std::vector;
typedef long long LL;
struct A{
int l, r, pos;
LL dis;
};
const int Maxn = ;
int s[Maxn], mark[Maxn], tot;
A l[Maxn];
struct cmp{
bool operator ()(A a,A b){
return a.dis > b.dis;
}
};
int main(){
#ifndef ONl_JUDGE
freopen ("backup.in", "r", stdin);
freopen ("backup.out", "w", stdout);
#endif
int n, k;
scanf ("%d%d", &n, &k);
priority_queue<A, vector<A>, cmp> q;
for (int i = ; i <= n; ++i)
scanf ("%d", s+i);
for(int i=;i<n;i++)
{
l[i].dis = s[i+]-s[i];
l[i].pos = i;
l[i].l = i-;
l[i].r = i+;
}
l[n-].r=;
for(int i=;i<n;i++)
q.push(l[i]);
LL ans = ;
while (!q.empty()){
A a = q.top();
q.pop();
if (mark[a.pos]) continue;
ans += l[a.pos].dis;
if (--k == ) break;
LL w = -l[a.pos].dis;
if (l[a.pos].l){
w += l[l[a.pos].l].dis;
mark[l[l[a.pos].l].pos] = ;
l[a.pos].l = l[l[a.pos].l].l;
if(l[a.pos].l)
l[l[a.pos].l].r = a.pos;
}
else
mark[a.pos] = ;
if (l[a.pos].r){
w += l[l[a.pos].r].dis;
mark[l[l[a.pos].r].pos] = ;
l[a.pos].r = l[l[a.pos].r].r;
if(l[a.pos].r)
l[l[a.pos].r].l = a.pos;
}
else
mark[a.pos] = ;
if (mark[a.pos] == ){
if (l[a.pos].r) l[l[a.pos].r].l = ;
if (l[a.pos].l) l[l[a.pos].l].r = ;
}
else{
l[a.pos].dis = w;
q.push(l[a.pos]);
}
}
printf ("%lld", ans);
return ;
}

贪心:SPOJ Backup Files的更多相关文章

  1. Python Backup Files

    近来书写 Python 脚本进行替换以前的 shell 脚本,发现 Python 优于 shell 的最直观的一点Python 结构明了,可读性高(本人认为)在此做一些记录 本次记录利用 Python ...

  2. [PowerShell] Backup Folder and Files Across Network

    ## This Script is used to backup folder/files and delete the old backup files. ## Author: Stefanie # ...

  3. TFS Express backup and restore

    When we setup source control server, we should always make a backup and restore plan for it. This ar ...

  4. ORA-19815,ORA-19809 :limit exceeded for recovery files

    数据库重新启动的时候,收到了ORA-19815的错误.从错误的提示来看,是由于闪回区的空间被填满导致无法成功启动.这种情形我们通常考虑的是清除归档日志,那就直接在OS层面rm了,真的是这样吗?客官,如 ...

  5. Mysql官方文档翻译系列-7.3.1 Establishing a Backup Policy

    原文链接 (https://dev.mysql.com/doc/refman/5.7/en/backup-policy.html) 正文 To be useful, backups must be s ...

  6. SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule

    In this post,I will show you SharePoint 2013 How to Backup Farm Automatically with a PowerShell and ...

  7. Create maintenance backup plan in SQL Server 2008 R2 using the wizard

    You will need to identify how you want your maintenance plan to be setup. In this example the mainte ...

  8. [How to] ROOT, Backup & Flash (MTKDroidTools, Spflashtool, CWM)

    这是一篇来自xda论坛的文章,写得很详细,很有用,以下是原文: Hi This is a guide to ROOT, backup and flash your MTK65xx or Other d ...

  9. [转]How to Use xp_dirtree to List All Files in a Folder

    本文转自:http://www.sqlservercentral.com/blogs/everyday-sql/2012/11/13/how-to-use-xp_dirtree-to-list-all ...

随机推荐

  1. typeerror $.ajax is not a function

    在web开发中使用jQuery进行前端开发遇到这么个问题,纠结了很久终于解决了,下面说一下解决方法. 大家可以参照下面几种排查的方法. 1.首先检查是否引用jQuery的库. 2.页面如果使用的ifr ...

  2. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    错误: 解决方案: 1."设置应用程序池默认属性"/"常规"/"启用32位应用程序",设置为 true. 如下图所示:(已测试,好使) 方法 ...

  3. iOS单例的两种实现

    单例模式算是开发中比较常见的一种模式了.在iOS中,单例有两种实现方式(至少我目前只发现两种).根据线程安全的实现来区分,一种是使用@synchronized,另一种是使用GCD的dispatch_o ...

  4. 挖潜无极限———数据挖掘技术与应用热点扫描[ZZ]

    “我们把世界看成数学,并且把你也看成数学”——用这句话来说明数据挖掘技术的复合性和应用的广泛性似乎再好不过.如今,虽然一些行业在应用这一技术上仍然缺乏足够的主动,但一个不能阻挡的趋势是:已经有越来越多 ...

  5. filter过滤器执行顺序

    浏览器请求---->进入过滤器---->进入doFilter方法--->执行chain.doFilter()方法就会放行----->进入业务逻辑方法------>进入过滤 ...

  6. js string操作总结

    var str = "0123456789"; console.log(str.substring(0)); //------------"0123456789" ...

  7. 初涉JavaScript模式 (1) : 简介

    什么是模式? 广义上的模式是指 :在物体或事件上,产生的一种规律变化与自我重复的样式与过程.在模式之中,某些固定的元素不断以可预测的方式周期性重现.最基本而常见的模式,称为密铺,具备重复性以及周期性两 ...

  8. js时间戳转为日期格式

    转自:http://wyoojune.blog.163.com/blog/static/57093325201131193650725/ 这个在php+mssql(日期类型为datetime)+aja ...

  9. jquery.lazyload.js图片延迟加载

    转:http://www.jb51.net/article/50273.htm 这篇文章主要介绍了Jquery图片延迟加载插件jquery.lazyload.js的使用方法,需要的朋友可以参考下   ...

  10. while死循环问题-输入字符就会死循环

    问题: 是否会遇到这样的问题,在while循环中 sanf("%d",&a);如果输入的不是数字,是字符就会进入死循环. 解决方案:都是缓冲区惹的祸,输入字符后,字符会一直 ...