1. 程式人生 > >HDU 2141 Can you find it?(找三個數的和)

HDU 2141 Can you find it?(找三個數的和)

Description

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

這個題目也提交了好多次

第一種方案是把2個數組的和用set來存,然後對每一個,在第3個數組裡面二分查詢。

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;

int bs(int key,int a[],int length)
{
	int lo = 0, hi =length;
	int mi;
	while (lo <= hi)
	{
		mi = ((hi - lo) >> 1) + lo;//lo和hi比較大的時候相加可能爆int
		if (a[mi] == key)
return mi; else if (a[mi]<key) lo = mi + 1; else hi = mi - 1; } return -1;//未找到 } int main() { int cas = 1; while (1) { int l, m, n; cin >> l >> m >> n; int *listl = new int[l]; int *listm = new int[m]; int *listn = new int[n]; for (int i = 0; i < l; i++)cin >> listl[i]; sort(listl, listl + l); for (int i = 0; i < m; i++)cin >> listm[i]; for (int i = 0; i < n; i++)cin >> listn[i]; set<int>se; for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)
se.insert(listm[i] + listn[j]);
		set<int>::iteratorit;
		int s, sum;
		cin >> s;
		cout << "Case " << cas << ":" << endl;
		for (int i = 0; i < s; i++)
		{			
			cin >> sum;
			int temp = 0;
			for (it = se.begin(); it != se.end(); it++)
			{
				if (bs(sum - *it, listl,l-1) >= 0)
				{
					temp = 1;
					break;
				}
			}
			if (temp)cout << "YES";
			else cout << "NO";
			cout << endl;
		}
		cas++;
	}
	return 0;
}

但是結果超時了。

仔細一想,其實用set用處不大,因為25萬個int數,根本不會有太多重複的。

所以還是按照學長PPT裡面說的那種,2個數組求和,遍歷第三個陣列。

當然了,計重數就不用了。

#include<iostream>
#include<algorithm>
using namespace std;

int bs(int key,int a[],int length)
{
	int lo = 0, hi =length;
	int mi;
	while (lo <= hi)
	{
		mi = ((hi - lo) >> 1) + lo;//lo和hi比較大的時候相加可能爆int
		if (a[mi] == key) return mi;
		else if (a[mi]<key) lo = mi + 1;
		else hi = mi - 1;
	}
	return -1;//未找到
}

int main()
{
	int cas = 1;
	while (1)
	{
		int l, m, n;
		cin >> l >> m >> n;
		int *listl = new int[l];
		int *listm = new int[m];
		int *listn = new int[n];
		int *sum = new int[m*n];
		for (int i = 0; i < l; i++)cin >> listl[i];
		for (int i = 0; i < m; i++)cin >> listm[i];
		for (int i = 0; i < n; i++)
		{
			cin >> listn[i];
			for (int j = 0; j < m; j++)sum[i + j*n] = listn[i] + listm[j];
		}
		sort(sum, sum + m*n);
		int s, su;
		cin >> s;
		cout << "Case " << cas << ":" << endl;
		for (int i = 0; i < s; i++)
		{			
			cin >> su;
			int temp = 0;
			for (int i = 0; i < l;i++)
			{
				if (bs(su - listl[i], sum,m*n-1) >= 0)
				{
					temp = 1;
					break;
				}
			}
			if (temp)cout << "YES";
			else cout << "NO";
			cout << endl;
		}
		cas++;
		delete sum;
	}
	return 0;
}
不過,還是超時了。

這個時候,我開始感覺,可能是因為二分查詢的函式需要改進。

於是把二分查詢的函式變成了

int bs(int key, int a[], int length)
{
int lo = 0, hi = length;
if (key<a[0] || key>a[length])return -1;
int mi;
while (lo <= hi)
{
mi = ((hi - lo) >> 1) + lo;//lo和hi比較大的時候相加可能爆int
if (a[mi] == key) return mi;
else if (a[mi]<key) lo = mi + 1;
else hi = mi - 1;
}
return -1;//未找到
}
}

