【题目链接】:http://hihocoder.com/contest/offers15/problem/1

【题意】

【题解】



把3个数组的元素全都合并在一个数组里面;

(当然你要记录每个数字原本是在哪一个数组里面的)

然后排个序;

找到原本a,b,c数组中的元素第一次出现的位置

pos[1],pos[2],pos[3];

然后按照公式直接计算

D=?

然后把,pos[1],pos[2],pos[3]中最小的那个pos[i]

令pos[i]=nex(pos[i]);

然后再重新计算D=?

重复上述步骤取最小的D就好;

贪心吧

相邻的肯定差最小;

看看是哪3个构成了最后的答案而已;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const LL MOD = 1e9+7;
const int N = 3*(1e5+100); struct abc
{
int id,val;
}; int n,m,l,cnt,pos[4],ans;
abc a[N]; bool cmp1(abc a,abc b)
{
return a.val < b.val;
} int nex(int x)
{
int r = pos[x];
pos[x] = n+1;
rep1(i,r+1,n)
if (a[i].id==x)
{
pos[x] = i;
break;
}
return pos[x];
} int fi()
{
int ju = 1;
if (pos[2]<pos[ju]) ju = 2;
if (pos[3]<pos[ju]) ju = 3;
return ju;
} int getd()
{
return abs(a[pos[1]].val-a[pos[2]].val)+
abs(a[pos[2]].val-a[pos[3]].val)+
abs(a[pos[3]].val-a[pos[1]].val);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> m >> l;
rep1(i,1,n) cin >> a[++cnt].val,a[cnt].id = 1;
rep1(i,1,m) cin >> a[++cnt].val,a[cnt].id = 2;
rep1(i,1,l) cin >> a[++cnt].val,a[cnt].id = 3;
n = cnt;
sort(a+1,a+1+n,cmp1);
rep1(i,1,3) pos[i] = nex(i);
ans = getd();
while (1)
{
int mi = fi();
pos[mi] = nex(mi);
if (pos[mi]==n+1) break;
ans = min(ans,getd());
}
cout << ans << endl;
return 0;
}

最新文章

  1. 如何写出高质量的技术博客 这边文章出自http://www.jianshu.com/p/ae9ab21a5730 觉得不错直接拿过来了 好东西要大家分享嘛
  2. Java中关于先有鸡还是先有蛋的问题----Class&amp;Object
  3. 在Win10系统中关闭Hyper-V
  4. 初学git(一):创建本地“仓库”
  5. 如何在windows 10 x64安装佳能 CP900 驱动
  6. AngularJS Best Practices: pretty urls
  7. 通过代码自定义cell 新浪微博页面显示
  8. C# 中的事件含义介绍
  9. Notepad++加上xml格式化的功能
  10. 【Linux.Python】Python进程后台启动
  11. Android API Level在11前后及16之后时Notification的不同用法
  12. 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性
  13. STM8建立IAR工程
  14. Java中整形、浮点、字符之间的转换
  15. js经典闭包
  16. iOS SDAutoLayout图文混排-共享
  17. Spring学习之路一
  18. 从壹开始前后端分离 42 ║支持多种数据库 &amp; 快速数据库生成
  19. 怎么让table中的&lt;td&gt;内容向上对齐
  20. Java 输入/输出——Java虚拟机读写其它进程的数据

热门文章

  1. spring JdbcTemplate在spring的ioc中使用
  2. day38 09-Spring类的完整生命周期及后处理Bean
  3. javaScript中的事件对象event是怎样
  4. 【python之路21】用户登陆程序函数
  5. web前端学习(二)html学习笔记部分(2)-- 改良的元素(input元素等等)
  6. Centos 下添加开机自启动服务和脚本【转】
  7. Vue源码探究-数据绑定的实现
  8. 计算机组成原理作业一 熟悉MIPS指令
  9. Leetcode905.Sort Array By Parity按奇偶排序数组
  10. 将centos 7 自带的 php 5.4升级为 5.6