bzoj1237
假如不存在相等的两个数不能配对,那很容易贪心得到,A中rank 1匹配B中rank 1
A中rank2 匹配B中rank 2……
有了相等不能匹配这个条件,那么A中rank i可能和rank i,i-1,i+1匹配
也有可能三对数字交换匹配
dp一下就好了
const inf=;
type node=array[..] of longint;
var a,b:node;
f:array[..] of int64;
n,i:longint; function min(a,b:int64):int64;
begin
if a>b then exit(b) else exit(a);
end; function calc(a,b:longint):longint;
begin
if a=b then exit(inf)
else exit(abs(a-b));
end; procedure qsort(var a:node);
procedure sort(l,r:longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
sort(,n);
end; begin
readln(n);
for i:= to n do
readln(a[i],b[i]);
qsort(a);
qsort(b);
f[]:=calc(a[],b[]);
f[]:=min(f[]+calc(a[],b[]),calc(a[],b[])+calc(a[],b[]));
for i:= to n do
begin
f[i]:=f[i-]+calc(a[i],b[i]);
f[i]:=min(f[i],f[i-]+calc(a[i-],b[i])+calc(a[i],b[i-]));
f[i]:=min(f[i],f[i-]+calc(a[i],b[i-])+calc(a[i-],b[i])+calc(a[i-],b[i-]));
f[i]:=min(f[i],f[i-]+calc(a[i],b[i-])+calc(a[i-],b[i-])+calc(a[i-],b[i]));
end;
if f[n]=inf then writeln(-) else writeln(f[n]);
end.
bzoj1237的更多相关文章
- 【BZOJ1237】配对(贪心,DP)
题意:有n个a[i]和b[i],调整顺序使abs(a[i]-b[i])之和最小,但a[i]<>b[i].保证所有 Ai各不相同,Bi也各不相同. 30%的数据满足:n <= 104 ...
- BZOJ1237: [SCOI2008]配对
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1237 题目大意:你有n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一 ...
- bzoj千题计划179:bzoj1237: [SCOI2008]配对
http://www.lydsy.com/JudgeOnline/problem.php?id=1237 如果没有相同的数不能配对的限制 那就是排好序后 Σ abs(ai-bi) 相同的数不能配对 交 ...
随机推荐
- 修改sqlplus提示符
如图所示 : 修改 提示符为 username(sid_serial#)@instance_name ,这样其实很方便的 以下是步骤 在11g中glogin.sql 文件是不存在的,取而代之的是 lo ...
- 游标、type使用示例
declare my_cur sys_refcursor; --定义游标变量 type v_record is record( --定义 record类型 obj_id number, ...
- Js编码和Java后台解码
1.java.将resultMsg 转为utf-8 (1) resultMsg = URLEncoder.encode(resultMsg, "utf-8"); (2) new S ...
- iTerm 使用expect实现自动远程登录,登录跳板机
#!/usr/bin/expect set timeout 10 spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2] exp ...
- Java文件操作 读写操作
一.Java读取文件 案例1:读取D盘的1.txt文件 编码: File file = new File("D:/1.txt"); FileReader fr = new File ...
- OpenGL中的深度、深度缓存、深度测试及保存成图片
1.深度 所谓深度,就是在openGL坐标系中,像素点Z坐标距离摄像机的距离.摄像机可能放在坐标系的任何位置,那么,就不能简单的说Z数值越大或越小,就是越靠近摄像机. 2.深度缓冲区 深度缓冲区原理就 ...
- leetcode problem 11 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- jquery解析XML(1)
jquery解析XML文件 html代码 <!DOCTYPE html><html><head><title>解析XML</title>&l ...
- 【Asp.Net】小BUG汇总[更新]
目录结构 1.Dictionary<T>遍历 2.Asp.net网站部署在C盘无法上传下载文件 3.Asp.Net网站发布后远程无法访问 4.GDI+中发生一般性错误 1.Dictiona ...
- mysql统计表的大小
如下是sql语句: SELECT TABLE_NAME as name,DATA_LENGTH+INDEX_LENGTH as len,TABLE_ROWS as rows FROM informat ...