CodeForces 620D Professor GukiZ and Two Arrays 双指针
Professor GukiZ and Two Arrays
题解:
将a数组都sort一遍之后, b数组也sort一遍之后。
可以观察得到 对于每一个ai来说, 整个数组bi是一个V型的。
并且对于ai+1的最优解一定是在ai的右边。
然后我们将a数组 和 b数组枚举一遍。
然后再将a数组22组合, b数组22组合之后, 再枚举一遍。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 4e6 + ;
int a[N], b[N];
struct Node{
int v, l, r;
bool operator<(const Node & t) const{
return v < t.v;
}
}A[N], B[N];
LL ans;
pll ansa, ansb;
LL suma = , sumb = ;
int f;
LL cal(int i, int j){
LL tsuma = suma - A[i].v + B[j].v;
LL tsumb = sumb - B[j].v + A[i].v;
return abs(tsuma - tsumb);
}
void Find(int n, int m){
if(!n || !m) return ;
sort(A+, A++n); sort(B+, B++m);
for(int i = , j = ; i <= n; ++i){
while((j+) <= m && cal(i,j) >= cal(i,j+)) ++j;
if(ans > cal(i,j)){
ans = cal(i, j);
ansa = pll(A[i].l, B[j].l);
ansb = pll(A[i].r, B[j].r);
}
}
}
int main(){
int n, m;
scanf("%d", &n);
f += (n==);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
A[i] = {a[i], i, };
suma += a[i];
}
scanf("%d", &m);
for(int i = ; i <= m; ++i){
scanf("%d", &b[i]);
B[i] = {b[i], i, };
sumb += b[i];
}
ans = abs(suma - sumb); ansa = ansb = {, };
Find(n, m);
int atot = , btot = ;
for(int i = ; i <= n; ++i){
for(int j = i+; j <= n; ++j){
A[++atot] = {a[i]+a[j], i, j};
}
}
for(int i = ; i <= m; ++i){
for(int j = i+; j <= m; ++j){
B[++btot] = {b[i]+b[j], i, j};
}
}
Find(atot, btot);
printf("%lld\n", ans);
if(ansa.fi == ){
puts("");
}
else if(ansb.se == ){
puts("");
printf("%d %d\n", ansa.fi, ansa.se);
}
else {
puts("");
printf("%d %d\n", ansa.fi, ansa.se);
printf("%d %d\n", ansb.fi, ansb.se);
}
return ;
}
CodeForces 620D Professor GukiZ and Two Arrays 双指针的更多相关文章
- codeforces 620D Professor GukiZ and Two Arrays
#include <bits/stdc++.h> using namespace std; + ; const long long inf = 1e18; int n, m; long l ...
- 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 D. Professor GukiZ and Two Arrays
Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...
- 【CodeForces 620D】Professor GukiZ and Two Arrays
题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...
- CodeForces 620A Professor GukiZ's Robot
水题 #include<cstdio> #include<cstring> #include<cmath> #include<stack> #inclu ...
- 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 ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【74.89%】【codeforces 551A】GukiZ and Contest
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 551 C GukiZ hates Boxes
--睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
随机推荐
- 如何确定FPGA电路中DDR4的Speed bin 是否兼容?
原创 by DeeZeng DDR4 是否兼容,拿更快速度的DDR4,是否可以不改FPGA工程,直接换料就能直接用? 实际工作中,经常会碰到因为DDR3/4 或其他料件换料了,需要判断FPGA工程中I ...
- Activity 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- codeforces1088D_Ehab and another another xor problem交互题
传送门 一道考验思维的交互题 大致思路就是从最高的二进制位向下询问 代入例子比如: 5 6 6 5 7 4 6 4 讨论一下 交互题的重点学会推理和归纳 #include <bits/stdc+ ...
- kali,ubuntu, debain DNS 配置
kali 是基于 debain 的一个 Linux 发行版 DNS 的配置 是在文件 /etc/resolv.conf 下. 但是,我们会发现 /etc/resolv.conf 每次重启都会失效, ...
- mysql 查询结果显示行号
mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...
- hadoop学习(五)----HDFS的java操作
前面我们基本学习了HDFS的原理,hadoop环境的搭建,下面开始正式的实践,语言以java为主.这一节来看一下HDFS的java操作. 1 环境准备 上一篇说了windows下搭建hadoop环境, ...
- Css3动画效果,彩色文字效果,超简单的loveHeart
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Cs ...
- Java——异常处理
1.java提供的异常不可能预见所有的问题,所以需要自己定义异常类,必须从已有的异常类继承,最好选择意思相近的异常类继承. class MyException extends Exception{} ...
- android ——活动
活动(Activity)主要用于和用户进行交互,是一种可以包含用户界面的组件. 1.手动创建活动 右击com.example.administrator.exp5→New→Activity→Empty ...
- Powered by .NET Core 进展:第5次发布尝试(Windows部署)
(图注:Windows 自带的性能监控,红色表示 CPU 占用,绿色表示 QPS) 今天中午 12:30 左右,我们进行了 .NET Core 博客站点的第5次发布(页脚会显示"Powere ...