(找数字)HDU - 2141

本文针对HDU-2141算法题目进行了解析,介绍了一种通过预处理和二分查找来降低时间复杂度的有效解法。通过对输入数据的巧妙处理,大幅提升了算法效率。

HDU - 2141

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
Sample Output
Case 1:
NO
YES
NO


题意:A,B,C三个集合,再给出一个数X,判断是否  Ai+Bj+Ck = X;

思路:代码可能很好写,超时才是坑点,看代码;

//注意时间复杂度 
#include<stdio.h>
#include<algorithm>
using namespace std;
int d[3000000];//m*n的范围 
int main()
{
	int a[505],b[505],c[505];
	int m,n,k,i,j,t,p,h,w=1,r;
	while(scanf("%d %d %d",&m,&n,&k)!=EOF)
	{
	      for(i=0;i<m;i++)
		    scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		    scanf("%d",&b[i]);
		for(i=0;i<k;i++)
		    scanf("%d",&c[i]);
		r=0;
		for(i=0;i<m;i++)
		    for(j=0;j<n;j++)
		        d[r++]=a[i]+b[j];//算出a[i]和b[j]的和,减少复杂度的关键一步 ,避免后续代码循环
		sort(d,d+r);//一次排序代替a,b,c三个数组的排序,避免后续代码循环  
		scanf("%d",&t);
		printf("Case %d:\n",w++);
		while(t--)
		{    
		    bool flag=true;
			scanf("%d",&h);
			for(i=0;i<k;i++)//由于上面做出的优化,这里只需要一次循环就可以了 
			{
			    int left=0,right=r-1,mid;
		    	    while(right>=left)//二分的关键应用 
		    	    {
		    	     	mid=(left+right)/2;
		    	        if(c[i]+d[mid]==h)//a,b,c三个数组同 h 进行比较 
		    	        {
		    	        	flag=false;
		    	        	break;
						}
						else if(c[i]+d[mid]>h)
						{
							right=mid-1;
						}
						else if(c[i]+d[mid]<h)
						{
							left=mid+1;
						}
					}
					if(!flag)
					break;
			}  
			if(!flag)
		          printf("YES\n");
		      else 
		          printf("NO\n");
		}
	}
	return 0;
}

### 关于HDU-1443问题的Java实现 针对HDU平台上的题目ID为1054的问题,在64位整数输入输出格式方面应采用`%lld`,而Java类名需指定为主类名为`Main`[^1]。 对于解决该问题的一个实例是在处理两个数组中寻重复数字的任务时。在Java方法内部可以利用`duplication[0]`来替代C/C++里的指针形式`*duplication`,这使得可以在返回布尔值的同时获取所需的数值[^2]。 下面给出一段适用于此场景下的Java代码片段: ```java import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); HashSet<Integer> set = new HashSet<>(); while (scanner.hasNextInt()) { int num = scanner.nextInt(); if (!set.add(num)) { // 如果add失败,则说明已经存在这个元素 System.out.println("Duplicate number found: " + num); break; // 或者继续收集所有的重复项取决于具体需求 } } scanner.close(); } private static boolean duplicate(int numbers[], int length, int duplication[]) { if (numbers == null || length <= 0) return false; for (int i = 0; i < length; ++i) { while (numbers[i] != i) { if (numbers[i] == numbers[numbers[i]]) { duplication[0] = numbers[i]; return true; } else { // Swap elements to their correct positions. int temp = numbers[numbers[i]]; numbers[numbers[i]] = numbers[i]; numbers[i] = temp; } } } return false; } } ``` 这段程序首先定义了一个`HashSet`用于存储读取到的数据,并通过尝试向集合中添加新元素的方式来检测是否存在重复数据;一旦发现有重复即刻打印并停止进一步操作。另外还提供了一个辅助函数`duplicate()`用来判断给定数组内是否有任何重复元素,并将到的第一个重复值存入传入参数`duplication[]`的第一位置上以便调用方访问。 需要注意的是上述代码仅作为示例展示如何运用特定技巧解决问题,并不一定完全符合原题目的所有约束条件以及边界情况处理,请根据实际题目描述调整逻辑细节以满足要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值