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 ...
随机推荐
- Spring管理 hibernate 事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- js的alert和confirm美化
--前言-- window对象的alert和confirm标准方法在不同浏览器的显示效果不太相同,有个相同点是都不是很美观.我们的想法是使用js和css分别仿照它们,提供另一套函数,使在不同浏览器的有 ...
- js 操作ASP.NET服务器控件
js 操作ASP.NET服务器控件 在ASP.NET中使用js时,js获取DOM元素时,经常获取不到,这是因为获取的方法有误,现在介绍一方法,解决如何使用js获取ASP.NET控件在浏览器端生成htm ...
- windows 8 设置hyper-v网络设置
1 windwos 8 设置hyperv 比较简单,和装操作系统都不多做解释.我只多说说网络的设置问题,因为可能装提windows 2008虚拟机,根据网上设置网络的方式都是要不然只能虚拟 机上网 , ...
- UDF2
问题 根据给定的gps点point(x,y)和北京的shape数据,关联出 AOI ID IO 输入 gps点表 create table gps ( x double, //经度 y double ...
- android 回调函数二:应用实例
前言:如果对android回调的概念不明白的请看:android 回调函数一:基本概念 1.定义接口 package com.app.util; public interface ZYJCallBac ...
- 安卓开发_浅谈Android动画(二)
在学习了四个基本动画之后,现在要学习一些更有用的效果 先给出所有的动画xml <?xml version="1.0" encoding="utf-8"?& ...
- Android源码分析之Looper
先来说说summary,Looper就是用来在某个线程中跑一个message loop.一个线程默认是没有message loop与之相关联的, 为了创建一个你必须在这个线程中调用Looper.pre ...
- iOS中 @synthesize 和 @dynamic 区别
OC object-c 为了让java的开发者习惯 使用.的操作,所以可以将接口类中的变量 使用@property来声明属性.但是在.h中声明的属性,必须在.m中使用@synthesize或者@dyn ...