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

读入之后先排序。

询问的时候可以o(m)效率得到答案。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<stack>
#include<map>
#include<algorithm>
using namespace std;

struct X
{
    int m;
    int a[10000+10];
}s[60];
int n;

struct Arr
{
    int f;
    int num;
}h[200000+10];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i].m);
        for(int j=1;j<=s[i].m;j++) scanf("%d",&s[i].a[j]);
        sort(s[i].a+1,s[i].a+1+s[i].m);
        s[i].a[s[i].m+1]=0x7FFFFFFF;
    }
    int k; scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        int A,B; scanf("%d%d",&A,&B);
        int z1=1,z2=1;
        int tot=0;
        while(1)
        {
            if(tot==s[A].m+s[B].m) break;
            if(s[A].a[z1]<s[B].a[z2])
            {
                h[++tot].f=1;
                h[tot].num=s[A].a[z1];
                z1++;
            }
            else if(s[A].a[z1]>s[B].a[z2])
            {
                h[++tot].f=2;
                h[tot].num=s[B].a[z2];
                z2++;
            }
            else
            {
                h[++tot].f=1;
                h[tot].num=s[A].a[z1];
                z1++;
            }
        }
        h[0].num=-1;
        int nc=0,nt=0;
        for(int i=1;i<=tot;i++) if(h[i].num!=h[i-1].num) nt++;
        int pos=1;
        while(1)
        {
            if(pos>tot) break;
            int yuan;
            for(int i=pos;i<=tot;i++)
            {
                if(h[i].num==h[pos].num) yuan=i;
                else break;
            }

            if(h[pos].f!=h[yuan].f) nc++;
            pos=yuan+1;
        }
        printf("%.1lf%%\n",1.0*nc/nt*100);
    }
    return 0;
}
时间: 2024-11-05 14:36:22

PAT (Advanced Level) 1063. Set Similarity (25)的相关文章

pat advanced level 1063

1063 Set Similarity (25)(25 分) Given two sets of integers, the similarity of the sets is defined to be N~c~/N~t~*100%, where N~c~ is the number of distinct common numbers shared by the two sets, and N~t~ is the total number of distinct numbers in the

Pat(Advanced Level)Practice--1063(Set Similarity)

Pat1063代码 题目描述: 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

PAT (Advanced Level) 1083. List Grades (25)

简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT (Advanced Level) 1021. Deepest Root (25)

先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[20000]; int n,sz

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

PAT (Advanced Level) 1114. Family Property (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { int id; int Father,Mother; int k;

PAT (Advanced Level) 1085. Perfect Sequence (25)

可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; long long k; long l

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

PAT (Advanced Level) 1109. Group Photo (25)

简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> #include<iostream> using namespace std