1063. Set Similarity (25)

时间限制
300 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets,
and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range
[0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated
by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

Sample Output:

50.0%
33.3%
解题思路:
输入数组后,根据输入的数组序号对数组进行排序,然后去重,再进行类似于merge操作的遍历,获得两个数组中不同的数的个数。

#include <iostream>
#include"stdio.h"
#include"stdlib.h"
#include"algorithm"
using namespace std; struct store{
    int a[10000];
    int length;
    bool sort_flag;//标记数组是否已经排序,可以避免重复排序
    int cnt;
};
int main()
{
    int N,M,K;
    struct store s[50];
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d",&M);
        s[i].length = M;
        s[i].cnt=M;
        s[i].sort_flag = false;
        for(int j=0;j<M;j++){
            scanf("%ld",&s[i].a[j]);
        }
    }
    scanf("%d",&K);     int first,second;
    for(int k=0;k<K;k++){
        scanf("%d%d",&first,&second);
        first--;
        second--;
        if(!s[first].sort_flag){
            sort(s[first].a,s[first].a+s[first].length);//快排
            //去重
            for(int i=s[first].length-1;i>=1;i--){
                if(s[first].a[i]==s[first].a[i-1]){
                    s[first].a[i]=-1;
                    s[first].cnt--;//记下数组中distinct的数的个数
                }
            }
            s[first].sort_flag=true;//已经排序了
        }         if(!s[second].sort_flag){
            sort(s[second].a,s[second].a+s[second].length);
            for(int i=s[second].length-1;i>=1;i--){
                if(s[second].a[i]==s[second].a[i-1]){
                    s[second].a[i]=-1;
                    s[second].cnt--;
                }
            }
            s[second].sort_flag=true;
        }
        int distinct=0;
        int i=0,j=0;
        while(i<s[first].length&&j<s[second].length){
            if(s[first].a[i]!=-1&&s[second].a[j]!=-1){
                if(s[first].a[i]<s[second].a[j]){
                    distinct++;
                    i++;
                }
                else if(s[first].a[i]>s[second].a[j]){
                    distinct++;
                    j++;
                }
                else{
                    i++;
                    j++;
                }
            }else{
                if(s[first].a[i]==-1&&s[second].a[j]!=-1){
                    i++;
                }else if(s[first].a[i]!=-1&&s[second].a[j]==-1){
                    j++;
                }
                else /*if(s[first].a[i]!=-1&&s[second].a[j]!=-1)*/{//用else if会超时
                    i++;
                    j++;
                }
            }
        }
        while(i<s[first].length){
            if(s[first].a[i]!=-1)
                distinct++;
            i++;
        }
        while(j<s[second].length){
           if(s[second].a[j]!=-1)
                distinct++;
            j++;
        }         long common = (s[first].cnt+s[second].cnt-distinct)/2;
        printf("%.1f%\n",(common*100.0/(common+distinct)));
    }
    return 0;
}


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. 【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 ...

  3. 1063 Set Similarity (25分)

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

  4. PAT 1063 Set Similarity (25)

    题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...

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

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

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

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

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

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

  8. PAT 1063 Set Similarity[比较]

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

  9. PAT 1063. Set Similarity

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

随机推荐

  1. 3D建模与处理软件简介

    [前言]自半年前笔者发表博客“什么是计算机图形学”以来,时常有人来向笔者询问3D模型的构建方法与工具.笔者的研究方向是以3D技术为主,具体包括3D建模,3D处理及3D打印三个方面,在3D建模与处理方面 ...

  2. [liferay6.2]input-date日期控件

    input-date日期控件 liferay6.2中默认提供了一个简单的日期控件input-date,调用代码片段如下: <% Calendar calendar = Calendar.getI ...

  3. Vue入门笔记#数据绑定语法

    #数据绑定语法# #文本: 数据绑定的基础表型形式,使用“Mustache”语法(双大括号)(特意查了一下Mustache同“moustache”释义为:髭:上唇的胡子,小胡子,最起码我觉得挺形象的) ...

  4. Jmeter测试环境搭建(一)

    一.工具描述 Apache JMeter是    100%的java桌面应用程序.它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件,Java Servlets,Java 对象,数据库,F ...

  5. RTTI (Run-Time Type Identification,通过运行时类型识别) 转

    参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.   RTTI提供了以下两个 ...

  6. DOM--2 创建可重用的对象

    对象中包含的 分类(内置对象: 注意这些都是function:除了Function对象,实例都是object) Function对象 Function实例function 构造函数的function ...

  7. CSS3-html,样式与样式表的创建,选择器

  8. xml基本操作和保存配置文件应用实例

    引言:在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.本文首先介绍了xml的基本操作,后面写了一个经常用到的xml保存配置文件的实例. xml常用方法: 定义 ...

  9. HDU2191(多重背包)

    #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> ...

  10. webpack练手项目之easySlide(三):commonChunks(转)

    Hello,大家好. 在之前两篇文章中: webpack练手项目之easySlide(一):初探webpack webpack练手项目之easySlide(二):代码分割 与大家分享了webpack的 ...