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. noi1816 画家问题(技巧搜索Dfs)

    /* Problem 画家问题 假设一个ans数组存的是对每一个点的操作 0表示不图 1表示图 那么 对于原图 g 操作第三行时对第一行没有影响 同样往下类似的 所以 假设我们知道了ans的第一行就是 ...

  2. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  3. (ternary operator)三元运算符.

    ternary operator: advantage: make a terse simple conditional assignment statement of if-then-else. d ...

  4. 使用EMMET中的小坑

    使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...

  5. GoogleAuthenticator

    <?php /** * PHP Class for handling Google Authenticator 2-factor authentication * * @author Micha ...

  6. .net版ckeditor配置水印功能(转)

    本文简单讲解ckfinder控件给上图片加水印效果. 1.将ckfinder/plugins/watermark/bin/Debug目录下的CKFinder_Watermark.dll和CKFinde ...

  7. mongo db安装和php,python插件安装

    安装mongodb 1.下载,解压mongodb(下载解压目录为/opt) 在/opt目录下执行命令 wget fastdl.mongodb.org/linux/mongodb-linux-x86_6 ...

  8. 【Python开发实战】Python环境的配置

    1. 安装Pythonsudo aptitude -y install python-dev 安装Distribute:支撑模块构建与导入的包sudo chmod -R 0775 /usr/local ...

  9. Python Tutorial 学习(四)--More Control Flow Tools

    4.1 if 表达式 作为最为人熟知的if.你肯定对这样的一些表达式不感到陌生: >>> x = int(raw_input("Please enter an intege ...

  10. vim代码折叠功能

    问题:怎样在vim中实现代码折叠功能? 解决方法:直接使用vim自带的快捷键和命令,便可以实现功能强大的折叠 小试折叠: 1  :set fdm=marker  在vim中执行该命令 2  5G  将 ...