朋友
来源:互联网 发布:三国志9优化 编辑:程序博客网 时间:2024/05/01 15:16
【问题描述】
mxy 即将前往新世界。
在前往新世界的过程中,ta 遇见了两种人。一种是只和 lowb 做朋友,即当且仅当自己的能力值大于对方时他们会成为朋友,另一种是大神我们做朋友,即当且仅当自己的能力
值小于对方时他们会成为朋友。
现在告诉你两种人的能力值。请你计算一共有多少对友好关系。
【输入】
每组测试数据有三行。
第一行有两个整数 m,n(1 <= m,n <= 20000),分别代表前者人数和后者人数。
第二行 m 个数,第三行 n 个数,代表各自的能力值。
【输出】
一行一个整数,表示有多少对友好关系。
【输入输出样例】
friend.in
5 3
8 1 7 3 1
3 6 1
friend.out
7
程序:
var
m,n,i,j,q,p:longint;
tj:int64;
a,b:array[0..20000]of longint;
procedure kp1(l,r:longint);
var
i,j,mid:longint;
begin
if l>=r then exit;
i:=l;j:=r;mid:=a[(l+r) div 2];
repeat
while a[i]>mid do inc(i);
while a[j]<mid do dec(j);
if i<=j then
begin
a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
inc(i);dec(j);
end;
until i>j;
kp1(l,j);
kp1(i,r);
end;
procedure kp2(l,r:longint);
var
i,j,mid:longint;
begin
if l>=r then exit;
i:=l;j:=r;mid:=b[(l+r) div 2];
repeat
while b[i]>mid do inc(i);
while b[j]<mid do dec(j);
if i<=j then
begin
b[0]:=b[i];b[i]:=b[j];b[j]:=b[0];
inc(i);dec(j);
end;
until i>j;
kp2(l,j);
kp2(i,r);
end;
begin
readln(m,n);
for i:=1 to m do
read(a[i]);
readln;
for i:=1 to n do
read(b[i]);
kp1(1,m);
kp2(1,n);
tj:=0;
for i:=1 to m do
begin
q:=1;p:=n;
if b[q]<a[i] then
begin
inc(tj,n);
continue;
end;
if b[p]>=a[i] then break;
while a[i]<=b[q] do inc(q);
if a[i]>b[q] then
begin
tj:=tj+(n-q+1);
continue;
end;
if a[i]>b[p] then
begin
tj:=tj+(n-p+1);
continue;
end;
end;
write(tj);
end.
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友?
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- heap corruption detected错误解决方法调试方法以及内存管理相关
- SVN的具体使用过程1【资料+操作+截图】仅服务器端
- 插入排序
- [LeetCode
- Gallery 显示缩略图流程
- 朋友
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- java基础之反射
- .gitignore 配置
- C#之设计模式之六大原则(转载)
- 使用jquery实现动态加载片段
- JAVA-20-数据约束、数据库设计三大范式、多表查询、存储过程、触发器、数据库权限
- 1-1 Linux系统安装
- 侦察兵