HDU 5101 - Select(基本计数 + 二分)

题意

找出两个不同班级的人的智商之和大于我们的组数

思路

可以先找出全部当中智商之和大于我们的组数,再减去相同班级的组数。

一开始用的树状数组,写T了。(写得优雅能过)

后来直接用二分找。因为这么找的话会重复计算一次,所以最后答案要除以2

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. #define BitCountll(x) __builtin_popcountll(x)
  31. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  32. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  33. const double PI = acos(-1.0);
  34. const int INF = 0x3f3f3f3f;
  35. using namespace std;
  36. const int MAXN = 100000 + 10;
  37. const int MOD = 1000007;
  38. typedef pair<int, int> pii;
  39. typedef vector<int>::iterator viti;
  40. typedef vector<pii>::iterator vitii;
  41. //vector<LL> num[MAXN], totClass;
  42. LL num[1010][101], totClass[MAXN];
  43. int ni[1010];
  44. int main()
  45. {
  46. //ROP;
  47. ios::sync_with_stdio(0);
  48. int T, i, j;
  49. cin >> T;
  50. while (T--)
  51. {
  52. LL n, pivot;
  53. int cnt = 0;
  54. cin >> n >> pivot;
  55. for (i = 0; i < n; i++)
  56. {
  57. LL tmp;
  58. cin >> ni[i];
  59. for (j = 0; j < ni[i]; j++)
  60. {
  61. cin >> tmp;
  62. num[i][j] = tmp; totClass[cnt++] = tmp;
  63. }
  64. sort(num[i], num[i] + ni[i]);
  65. }
  66. sort(totClass, totClass + cnt);
  67. LL ans = 0;
  68. for (i = 0; i < n; i++)
  69. {
  70. int curAns = 0;
  71. for (j = 0; j < ni[i]; j++)
  72. {
  73. LL ano = pivot - num[i][j];
  74. ans += cnt - (lower_bound(totClass, totClass + cnt, ano + 1) - totClass);
  75. ans -= ni[i] - (lower_bound(num[i], num[i] + ni[i], ano + 1) - num[i]);
  76. }
  77. }
  78. cout << ans / 2 << endl;
  79. }
  80. return 0;
  81. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值