1475C Ball in Berland (思维)

系统管理员 2021-07-16 15:33 371阅读 0赞

题目

思路:看N为2e5可知复杂度为O(n)或O(nlogn),在这我用两个map分别记录每个男和女各自可以和多少匹配,首先选好一组匹配,那么还可以找出多少组匹配与之组成两组呢?答案为:k - 此组匹配中男生可以匹配女生数 - 此组匹配中女生可以匹配男生数+1(他们自身匹配那条线多匹配了一次)。

在这里插入图片描述Code:

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<algorithm>
  5. #include<memory.h>
  6. #include<cmath>
  7. #define pii pair<int,int>
  8. #define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  9. using namespace std;
  10. typedef long long ll;
  11. const int Max = 5e5 + 5;
  12. int Mod = 1e9 + 7;
  13. int lst[Max];
  14. int men[Max], women[Max];
  15. int main()
  16. {
  17. FAST;
  18. int t;
  19. cin >> t;
  20. while (t--)
  21. {
  22. map<int, int> nan, nv;
  23. int a, b, k;cin >> a >> b >> k;
  24. for (int i = 1;i <= k;i++)
  25. {
  26. int j;cin >> j;
  27. nan[j]++;
  28. men[i] = j;
  29. }
  30. for (int i = 1;i <= k;i++)
  31. {
  32. int j;cin >> j;
  33. nv[j]++;
  34. women[i] = j;
  35. }
  36. ll ans = 0;
  37. for (int i = 1;i <= k;i++)
  38. {
  39. ans += k - nan[men[i]] - nv[women[i]] + 1;
  40. }
  41. cout << ans/2 << endl;
  42. }
  43. }

发表评论

表情:
评论列表 (有 0 条评论,371人围观)

还没有评论,来说两句吧...

相关阅读

    相关 codeforces 25C. Roads in Berland

    有n个城市,每个城市都能到达别的城市,n\n的矩阵表明i到j城市的最短距离,现在要建造一些新的道路,在两个城市之间。问每次建造这些道路之后每两个城市之间的距离之和为多少。 如

    相关 B. Berland Crossword (构造)

    [题目][Link 1] 对于下面这个图可以知道当U涂中间三个时是不会对其它的三种产生限制的,而如果涂了4个则必然会占到L R其中一个相邻的格子,涂了5个必然会占掉相邻的