HDU 5101 - Select (基本计数)

题意

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

思路

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

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

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

代码

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const int MAXN = 100000 + 10;
const int MOD = 1000007;
 
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
 
//vector<LL> num[MAXN], totClass;
 
LL num[1010][101], totClass[MAXN];
int ni[1010];
 
 
int main()
{
    //ROP;
    ios::sync_with_stdio(0);
    int T, i, j;
    cin >> T;
    while (T--)
    {
        LL n, pivot;
        int cnt = 0;
        cin >> n >> pivot;
        for (i = 0; i < n; i++)
        {
            LL tmp;
            cin >> ni[i];
            for (j = 0; j < ni[i]; j++)
            {
                cin >> tmp;
                num[i][j] = tmp; totClass[cnt++] = tmp;
            }
            sort(num[i], num[i] + ni[i]);
        }
        sort(totClass, totClass + cnt);
        LL ans = 0;
        for (i = 0; i < n; i++)
        {
            int curAns = 0;
            for (j = 0; j < ni[i]; j++)
            {
                LL ano = pivot - num[i][j];
                ans += cnt - (lower_bound(totClass, totClass + cnt, ano + 1) - totClass);
                ans -= ni[i] - (lower_bound(num[i], num[i] + ni[i], ano + 1) - num[i]);
            }
        }
        cout << ans / 2 << endl;
    }
    return 0;
}

Powered by Jekyll and Theme by solid