【HDU】 5101 Select 二分搜索 upper_bound

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. (T≤20)
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 ( 0≤n≤1000 ), k( 0≤k<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( 0≤m≤100 ), vi

Output
For each test case, output a single integer.

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

Sample Output
5

Source
BestCoder Round #17

如果用以下代码就超时

#include<stdio.h>
#include<algorithm>
using namespace std;
int T,N,k,a[1001][101],v[100010];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&k);
        for(int n,i=0;i<N;i++)
        {
            scanf("%d",&n);a[i][0]=n;//记录个数n 
            for(int j=1;j<=n;j++)
                scanf("%d",&a[i][j]);
            sort(a[i]+1,a[i]+n+1);
        }
        int ans=0;
        for(int i=0;i<N;i++)
            for(int j=1;j<=a[i][0];j++)
                for(int x=i+1;x<N;x++)
                    ans+=a[x][0]-(upper_bound(a[x]+1,a[x]+a[x][0]-1,k-a[i][j])-a[x]-1); 
        printf("%d\n",ans);
    }
    return 0;
}

三个for,如何去掉一个?直接看代码

#include<stdio.h>
#include<algorithm>
using namespace std;
int T,N,k,a[1005][105],v[100100];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&k);int t=0;
        for(int n,i=0;i<N;i++)
        {
            scanf("%d",&n);a[i][0]=n;//记录个数n 
            for(int j=1;j<=n;j++)
                scanf("%d",&a[i][j]),v[t++]=a[i][j];
            sort(a[i]+1,a[i]+n+1);
        }
        sort(v,v+t);
        int temp;long long ans=0;
        for(int i=0;i<N;i++)
            for(int j=1;j<=a[i][0];j++)
            {
                if(j==1||a[i][j]!=a[i][j-1]) //减少重复 
                    temp=v+t-upper_bound(v,v+t,k-a[i][j])-(a[i]+1+a[i][0]-upper_bound(a[i]+1,a[i]+1+a[i][0],k-a[i][j]));
                ans+=temp;
            }
        printf("%lld\n",ans/2);
    }
    return 0;
}

对了还有一个要注意到的地方 答案可能超出int范围,用long long

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值