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\),问最多能抽出多少个数, ...
随机推荐
- Java多线程---------同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期
1.问题的引出 class MyThread implements Runnable{ private int ticket = 5 ; // 假设一共有5张票 public void run(){ ...
- RHCA学习笔记:RH442-Unit5 队列原理
NIT 5 Queuing Theory 队列原理 目标: 1.明白性能调优的关键术语 2. 应用队列技术解决性能问题 3.明白性能调优的复杂性 5.1 Introd ...
- careercup-数学与概率 7.6
7.6 在二维平面上,有一些点,请找出经过点数最多的那条线. 解法: 类似于leetcode:Max Points on a Line 我们只需在任意两点之间“画”一条无限长的直线(也即不是线段),并 ...
- How does CCFileUTils::fullPathForFilename work
OverView The purpose of this document is show you how does function CCFileUtils::fullPathForFilename ...
- android使用模拟机测试时,若要联网IP地址该怎么写?
android使用模拟机测试时,如果服务器也是本机,那么IP地址如果写为localhost或者127.0.0.1,这样其实是不能访问到本机上部署的服务端,那么该怎么写呢?很简单,把IP地址改为10.0 ...
- Demo1_iOS9网络适配_改用更安全的HTTPS
iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一 使用TLS 1.2 SSL.采用TLS 1.2 协议,目的是 强制增强数据访问安全,而且 系统 Foundation ...
- WPF 自定义路由事件 与 附加路由事件
为student添加附件事件
- rndc: connect failed: 127.0.0.1#953: connection refused
[root@localhost sbin]# ./named -v bind 9.5.1-p3-v3.0.9 问题现象: [root@localhost sbin]# ./rndc flush -p ...
- Sql触发器脚本
ALTER Trigger [dbo].[test] --新建触发器 On [dbo].[test1] --在test1表中创建触发器 for insert --触发条件 As --事件触发后所要做的 ...
- MVC小系列(十一)【Html.BeginForm与Ajax.BeginForm】
Html.BeginForm与Ajax.BeginForm都是mvc的表单元素,前者是普通的表单提交,而后者是支持异步的表单提交,直接用mvc自带的Ajax.BeginForm就可以很容易完成一个异步 ...