這個結果時候又變成了Output Limit Exceeded

實在找不出來哪裡輸出錯了,就百度了一下Output Limit Exceeded

剛好看到一個帖子http://bbs.csdn.net/topics/320153052

裡面說要儘量把while(1)這種的改成while(cin>>a)這種的

雖然以前在csuoj裡面用while(1)沒有出現這種問題,不過還是試了一下,結果真的AC了。

相關推薦

HDU 2141 Can you find it?個數

Description 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 th

HDU 2141 Can you find it? 二分

input int you time chm source tis 去掉 註意 題目鏈接: Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Ja

HDU-2141-Can you find it?二分

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2141 Problem Description Give you three sequences of numbers A, B, C, then we give you a

HDU 2141 Can you find it?(二分)

clas i++ clu div cpp style [] href ++ 題目鏈接:clicl here~~ 【題目大意】: Give you three sequences of numbers A, B, C, then we give you a number

HDU 2141 can you find it 二分

cnblogs algorithm main 依次 樸素 合數 ret void style 1.題意:給出三組數,給出一個數X,試問是否能從三個數組中各選一個數,A,B,C使得X=A+B+C 2.分析:題設的輸入數據為先是依次給出三組數的長度L、N、M,下面三行給出三組數

HDU 2143 Can you find it?基礎二分

i++ php http miss then represent ise for stream Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (

HDU 2141 Can You Find It?(二分)

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others) Total Su

hdu 2141 Can you find it?

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 numbe

hdoj-2141-Can you find it?

style spa sin ble AS ace () href binary 題目鏈接 1 /* 2 Name:HDU-2141-Can you find it? 3 Copyright: 4 Author: 5 D

Can you find it? 二分

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

HDU - 2141Can you find it?

sizeof sin case uniq utf first ted 整數 代碼 Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calcul

hdu2141 can you find it?分治第一場基礎演算法

第一場基礎演算法題目連結hdu2141 //o(n^3)複雜度,肯定炸掉啦,所以想個簡單法 //把前倆數組合並,反正就是合併倆,至於哪倆先不管 //然後用一個新陣列存一下和,快排排序一下, //因為新數組裡面元素最多,就把他用來二分 #include<cstdio>

HDU-1796 How many integers can you find容斥原理

                            How many integers can you find      

HDU 4027 Can you answer these queries?線段樹區間開方

sizeof sqrt .cn swap %d nes nts following clr Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 6576

[容斥原理] hdu 1796 How many integers can you find

pos lcm 一個 每一個 fin memset 而不是 std != 題意: 給一個N。然後給M個數,問1~N-1裏面有多少個數能被這M個數中一個或多個數整除。 思路: 首先要N-- 然後對於每一個數M 事實上1~N-1內能被其整除的 就是有(N-1)/

HDU 1796 How many integers can you find (容斥)

ace return algo 整除 ctype hdu scanf ++ main 題意:給定一個數 n,和一個集合 m,問你小於的 n的所有正數能整除 m的任意一個的數目。 析:簡單容斥,就是 1 個數的倍數 - 2個數的最小公倍數 + 3個數的最小公倍數 + ...(

HDU 4027 Can you answer these queries?線段樹/區間不等更新

push battle mark put action light blog acc lang 傳送門 Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU 4027 Can you answer these queries? 線段樹+暴力

題意: 給出一段序列和兩種操作,第一種操作,將x,y區間的數均開平方,第二種操作,對x,y區間進行求和。 分析: 一開始還不敢用線段樹做,因為純線段樹下來根暴力列舉複雜度差不了多少,但由於開方,所以在很少次的迴圈裡就能達到1,所以就可以直接這麼做了。但題目沒有說名忍耐值的範圍,要是0太多

HDU-2199 Can you solve this equation?二分

                           Can you solve this equation?       &

HDU-2199-Can you solve this equation?二分

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2199 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can