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. Java_Activiti5_菜鸟也来学Activiti5工作流_之JUnit单元测试(四)

    /**ActivitiSpringJuinitTest.java * author : 冯孟活 ^_^ * dates : 2015年9月2日 下午2:16:54 * class : activiti ...

  2. HTML5 <Audio/>标签Api整理(二)

    1.实例2: 相对较完整 Html代码: <style> #volumeSlider .slider-selection { background:#bababa; } </styl ...

  3. 测测你适合从事Web前端开发吗

    一般初创的互联网公司最烧钱的时候往往都是刚刚获得风投或融资的时候,因为他们要把钱砸向前端,因为那时候没有客户访问,对于企业来说只有先做好前端技 术.做好客户体验一切才有可能.用户体验做好,才有人访问, ...

  4. Asp.net Mvc4 基于Authorize实现的模块访问权限

    在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...

  5. nest 'for' loop.

    /* nest for loop demo. Note that,'upside' triangle controls 'inner condition'. */ import kju.print.P ...

  6. [转帖]FPGA--Vivado

    来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...

  7. Scrum教练不应兼任product owner

    ScrumMasters Should Not Also Be Product Owners(中文翻译) December 2, 2014 by Mike Cohn 翻译:2015.2.18 by o ...

  8. 使用Eclipse搭建C/C++开发环境(转)

    使用Eclipse搭建C/C++开发环境  文章出自:http://www.cnblogs.com/liuxianan/archive/2013/01/15/2861196.html 说明:网上有很多 ...

  9. linux搜索命令

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  10. H5的本地存储

    localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...