PAT 1063 Set Similarity (25)
题意:给你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)的更多相关文章
- 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 ...
- 1063. Set Similarity (25)
1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- PAT 1063 Set Similarity[比较]
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...
- 【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 ...
- PAT (Advanced Level) 1063. Set Similarity (25)
读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT甲题题解-1063. Set Similarity (25)-set的使用
题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...
- 【PAT甲级】1063 Set Similarity (25 分)
题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...
- 1063 Set Similarity (25分)
Given two sets of integers, the similarity of the sets is defined to be /, where Nc is the number ...
随机推荐
- java LinkedBlockingQueue和ConcurrentLinkedQueue的区别
实现上看,两者都继承于AbstractQueue,但是ConcurrentLinkedQueue实现了Queue,而LinkedBlockingQueue实现了BlockingQueue,Blocki ...
- 配置文件,环境配置和war报分离,方便生产更改
在生产环境实现配置文件和war包 的分离,为方便在必要的时候进行一定的更改,可以避免修改包,但是需要重启 最初为这样的选择配置,单不知为何未生效,修改为配置2配置方法,但不灵活,待跟进.配置1: &l ...
- 最近提交并被合并的 jDiameter pull request 31 解决的问题
使用过程中发现的问题都提交并合并了,应该会出现在1.7.0版本中: https://github.com/RestComm/jdiameter/pull/31 修复多个超时事件同时发生的问题. 修复B ...
- 再说Play!framework http://hsfgo.iteye.com/blog/806974
这篇帖子的内容我本来想发到 http://www.iteye.com/topic/806660这里的主贴里去的,想挽回被隐藏的命运,但我写完本贴的内容,却发现为时已晚.好吧,我承认,上一个贴的标题容易 ...
- JS基本概念 -- 数据类型(一)
ECMAScript中有5种简单数据类型(也成为基本数据类型): Undefined.Null.Boolean.Number.String; 1种复杂数据类型: Object,Object本质上是由一 ...
- log4net 日志框架的配置
log4net 日志框架的配置——静态文件(一) 添加对log4net程序集的引用 选择程序集文件添加引用即可,需要注意的是需要添加相应程序版本的程序集,如果你的应用是基于.netFramework2 ...
- css三角形的实现
实底三角形: <html> <head> <title></title> <style type="text/css"> ...
- SAP ALV标准范例程序介绍
下面介绍几个学习ALV的SAP自带标准程序实例 1. BALVSD06 : Output flights (simple version + save)这是该系列范例最简单的一个,建议以此入门.使用的 ...
- xCode删除storyboard,新建window并启动
application:didFinishLaunchingWithOptions该函数是应用程序启动之后首次加载页面的函数,删除storyboard之后,需要在这里new出新的window,初始化, ...
- 【Android开发资料分享】自己整理的Android开发资料,非常全面
学习Android以来,不知不觉中收集了大量非常优秀的Android开发资料,一直没有系统的整理,最近抽时间把收藏夹中的资料做了一下整理,现在分享给大家,希望能够帮助到需要的人.这份资料我还会不断的更 ...