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 ...
随机推荐
- 【iOS】Quartz2D信纸条纹
一.前导程序 新建一个项目,在主控制器文件中实现以下几行代码,就能轻松的完成图片在视图中的平铺. - (void)viewDidLoad { [super viewDidLoad]; UIImage ...
- 使用Jsoup解析html网页
一. JSOUP简介 在以往用java来处理解析HTML文档或者片段时,我们通常会采用htmlparser(http://htmlparser.sourceforge.net/)这个开源类库.现在 ...
- Durandal介绍
Durandal是一个JS框架用于构建客户端single page application(SPAs).它支持MVC,MVP与MVVM前端构架模式.使用RequireJS做为其基本约定层,D ...
- sublimeCodeIntel 的配置
在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...
- 我为什么要做富文本编辑器【wangEditor5个月总结】
请访问wangEditor官网:www.wangEditor.com ----------------------------------------------------------------- ...
- 关于字符串replace方法第二个参数探究
网上有关replace的文章很多了,这里主要聊聊它的第二个参数.阅读本文需要对replace方法有一定了解.W3school=>replace 我们要把一段字符串中的某些指定字符替换掉,第一时间 ...
- SET UPDATE TASK LOCAL
SET Effect Switches on the local update task. This means that when you specify CALL FUNCTION ... IN ...
- 升级sp1后文档无法编辑
现象: A problem occurred while connecting to the server. If the problem continues, contact your admini ...
- 转:使用Nlog记录日志到数据库
原文:http://www.cnblogs.com/Gyoung/archive/2012/10/18/2729613.html Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台, ...
- java多线程系列8-线程的优先级
在java中设置线程优先级使用setPriority,在jdk中的源代码如下: public final void setPriority(int newPriority) { ThreadGroup ...