Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays
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的更多相关文章
- 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 ...
- 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 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
随机推荐
- 理解 MEF
1.它解决什么问题? 考虑下面的需求,甲程序员对外暴露接口,内部提供实现.乙程序员使用甲提供的接口,根据面向接口编程的原则,乙关联一个接口类型的引用.正常情况下,乙要使用甲的实现,必须实例化一个具体对 ...
- 前端JS开发框架-DHTMLX
一:介绍 dhtmlxSuite是一个JavaScript库,提供了一套完整的Ajax -驱动UI组件.我们能够使用dhtmlxSuite构建 简洁界面,快速性能,和丰富用户体验的企业级web应用程序 ...
- oracle db mos文章 翻译系列
http://blog.csdn.net/msdnchina/article/details/38377125
- leecode 每日解题思路 127-Factorial Trailing Zeroes
原题描述: 原题地址: Factorial Trailing Zeroes 题目描述很直接, 给出一个整数N, 求这个N的阶乘后尾有几个零.(要求O(logN)时间复杂度) 个人思路: 一开始,最简单 ...
- MySQL【第三篇】数据类型
一.整型 整型的每一种都有无符号(unsigned)和有符号(signed)两种类型. MySQL数据类型 含义 tinyint(m) 1个字节表示:signed(-128~127):unsigned ...
- 关于Intent的七大属性
原谅我愚昧,Intent七大属性这个概念我也是昨天才接触到,看了一下,都是一些常用的东西,就是没有总结过,那么今天就来简单总结一下. Intent七大属性是指Intent的ComponentName. ...
- Linux基础命令(三)
一.常用命令—文件目录类命令 1.ls 列出指定或默认目录的文件信息 使用形式: ls [选项] [目录名] 实例: $ls $ls –als $ls /home/sq/Desktop $ls ./D ...
- Netbeans7.0完美中文+Consolas字体显示配置(亲测可用)
最近把开发环境从Eclipse迁移到了Netbeans上面.因为Netbeans已经相当优秀,速度快功能也不必Eclipse差,但是一只有 一个问题一直让我对eclipse非常纠结:如果把字体选择为C ...
- Asp.net上传出现“超过了最大请求长度”的问题解决方法
在开发ASP.NET网站后台管理系统时,我们可能会遇到这样的问题:上传大于4M的文件时,会提示错误:错误信息如下: 1.异常详细信息:超过了最大请求长度. 2.引发异常的方法:Byte[] GetEn ...
- A swift Tour(2) Control Flow
Control Flow 用 if 和 switch 来做条件语句,并且用for-in,for,while,和do-while做循环,条件和循环的括号是可以不写的,但是body外面的括号是必须写的 l ...