题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集)。

思路:k有2000,集合大小有10000。先将每个集合排序,对每个询问分别设两个指针指向两个集合的头。设a[i]为指针1的值,b[j]为指针2的值。如果a[i]==b[j],交集加一;如果不相同,值较小的指针向后移一位;每次都要去重,并且并集加一。

代码:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e4+;
int a[][N],cnt[];
int main()
{
int n,k,u,v,i,j;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&cnt[i]);
for(j=;j<=cnt[i];j++)
{
scanf("%d",&a[i][j]);
}
sort(a[i]+,a[i]+cnt[i]+);
}
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&u,&v);
i=,j=;int sum=,ans=;
while(i<=cnt[u]&&j<=cnt[v])
{
if(a[u][i]==a[v][j])
{
ans++;
while(a[u][i+]==a[u][i]&&i<=cnt[u]){
i++;
}
while(a[v][j+]==a[v][j]&&j<=cnt[v]){
j++;
}
i++;j++;
}
else if(a[u][i]<a[v][j])
{
while(a[u][i+]==a[u][i]&&i<=cnt[u]){
i++;
}
i++;
}
else if(a[u][i]>a[v][j]){
while(a[v][j+]==a[v][j]&&j<=cnt[v]){
j++;
}
j++;
}
sum++;
}
if(cnt[u]>=i) sum+=cnt[u]-i+;
if(cnt[v]>=j) sum+=cnt[v]-j+;
printf("%.1f%%\n",100.0*ans/sum);
}
return ;
}

PAT 1063 Set Similarity (25)的更多相关文章

  1. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  2. 1063. Set Similarity (25)

    1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  3. PAT 1063 Set Similarity[比较]

    1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...

  4. PAT 1063. Set Similarity

    1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...

  5. 【PAT】1063. Set Similarity (25) 待改进

    Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...

  6. PAT (Advanced Level) 1063. Set Similarity (25)

    读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...

  7. PAT甲题题解-1063. Set Similarity (25)-set的使用

    题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...

  8. 【PAT甲级】1063 Set Similarity (25 分)

    题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...

  9. 1063 Set Similarity (25分)

    Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number ...

随机推荐

  1. 【Effective Java】1、静态工厂的方式代替构造函数

    使用一个服提供者对进行服务的提供,服务的请求通过不同的提供者提供不同的服务,服务提供者首先必须在工厂中进行注册,然后才可以通过工厂实例化服务 Service.java package cn.xf.cp ...

  2. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance

    http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...

  3. 基于UML项目的分析与设计

    1,概述 项目中需求和设计的文档是必然的,UML工具可以帮助指导我们从不同的角度去看待一个新的系统,并把这个系统分解剖析出来.本篇文章主要讲述的是如何将UML应用到项目的开发工作中,关于如何学习UML ...

  4. js判断用户的浏览器设备是移动端还是pc端

    最近做的一个网站页面中需要根据用户的访问设备的不同来显示不同的页面样式,主要是判断移动设备还是电脑浏览器访问的. 下面给出js判断处理代码,以作参考. <script type="te ...

  5. has_many :through VS has_and_belongs_to_many

    user role has_and_belongs_to_many role.destroy:  关联表user_roles先删除记录,再role删除. has_many :through user. ...

  6. SharePoint 2013 内容部署功能简介

    在之前的项目中,当客户有新的需求的时候,我们通常在测试环境上开发或者实施,然后手动在生产环境再弄一次.当发现内容部署这个东西,才知道这样是多么不合理的.我们可以创建两个网站集,一个用来修改,然后通过计 ...

  7. ArcGIS 10 SP5中文版(ArcGIS10补丁5中文版)

    下载地址:百度网盘下载地址:http://pan.baidu.com/s/1o7qPGhk 来自:http://zhihu.esrichina.com.cn/?/sort_type-new__day- ...

  8. Oracle SQL Tips

    左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...

  9. Jsoup解析Html中文文档

    jsoup 简介Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HTM ...

  10. Android项目结构分析

    andriod项目目录结构如下图: 1. src目录 该目录一个普通的保存java源文件的目录,其和普通java工程中的src目录是一样的. 2. gen目录 此目录用于存放所有由ADT插件自动生成的 ...