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 ...
随机推荐
- 通过GPS数据反向地理信息编码, 得到当前位置信息
检查可用性 这属于基础知识, 不赘述, 总的来说,你的设备的支持要打开, 添加CoreLocation的framework, 引用头文件, 添加委托,然后, 好的实践是在使用前编程检查相关可用性: - ...
- Emmet插件比较实用常用的写法
看了一些关于Emmet插件写法的文档,港真,怎么可以写这么长啊.其实知道几个大概要点加上实践基本就能上手写了啊 杂话 我前面有一篇[今天发现新大陆:haml和Emmet ],其实一开始的想法是写给自己 ...
- C++调用C#dll类库中的方法(非显性COM)
一般在网上搜C++如何调用C#的函数,出来的结果都是做成COM组件,但是这种方法dll安装麻烦,需要注册COM组件,需要管理员权限,调试麻烦,经常需要重启机器,反正有诸多不便. 然后在看<CLR ...
- 详解Paint的setShader(Shader shader)
一.概述 setShader(Shader shader)中传入的自然是shader对象了,shader类是Android在图形变换中非常重要的一个类.Shader在三维软件中我们称之为着色器,其作用 ...
- iOS 学习 - 1.代理传值
代理的目的是改变或传递控制链.允许一个类在某些特定时刻通知到其他类,而不需要获取到那些类的指针.可以减少框架复杂度. 另外一点,代理可以理解为java中的回调监听机制的一种类似 优点:1.避免子类化带 ...
- AFNetworking使用方法
官网下载2.5版本:http://afnetworking.com/ 此文章是基于AFNetworking2.5版本的,需要看AFNetworking2.0版本的请看上一篇文章:AFNetworkin ...
- MFC 网络编程 -- 总结
原文链接:http://www.cnblogs.com/lidabo/archive/2012/07/19/2598734.html 1.基于 TCP 的 socket 编程 /* 服务器端程序流程: ...
- 问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
最近单位组了一台电脑,配置还好,E3大法+R9 280,装了Win7 64位系统. ========================================================== ...
- php基本语法
<?phpecho "hello test01";$var ="PHP";echo "$var";echo "<br ...
- javascript 特效实现(2)——回到顶部效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...