【CodeForces 620D】Professor GukiZ and Two Arrays
题意
两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值、最少交换次数、交换数对
分析
交换0次、1次可得到的最小的差可以枚举出来。
交换两次,如果枚举就超时了。
我们预处理把第一个数列两两组合的所有情况存储起来为u数组,并且按照大小排序,接着在另一个数列里枚举两个数后,用二分的方法,求交换后使得 差的绝对值最小 的u。
二分查找最接近的值可以用lower_bound函数。
代码
#include<stdio.h>
#include<cmath>
#include<map>
#include<utility>
#define N 2005
#define ll long long using namespace std;
ll a[N],b[N],n,m,suma,sumb,v,ans,c; map<ll,pair<int,int> >u;
map<ll,pair<int,int> >::iterator it;
pair<int,int>swap1,swap2; void update(int i,int j)
{
if(abs(c-it->first) < v)
{
ans=;
v = abs(c - it->first);
swap1 = it->second;
swap2 = {i,j};
}
} int main()
{
scanf("%lld",&n);
for(int i=; i<=n; i++)
{
scanf("%lld",&a[i]);
suma+=a[i];
} scanf("%lld",&m);
for(int i=; i<=m; i++)
{
scanf("%lld",&b[i]);
sumb+=b[i];
} v=abs(suma-sumb);
if(!v)
{
printf("0\n0\n");
return ;
} for(int i=; i<n; i++)
for(int j=i+; j<=n; j++)
u[(a[i]+a[j])*2LL] = {i,j}; for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
if(abs(suma-sumb-2LL*(a[i]-b[j]))<v)
{
ans=;
v=abs(suma-sumb-2LL*(a[i]-b[j]));
swap1= {i,j};
}
for(int i=; i<m; i++)
for(int j=i+; j<=m; j++)
{
c=suma-sumb+2LL*(b[i]+b[j]);
it=u.lower_bound(c);
if( it != u.end() )
update(i,j);
if( it != u.begin() )
{
it--;
update(i,j);
}
} printf("%lld\n%lld\n",v,ans); if(ans==)printf("%d %d\n",swap1.first,swap1.second);
if(ans==)printf("%d %d\n%d %d\n",swap1.first,swap2.first,swap1.second,swap2.second); return ;
}
2017.7.21 ps. 今天再做这题,wa了八下。因为自己写的二分,一开始忘记考虑n==1的情况(这样就不存在a数组里选两个加起来了,二分的结果没有意义)。之后发现我二分查找的是加了绝对值的,显然不需要加绝对值。以及如果二分查找的值不存在,那么得到的就是大于它的第一个值,所以还要考虑小于它的第一个值。最终AC的代码如下:
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
#define N 2001
int n,m,a[N],b[N];
struct Node{
ll a,b,v;
}sum[N*N];
ll sa,sb;
int x[],y[];
int cnt;
ll ans;
bool cmp(const Node& a,const Node& b){
return a.v<b.v;
}
int bs(ll s){
int l=,r=cnt;
while(l<r){
int mid=l+r>>;
if(sum[mid].v<s)
l=mid+;
else
r=mid;
}
return l;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",a+i),sa+=a[i];
scanf("%d",&m);
for(int i=;i<=m;++i)
scanf("%d",b+i),sb+=b[i]; for(int i=;i<n;++i)
for(int j=i+;j<=n;++j)
sum[++cnt]=(Node){i,j,2LL*(a[i]+a[j])}; ans=abs(sb-sa); sort(sum+,sum++cnt,cmp);
int k=; for(int i=;i<=n;++i)
for(int j=;j<=m;++j){
ll t=abs(sa-a[i]*-sb+b[j]*);
if(t<ans){
ans=t;
k=;
x[]=i;y[]=j;
}
}
if(cnt){
for(int i=;i<m;++i)
for(int j=i+;j<=m;++j){
ll t=sa-sb+2LL*(b[i]+b[j]);
int d=bs(t);
for(int g=-;g<;++g)if(d+g>&&d+g<=cnt){
ll tans=abs(t-sum[d+g].v);
if(tans<ans){
ans=tans;
k=;
x[]=sum[d+g].a;y[]=i;
x[]=sum[d+g].b;y[]=j;
}
}
}
}
printf("%lld\n%d\n", ans,k);
for(int i=;i<k;++i)
printf("%d %d\n",x[i],y[i]);
return ;
}
【CodeForces 620D】Professor GukiZ and Two Arrays的更多相关文章
- CodeForces 620D Professor GukiZ and Two Arrays 双指针
Professor GukiZ and Two Arrays 题解: 将a数组都sort一遍之后, b数组也sort一遍之后. 可以观察得到 对于每一个ai来说, 整个数组bi是一个V型的. 并且对于 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...
- 【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 ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 757B】 Bash's Big Day
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- Hadoop 一二事(1) - 简单介绍与杂谈
大数据大数据,身边很多朋友都在谈大数据,Big Data!!! 到底是什么,用来干嘛的,也很少有人说得出一二,那今天开始就简单说说这一二事吧 hadoop 的来源:是作者女儿的一个玩具 - 一只黄色的 ...
- Jenkins遇到问题二:Jenkins服务器磁盘空间管理策略
Jenkins在帮助我们自动化构建服务的同时也在消耗服务器的磁盘空间,试想如果构建的项目个数很多,而Jenkins 服务器磁盘空间又不是非常大的话,每隔一段时间磁盘空间就会爆满导致Jenkins出现磁 ...
- Jython概要
1.安装jython 1.1 进入http://www.jython.org/downloads.html ,网页上会显示当前最稳定的版本(The most current stable releas ...
- WPF在XAML中Binding使用StringFormat属性
1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...
- C语言 百炼成钢5
//题目13:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 //本身.例如:153是一个“水仙花数”,因为153 = 1的三次方+5的三次方+3的三次方. #de ...
- 在ASP.Net和IIS中删除不必要的HTTP响应头
引入 每次当浏览器向Web服务器发起一个请求的时,都会伴随着一些HTTP头的发送.而这些HTTP头是用于给Web服务器提供一些额外信息以便于处理请求.比如说吧.如果浏览器支持压缩功能,则浏览器会发送A ...
- ScrollView 简单出错
ScrollView can host only one direct child 主要是ScrollView内部只能有一个子元素,即不能并列两个子元素,所以需要把所有的子元素放到一个LinearLa ...
- 简便的自动布局,对UIStackView的个人理解!
序言: 更新了很久的Linux,我怕朋友们都视觉疲劳了,今天就更新在学ios开发时候,对一些知识点的理解.希望各位会喜欢! 正文: UIStackView 类提供了一个高效的接口用于平铺一行或一列的视 ...
- Django实际站点项目开发经验谈
开发了两个月的Django站点正式上线了,看着网站从无到有,从前端到后台,从本地开发到环境部署,一点一滴的堆砌成型,着实带给我不小的乐趣. Django站点介绍: 开发环境:阿里云服务器centos6 ...
- windows API 开发飞机订票系统 图形化界面 (一)
去年数据结构课程设计的作品,c语言实现,图形化界面使用windows API实现. 首发在我csdn博客:http://blog.csdn.net/u013805360/article/details ...