首页 > ACM题库 > HDU-杭电 > hdu 2578 Dating with girls(1)-分治-[解题报告]C++
2014
02-10

hdu 2578 Dating with girls(1)-分治-[解题报告]C++

Dating with girls(1)

问题描述 :

Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ‘ IQ, The girls make a problem, and the boys who can solve the problem
correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!

输入:

The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.

输出:

The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.

样例输入:

2
5 4
1 2 3 4 5
8 8
1 4 5 7 8 9 2 6

样例输出:

3
5

做了Dating with girls(2),强迫症犯了,所以一定要把这道题也做了。

排序,删除重复元素,然后二分查找一遍。水题。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 1000005
int a[N],n,k;
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int Find(int x)
{
    int l,r;
    l=1;r=n;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(a[mid]<x) l=mid+1;
        else if(a[mid]>x) r=mid-1;
        else return 1;
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        qsort(a+1,n,sizeof(a[0]),cmp);
        int j=1;
        a[j++]=a[1];
        for(int i=2;i<=n;i++)
            if(a[i]!=a[i-1]) a[j++]=a[i];
        n=j-1;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int temp;
            if(a[i]*2==k)
            {
                ans++;
                continue;
            }
            temp=k-a[i];
            if(Find(temp)) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

解题转自:http://blog.csdn.net/zizaimengzhongyue/article/details/14104891


  1. 阿努纳奇人对 他们在睁一只眼闭一只眼,因为不对他们的目的(人类的劳作和贡献)做出影响就好,其实阿努纳奇人也不是残暴,只是当人类是白老鼠,地球是实验室也是监狱,人类意思觉醒是他们不像看到的,那样会害怕他们不继续为金钱劳动(资源) 。但他们很爱他们的家园,

  2. 阿努纳奇人对 他们在睁一只眼闭一只眼,因为不对他们的目的(人类的劳作和贡献)做出影响就好,其实阿努纳奇人也不是残暴,只是当人类是白老鼠,地球是实验室也是监狱,人类意思觉醒是他们不像看到的,那样会害怕他们不继续为金钱劳动(资源) 。但他们很爱他们的家园,

  3. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  4. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  5. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  6. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }