hdu 5101 select

来源:互联网 发布:淘宝上哪家大码女装好 编辑:程序博客网 时间:2024/06/05 15:47

Select

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


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
13 11 21 22 1 1
 

Sample Output
5
 

Source
BestCoder Round #17
 

500ms,用了类似尺取的方法。
对于一个排好序的数组a[i]int l = 0,r = size - 1;while(l < r) {    if(a[l] + a[r] >= k) {        r --;m ++;    } else {        ans += m;        l ++;    }}int t = size - l - 1;ans += (1 + t) * t / 2;
这样可以计算出a数组任选两个数大于k的对数。
先找前面和后面一对一对的,然后可以证明重合这一点到后面所有数中任选一对都可以大于k,而且后面计算的部分没有重复,这也可以证明出来


/***********************************************\ |Author: YMC |Created Time: 2014/11/8 19:39:33 |File Name: b.cpp |Description: \***********************************************/#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define mset(l,n) memset(l,n,sizeof(l))#define rep(i,n) for(int i=0;i<n;++i)#define maxx(a) memset(a, 0x3f, sizeof(a))#define zero(a) memset(a, 0, sizeof(a))#define srep(i,n) for(int i = 1;i <= n;i ++)#define MP make_pairconst int inf=0x3f3f3f3f ;const double eps=1e-8 ;const double pi=acos (-1.0);typedef long long ll;using namespace std;int n;ll k;ll da[1005][105];int nn[1005];ll tt[100005];int tot;int main() {//freopen("input.txt","r",stdin);    //freopen("out","w",stdout);    int T;    scanf("%d",&T);    while(T--) {        scanf("%d %I64d",&n,&k);        tot = 0;        rep(i,n) {            scanf("%d",&nn[i]);            rep(j,nn[i]) {                scanf("%I64d",&da[i][j]);                tt[tot ++] = da[i][j];                }            sort(da[i],da[i] + nn[i]);        }        if(n == 1) {            puts("0");            continue;        }        sort(tt,tt+tot);        int l = 0,r = tot - 1;        ll le = 0;        ll ans = 0;        while(l < r) {            if(tt[l] + tt[r] > k) {                r--;                le++;            } else {                ans += le;                l ++;            }        }        ll f = tot - l - 1;        ans += (1 + f) * f / 2;        ll tp = 0;        ll tp1 = 0;        for(int i = 0; i < n; ++i) {            if(nn[i] == 1 || nn[i] == 0) continue;            l = 0;r = nn[i] - 1;            tp = 0;            tp1 = 0;            le = 0;            while(l < r) {                if(da[i][l] + da[i][r] > k) {                    r --;                    le ++;                } else {                    tp += le;                    l ++;                }            }            f = nn[i] - l - 1;            tp += (1 + f) * f / 2;            ans -= tp;         }        printf("%I64d\n",ans);    }return 0;}/*对于一个排好序的数组a[i]int l = 0,r = size - 1;while(l < r) {    if(a[l] + a[r] >= k) {        r --;m ++;    } else {        ans += m;        l ++;    }}int t = size - l - 1;ans += (1 + t) * t / 2;*/



0 0
原创粉丝点击