HDU 2578 - Dating with girls(1)

35 篇文章 0 订阅
3 篇文章 0 订阅

传送门HDU 2578 - Dating with girls(1)

说好的用二分的,题目看着看着我就用以前的方法了。等下再试试二分。


这个题目和哈希专题里面的一题几乎一样,事实证明我看别人的解题报告还是没有白看的。。

开一个vis数组,防止重复计算。

代码挺好理解的,就不说了。


1.土办法。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 110000;

int num[MAXN];
int xvis[MAXN];

int main()
{
    //freopen("input.txt", "r", stdin);
    int T, i, j, x, y, n, k, cnt;
    scanf("%d", &T);
    while (T--)
    {
        cnt = 0;
        memset(xvis, 0, sizeof(xvis));
        scanf("%d%d", &n, &k);
        for (i = 0; i < n; i++)
            scanf("%d", &num[i]);\
        sort(num, num + n);
        x = n - 1, y = 0;
        while (true)
        {
            if (num[x] + num[y] == k)
            {
                if (!xvis[num[x]])
                {
                    cnt++;
                    xvis[num[x]] = 1;
                    x--, y++;
                }
                else
                    x--, y++;
            }
            else if (num[x] + num[y] < k)
                y++;
            else
                x--;
            if (x < 0 || y >= n)
                break;
        }
        printf("%d\n", cnt);
    }
    return 0;
}


2.二分手写版


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 110000;

int num[MAXN];
int xvis[MAXN];

bool BinarySearch(int value, int n)
{
    int m, x = 0, y = n;
    while (x < y)
    {
        m = x + (y - x) / 2;
        if (num[m] == value)
            return true;
        else if (num[m] > value)
            y = m;
        else
            x = m + 1;
    }
    return 0;
}


int main()
{
    //freopen("input.txt", "r", stdin);
    int T, i, j, x, y, n, k, cnt;
    scanf("%d", &T);
    while (T--)
    {
        cnt = 0;
        memset(xvis, 0, sizeof(xvis));
        scanf("%d%d", &n, &k);
        for (i = 0; i < n; i++)
            scanf("%d", &num[i]);
        sort(num, num + n);
        for (i = 0; i < n; i++)
        {
            if (BinarySearch(k - num[i], n))
            {
                if (xvis[num[i]])
                    continue;
                else
                {
                    cnt++;
                    xvis[num[i]] = 1;
                }
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}


3.STL二分。。。。。(纯凑版面)


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 110000;

int num[MAXN];
int xvis[MAXN];

int main()
{
	//freopen("input.txt", "r", stdin);
	int T, i, j, x, y, n, k, cnt;
	scanf("%d", &T);
	while (T--)
	{
		cnt = 0;
		memset(xvis, 0, sizeof(xvis));
		scanf("%d%d", &n, &k);
		for (i = 0; i < n; i++)
			scanf("%d", &num[i]);
		sort(num, num + n);
		for (i = 0; i < n; i++)
		{
			if (binary_search(num, num + n, k - num[i]))
			{
				if (xvis[num[i]])
					continue;
				else
				{
					cnt++;
					xvis[num[i]] = 1;
				}
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值