pat 1064...二分+最简单的容斥...

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>

using namespace std;

int a[55][10005];
int b[55];
int N, M, K;

bool b_search(int key, int arr[], int size)
{
    int l = 0;
    int r = size - 1;
    while ( l <= r )
    {
        int mid = ( l + r ) >> 1;
        if ( arr[mid] == key )
            return true;
        else if ( key < arr[mid] )
            r = mid - 1;
        else
            l = mid + 1;
    }
    return false;
}

float query(int i, int j)
{
    int cnt = 0;
    sort(a[j], a[j] + b[j]);
    sort(a[i], a[i] + b[i]);
    int* end1 = unique(a[i], a[i] + b[i]);
    int* end2 = unique(a[j], a[j] + b[j]);
    int A = end1 - a[i];
    int B = end2 - a[j];
    for (int k = 0; k < A; k++)
        if ( b_search(a[i][k], a[j], B) )
            cnt++;
    return ((float)cnt ) * 100.0 / ( A + B - cnt );
}

int main()
{
//    freopen("1.txt", "r", stdin);
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &b[i]);
        for (int j = 0; j < b[i]; j++)
            scanf("%d", &a[i][j]);

    }

    scanf("%d", &K);
    while ( K-- )
    {
        int x, y;
        scanf("%d%d", &x, &y);
        float ans = query(x - 1, y - 1);
        printf("%.1f%%
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/jiongjiong-mengmeng/p/3333995.html