hdoj 5101 Select 【二分 + 思维】



Select

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1455    Accepted Submission(s): 409


Problem Description
One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can't get good result without teammates.
So, he needs to select two classmates as his teammates. 
In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu's IQ is a given number k. We use an integer v[i] to represent the IQ of the ith classmate. 
The sum of new two teammates' IQ must more than Dudu's IQ.
For some reason, Dudu don't want the two teammates comes from the same class.
Now, give you the status of classes, can you tell Dudu how many ways there are.
 

Input
There is a number T shows there are T test cases below. ( T20)
For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n (  0n1000 ), k(  0k<231 ).
Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer v[i], which means there is a person whose iq is v[i] in this class. m(  0m100 ), v[i](  0v[i]<231 )
 

Output
For each test case, output a single integer.
 

Sample Input
 
       
1 3 1 1 2 1 2 2 1 1
 

Sample Output
 
       
5
 



题意:给你一个数k和n个集合,让你从两个不同的集合里面选两个数使它们的和大于k,问有多少种方案。


直接暴力时间复杂度O(n*n*m*m),适当优化后O(n*n*m),显然不可取。


思路:把所有集合并为一个集合b[],然后把所有集合a[1]...a[n] 和 b[]升序排列。

枚举n个集合里面的所有元素,假设枚举集合a[i]。 设集合b[]元素个数top,集合a[i]元素个数为num[i]。


考虑元素a[i][j]的贡献,我们求出temp = k - a[i][j] + 1,意味着 达到或者超过temp的元素均满足。

那么在b[]里面查找>=temp的第一个元素位置p2,满足条件的元素显然有top - p2 + 1个。由于top - p2 + 1个元素里面可能包含有集合a[i]自身的元素需要除去。我们只需在集合a[i]里面查找>=temp的第一个元素位置p1,满足条件的元素有num[i] - p1 + 1个。这样得到其它集合满足条件的元素为 p2 - p1。

枚举所有元素后,由于同一组(x, y)我们算了两次,最后除去2就可以了。时间复杂度O(nmlog(nm))。



AC代码:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 500000+10
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
using namespace std;
int num[1100];
LL a[1100][110];
LL b[100100];
int Find(LL *s, LL val, int l, int r)
{
    int ans;
    while(r >= l)
    {
        int mid = (l + r) >> 1;
        if(s[mid] >= val)
        {
            ans = mid;
            r = mid-1;
        }
        else
            l = mid+1;
    }
    return ans;
}
int main()
{
    int t, kcase = 1;
    Ri(t);
    W(t)
    {
        int n; LL k;
        Ri(n); Rl(k);
        int top = 0;
        for(int i = 1; i <= n; i++)
        {
            Ri(num[i]);
            for(int j = 1; j <= num[i]; j++)
                Rl(a[i][j]), b[++top] = a[i][j];
            sort(a[i]+1, a[i]+num[i]+1);
        }
        sort(b+1, b+top+1);
        LL ans = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= num[i]; j++)
            {
                LL temp = k - a[i][j] + 1;
                int p1, p2;
                if(a[i][num[i]] < temp)//最大的元素不满足 没必要查找
                    p1 = 0;
                else
                    p1 = num[i] - Find(a[i], temp, 1, num[i]) + 1;
                if(b[top] < temp)
                    p2 = 0;
                else
                    p2 = top - Find(b, temp, 1, top) + 1;
                ans += p2 - p1;
            }
        }
        Pl(ans / 2);
    }
    return 0;
}



你可能感兴趣的:(思维,二分)