贪心: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 ...
随机推荐
- CTE在Oracle和Sqlserver中使用的差异
CTE是一个很好用的工具,他可以帮助我们清晰代码结构,减少临时表使用,同时oracle和sqlserver都提供支持.但在oracle和sqlserver中使用CTE也存在一定区别. Oracle使用 ...
- mxnet运行时遇到问题及解决方法
1.训练好模型之后,进行预测时出现这种错误: mxnet.::] src/ndarray/ndarray.cc:: Check failed: ,) to.shape=(,) 这种问题的解决方法,在全 ...
- 解决win service 2003 IIS发布Gis网站后,访问地图服务出错,无法正常打开而且 事件查看器出现错误提示。
错误详情: 应用程序-特定 权限设置未将 COM 服务器应用程序(CLSID 为{379376DB-AEA6-40D1-9491-9345E61EF6BE})的 本地 激活 权限授予用户 NT AUT ...
- 全国OA系统下载地址(全)
思道OAhttp://www.anyoffice.net微软.NET平台,支持64位 金和OAhttp://www.jinher.com 红帆OAhttp://www.ioffice.cn 致远OAh ...
- iOS里面如何同时使用开启ARC的库 和 没有开启 ARC的库,ARC 与非 ARC同时存在的问题
旧工程配置arc方案: 1,直接在targets->build phases中修改compiler Flags,是否支持arc.添加:-fobjc-arc,就可以让旧项目支持arc. 新工程配置 ...
- java常用指令
javac 编译java源文件到字节码文件 -d XXX 1.指定编译后的字节码文件存放位置. 2.若编译的java源文件中使用包名,则根据包名生成相应的子目录 javac -d . Hello.ja ...
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- Sublime Text 皮肤插件安装
安装皮肤, 举例sodahttps://github.com/buymeasoda/soda-themectrl+shift+p => Package Control: Install Pack ...
- window下配置SSH连接GitHub、GitHub配置ssh key(转)
转自:http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html 此经验分两部分: 第一部分介绍:在windows下通过msysGit ...
- truncate 命令删除恢复
truncate命令可以一次性删除当前表中所有记录并且不留任何日志,同时这个表的ID就自动初化从1开始,今天我就来给大家尝试一个利用truncate清除记录之后恢复过程. 实际线上的场景比较复杂,当时 ...