贪心:SPOJ Backup Files
BACKUP - Backup Files
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的更多相关文章
- Python Backup Files
近来书写 Python 脚本进行替换以前的 shell 脚本,发现 Python 优于 shell 的最直观的一点Python 结构明了,可读性高(本人认为)在此做一些记录 本次记录利用 Python ...
- [PowerShell] Backup Folder and Files Across Network
## This Script is used to backup folder/files and delete the old backup files. ## Author: Stefanie # ...
- TFS Express backup and restore
When we setup source control server, we should always make a backup and restore plan for it. This ar ...
- ORA-19815,ORA-19809 :limit exceeded for recovery files
数据库重新启动的时候,收到了ORA-19815的错误.从错误的提示来看,是由于闪回区的空间被填满导致无法成功启动.这种情形我们通常考虑的是清除归档日志,那就直接在OS层面rm了,真的是这样吗?客官,如 ...
- 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 ...
- 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 ...
- 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 ...
- [How to] ROOT, Backup & Flash (MTKDroidTools, Spflashtool, CWM)
这是一篇来自xda论坛的文章,写得很详细,很有用,以下是原文: Hi This is a guide to ROOT, backup and flash your MTK65xx or Other d ...
- [转]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 ...
随机推荐
- 10.23 noip模拟试题
尼玛蛋pdf好难粘 直接写了 T1 /*开始写wa了 我真弱2333 关于p的排序规则不只是差值 为了字典序最小 还要拍别的*/ #include<cstdio> #include< ...
- 实用脚本 - - insertAfter 在现有元素后插入一个新元素
function insertAfter(newElement,targetElement){ var parent = targetElement.parentNode; if(parent.las ...
- maclean-【性能调优】Oracle AWR报告指标全解析 学习笔记
原文链接:http://www.askmaclean.com/archives/performance-tuning-oracle-awr.html AWR小技巧 手动执行一个快照: Exec dbm ...
- OC - 30.如何封装自定义布局
概述 对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果 由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可 在进行分装的时候,通常需要用 ...
- ZendStudio快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当前 ...
- Java 基础(一)
Java不只是一种语言,更是一个完整的平台,有一个庞大的库,其中包含了很多可重用的代码和一个提供诸如安全性.跨操作系统的可移植性以及自动垃圾收集等服务的执行环境. javaSE: 整个java技术的核 ...
- Vijos1734 NOI2010 海拔 平面图最小割
建立平面图的对偶图,把最小割转化成最短路问题 Dijkstra算法堆优化 (被输入顺序搞WA了好几次T_T) #include <cstdio> #include <cstring& ...
- 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- 分数拆分( Fractions Again, UVA 10976)-ACM
It is easy to see that for every fraction in the form (k > 0), we can always find two positive i ...
- Win7系统下完全删除Mysql
今天不知为什么Mysql服务器突然连接不上,于是胡乱折腾了一番,导致最后不得不重新安装Mysql.安装不成功,服务器起不来,就是最后那步的时候服务器启动不了,这是因为Mysql在卸载的时候没有彻底卸载 ...