Professor GukiZ and Two Arrays

题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小;输出这个最小的和的差值的绝对值;并且输出交换次数和交换的序号(从1 开始)
Input
5
5 4 3 2 1
4
1 1 1 1
Output
1
2
1 1
4 2

策略:

若是只交换一次,直接O(n^2)暴力即可;但是里面可以交换两次。。若是分开看。。没思路。那就开始时就预处理出同一个序列中任意两个位置的数的和,这就还是转换为了一次交换。一个序列任意两元素之和的个数已经到了O(n^2),那在处理的时候使用贪心策略(看代码就知道了),是O(n^2);

ps:原本1e9数量级的范围相加减是不会爆int的范围,但是我就是坑在这个上了。。强制改成了2ll*。。。(求解);

//483ms
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for((i) = 0;i < (n);i++)
#define all(vec) (vec).begin(),(vec).end()
typedef long long ll;
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
vector<pair<int,PII> > va,vb;
int a[],b[];
PII ans[];
int main()
{
int na,nb,i,j,tot = ;
ll sum = ;
cin>>na;
rep(i,na){
scanf("%d",a + i);
sum += a[i];
rep(j,i) va.pb({a[j]+a[i],{j,i}});
}
cin>>nb;
rep(i,nb){
scanf("%d",b + i);
sum -= b[i];
rep(j,i) vb.pb({b[j]+b[i],{j,i}});
}
ll mn = abs(sum);
ll dif;
rep(i,na){
rep(j,nb){
dif = sum - 2ll*a[i] + 2ll*b[j]; //要分开*2再-+;
if(mn > abs(dif)){
mn = abs(dif);
tot = ;
ans[] = {i,j};
}
}
}
sort(all(va)); sort(all(vb));
for(i = ,j = ;i < va.size() && j < vb.size();){
dif = sum - 2ll*va[i].A + 2ll*vb[j].A; // 改成2ll才A
if(mn > abs(dif)){
mn = abs(dif);
tot = ;
ans[] = {va[i].B.A,vb[j].B.A};
ans[] = {va[i].B.B,vb[j].B.B};
}
if(dif > ) i++;
else j++;
}
printf("%I64d\n",mn);
printf("%d\n",tot);
rep(i,tot)
printf("%d %d\n",ans[i].A+,ans[i].B+);
}

Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays的更多相关文章

  1. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分

    D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...

  2. Educational Codeforces Round 6 A. Professor GukiZ's Robot 水

    A. Professor GukiZ's Robot   Professor GukiZ makes a new robot. The robot are in the point with coor ...

  3. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  4. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  5. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  6. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  8. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  9. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

随机推荐

  1. java的定时器用法

    java定时器的使用 定时器类Timer在java.util包中.使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,设定指定的任务task在 ...

  2. android的Broadcast receiver

    broadcast receiver是用来监听intent的. android大量使用了broadcast receiver,比如:开机.电话打进来.发送消息,手机电量过低 有两种方式注册broadc ...

  3. sed命令详解--转

    1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏幕.具体过程如下 ...

  4. HD1285(拓扑排序)

    package cn.hncu.dataStruct.search.topSort; import java.util.Scanner; public class Hdu1285 { static S ...

  5. G方法的华丽升级

    ThinkPHP长期以来需要通过debug_start.debug_end方法甚至Debug类才能完成的功能,3.1版本中被一个简单的G方法取代了,不可不谓是一次华丽升级.G方法的作用包括标记位置和区 ...

  6. 【转】数据库SQL优化大总结之 百万级数据库优化方案

    原帖地址:http://www.cnblogs.com/yunfeifei/p/3850440.html#undefined 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 or ...

  7. VS2015 Cordova Ionic移动开发(一)

    一.Windows环境配置 1.如果已经安装VS2015,打开[工具]-[选项]找到Cordova选项: 运行依赖关系查看器,用来检测开发环境是否完整. 如果检测显示: 那么就是环境配置完成了.可以直 ...

  8. Xcode中实现ARC和MRC混编

    1.在Xcode中打开项目文件 2.选中项目名称 3.在右侧选择build phass 选项卡 4.选择 complite source 选项 5.选择要支持MRC编译的.m文件,双击 6.在弹出的框 ...

  9. 伪Base16的构思和实现

    最近看见了一个迅雷地址,发现将其转换为普通链接的工具后,发现所谓专用地址地址就是原地址前加一个表示迅雷的前缀,后进行Base64编码.查阅Base64编码过程后,突发奇想:能否做一个Base16算法? ...

  10. 数据结构与算法 - OC 实现

    [原创]http://www.cnblogs.com/luoguoqiang1985/ 冒泡排序:通过N-1次对剩余未排序元素中最大(小)元素的上浮来实现排序,上浮过程通过交换相邻元素实现. 选择排序 ...