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. 二叉树可视化--Graphviz

    大家平时写C程序有没有种把内存里的数据结构全给画出来的冲动呢?数据量小的话,画起来还蛮简单,用viso,我前面的文章都用viso画的.之前写红黑树代码的时候,用的是命令行把整个树打印出来,不过只是一些 ...

  2. 四种方案解决ScrollView嵌套ListView问题

    在工作中,曾多次碰到ScrollView嵌套ListView的问题,网上的解决方法有很多种,但是杂而不全.我试过很多种方法,它们各有利弊. 在这里我将会从使用ScrollView嵌套ListView结 ...

  3. VMware虚拟机安装Linux英文改中文系统并更新yum安装输入法

    今天用VMware Workstation11时,出现了个"难题",在选择了系统盘后(我用iso镜像centos6.5)VMware会提示高速安装,安装后系统是英文版,并且找不到系 ...

  4. 对Cookie进行增删改查

    public class CookieServletDemo extends HttpServlet { public void doGet(HttpServletRequest request, H ...

  5. 获取java项目 classpath目录

    this.getClass().getResource("/").getPath(); 从根目录获取载入文件: this.getClass().getResourceAsStrea ...

  6. iOS之GCDAsyncSocket第三方库的使用

    Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通信的双发,即客户端(Clien ...

  7. PHP 获取当前日期的上个月的日期

    获取当前日期的上个月的日期 <?php /** *参考有: *http://www.oschina.net/code/snippet_96541_4015 *http://stackoverfl ...

  8. R-大数据分析挖掘(5-R基础回顾)

    (一)R函数 R是一种解析型语言,输入后可直接获取结果 函数(输入参数,参数) R的函数分为“高级”和“低级函数” • 高级函数可调用低级函数 • 高级函数称为泛型函数 • 函数名  <-­‐ ...

  9. 20151225jquery学习笔记---编辑器插件

    编辑器(Editor),一般用于类似于 word 一样的文本编辑器,只不过是编辑为 HTML格式的.分类纯 JS 类型的,还有 jQuery 插件类型的.一. 编辑器简介我们使用的 jQuery 版本 ...

  10. 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY

    [转载]http://blog.csdn.net/w87875251l/article/details/7929657 不允许对索引显式地使用 DROP INDEX,该索引正用于 UNIQUE KEY ...