SSL1125 集合

来源:互联网 发布:小内存win10平板优化 编辑:程序博客网 时间:2024/05/17 04:24

题目大意

给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 109,并且每个集合的元素个数不大于105。我们希望求出A、B之间的关系。

任务 :给定两个集合的描述,判断它们满足下列关系的哪一种:

A是B的一个真子集,输出“A is a proper subset of B”

B是A的一个真子集,输出“B is a proper subset of A”

A和B是同一个集合,输出“A equals B”

A和B的交集为空,输出“A and B are disjoint”

上述情况都不是,输出“I’m confused!”

思路

时间复杂度o(n)。

代码

const  maxn=400319;var  n,m,s:longint;  hx:array [0..maxn] of longint;function try(k:longint):longint;var  t:longint;begin  t:=k mod maxn;  while (hx[t]<>0)and(hx[k]<>k) do    t:=(t+1) mod maxn;  try:=t;end;procedure init;var  t,i:longint;begin  read(n);  for i:=1 to n do    begin      read(t);      hx[try(t)]:=t;    end;end;procedure main;var  i,t:longint;begin  read(m);  for i:=1 to m do    begin      read(t);      if hx[try(t)]<>t then inc(s);    end;end;procedure print;begin  if (n=m) and (s=0) then    begin      write('A equals B');      exit;    end;  if (n>m) and (s=0) then    begin      write('B is a proper subset of A');      exit;    end;  if (m>n) and (n+s=m) then    begin      write('A is a proper subset of B');      exit;    end;  if (s=m) then    begin      write('A and B are disjoint');      exit;    end;  write('I''m confused!');end;begin  init;  main;  print;end.
0 0
原创粉丝点击