Codeforces Round #697 (Div. 3) C 容斥原理

C. Ball in Berland

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.

Each class must present two couples to the ball. In Vasya’s class, aa boys and bb girls wish to participate. But not all boys and not all girls are ready to dance in pairs.

Formally, you know kk possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.

For example, if a=3a=3, b=4b=4, k=4k=4 and the couples (1,2)(1,2), (1,3)(1,3), (2,2)(2,2), (3,4)(3,4) are ready to dance together (in each pair, the boy’s number comes first, then the girl’s number), then the following combinations of two pairs are possible (not all possible options are listed below):

  • (1,3)(1,3) and (2,2)(2,2);
  • (3,4)(3,4) and (1,3)(1,3);

But the following combinations are not possible:

  • (1,3)(1,3) and (1,2)(1,2) — the first boy enters two pairs;
  • (1,2)(1,2) and (2,2)(2,2) — the second girl enters two pairs;

Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.

Input

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.

The first line of each test case contains three integers aa, bb and kk (1≤a,b,k≤2⋅1051≤a,b,k≤2⋅105) — the number of boys and girls in the class and the number of couples ready to dance together.

The second line of each test case contains kk integers a1,a2,…aka1,a2,…ak. (1≤ai≤a1≤ai≤a), where aiai is the number of the boy in the pair with the number ii.

The third line of each test case contains kk integers b1,b2,…bkb1,b2,…bk. (1≤bi≤b1≤bi≤b), where bibi is the number of the girl in the pair with the number ii.

It is guaranteed that the sums of aa, bb, and kk over all test cases do not exceed 2⋅1052⋅105.

It is guaranteed that each pair is specified at most once in one test case.

Output

For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.


小蒟蒻来写题解了。。。。

**题目大意就是:**给你k个二元组,从中挑出来2个,且对应元素不相等的个数

思路解

看各位大佬说的,就是容斥原理。一看是这么一会事。

反向思维,满足条件的不好求那就求不满足的。

我们枚举每个二元组,这时候当第i个二元组的时候,我们假设他与后面没有枚举到的二元组全部可以组成答案,那么就是n - i个。但是题意要求我们需要不能让对应位置相等。那么我们就需要将现有的答案来减去后面的二元组包含此时二元组中男生和女生的个数,但会可能多减一次后面与此时枚举的二元组相同的情况,所以需要加回来

求解

我们用一个数组A来存某个男人出现了多少次,同理B存女人

用一个map来求二元组相同情况的时候的个数

记得要用ll

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctype.h>
#include <algorithm>
#include <cstring>
#include <map>
#define ll long long

using namespace std;

typedef pair<int,int>p;

map<p, int> m;  //存每一对的男女关系


int aa[200000 + 10];
int bb[200000 + 10];
int A[200000 + 10]; //存某个男的在组合中出现了多少次
int B[200000 + 10]; //存某个女的在组合中出现了多少次

int n;

int main(){
    int t;
    cin >> t;
    ll a, b, k;

    while(t--){
        cin >> a >> b >> k;
        for (ll i = 1; i <= k; i++){
            cin >> aa[i];
        }
        for (ll i = 1; i <= k; i++){
            cin >> bb[i];
        }

        for (ll i = 1; i <= k; i++){
            m[p(aa[i],bb[i])]++;
            A[aa[i]]++;
            B[bb[i]]++;
        }
        ll ans = 0;
        for (ll i = 1; i <= k; i++){ //细节:可以不用到k,到k还咋枚举下一个
            m[p(aa[i],bb[i])]--;
            A[aa[i]]--;
            B[bb[i]]--; // 避免算上当前枚举的自己这个,以及后面不要被前面算过的所干扰   
            ans += k - i - A[aa[i]] - B[bb[i]] + m[p(aa[i],bb[i])]; // 不要想当然以为这个aa[i]是每个人,他指的是包含有这aa[i]编号的人的对数有多少
        }
        printf("%lld\n", ans);
        m.clear(); //!!!!!注意归零清空
        memset(A, 0, sizeof(A));
        memset(B, 0, sizeof(B));
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值