简单搜索练习(一)二分入门 HDU 2141 Can you find it?POJ 2785 4 Value whose Sum is 0
2017-11-25 01:01
501 查看
简单搜索练习(一):二分入门
最近事情比较多,忙各类社团的事情。再加上班里的一些事务,算法一直没怎么复习。一直想复习一下动态规划也就只看了一下书,前几天全校的排位赛挂机50min+心态差点崩掉,线段树、概率DP啥的一脸懵逼。学长一直给我们拉题做,说学好动态规划要先练好搜索。所以就一直开搜索专题。
一、二分搜索入门:
PS:二分的题目常见的就一般是二分查找存在性与二分答案两种
1、 HDU 2141 Can you find it?
Time Limit: 10000/3000 MS (Java/Others)
Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 33365
Accepted Submission(s): 8282
Problem 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
题意
给你三个数组A,B,C,每个数组中含有N个数字,有M次询问,
每次给定一个X,问是否存在Ai+Bi+Ci=X。
Solution
看到题目估算一下暴力的时间复杂度500*500*500*1000,超过1e10,估计暴力过不了。一般来说,搜索的第一个优化就是二分。思路如下,先求出Ai+Bi然后sort一下,对于每一个X,二分查找X-Ci。
另一种方法是hash,本人太弱,把控不好
Attention
对于初学二分的人来说,二分的终止条件是最容易弄错的。有的时候需要进行一定的调整,不能死记模板。
代码如下:
PS:对于二分查找来说最重要的是要有序,sort非常关键。
2、POJ 2785 4 Values whose Sum is 0
Time Limit: 15000MS
Memory Limit: 228000K
Accepted: 7472
Case Time Limit: 5000MS
Description
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
Source
Southwestern Europe 2005
题意
将上一题的三个数组变成了四个数组。上一个题不太会的可以那这个来练一下手。
Solution:
分别将A、B与C、D分别合成两个大数组,枚举其中一个再二分查找。本题的终止条件与上题不同。因为要找一共多少组,所以不要只找到一组就停止。
最近事情比较多,忙各类社团的事情。再加上班里的一些事务,算法一直没怎么复习。一直想复习一下动态规划也就只看了一下书,前几天全校的排位赛挂机50min+心态差点崩掉,线段树、概率DP啥的一脸懵逼。学长一直给我们拉题做,说学好动态规划要先练好搜索。所以就一直开搜索专题。
一、二分搜索入门:
PS:二分的题目常见的就一般是二分查找存在性与二分答案两种
1、 HDU 2141 Can you find it?
Time Limit: 10000/3000 MS (Java/Others)
Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 33365
Accepted Submission(s): 8282
Problem 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
题意
给你三个数组A,B,C,每个数组中含有N个数字,有M次询问,
每次给定一个X,问是否存在Ai+Bi+Ci=X。
Solution
看到题目估算一下暴力的时间复杂度500*500*500*1000,超过1e10,估计暴力过不了。一般来说,搜索的第一个优化就是二分。思路如下,先求出Ai+Bi然后sort一下,对于每一个X,二分查找X-Ci。
另一种方法是hash,本人太弱,把控不好
Attention
对于初学二分的人来说,二分的终止条件是最容易弄错的。有的时候需要进行一定的调整,不能死记模板。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[503],c[250003]; //二分查找 t 这里的二分查找条件有很多种 bool fi(int t,int l,int r) { int mid; while(l<r-1)//注意终止条件 { mid=(l+r)/2; if(c[mid]<=t) l=mid; else r=mid; } //不同的终止条件有不同的对应 if(c[l]==t||c[r]==t) return true; else return false; } int main() { int t,k=0,size,l,n,m,b,size1,s; bool su; while(scanf("%d%d%d",&l,&n,&m)!=EOF) { printf("Case %d:\n",++k); size=0; for(int i=1;i<=l;i++) scanf("%d",&a[i]); //将A、B合并为一个 for(int i=1;i<=n;i++) { scanf("%d",&b); for(int j=1;j<=l;j++) c[++size]=b+a[j]; } sort(c+1,c+1+size); for(int i=1;i<=m;i++) scanf("%d",&a[i]); scanf("%d",&s); for(int i=1;i<=s;i++) { su=false;//判断是否查找成功 scanf("%d",&b); for(int j=1;j<=m;j++) if(fi(b-a[j],1,size)) { printf("YES\n"); su=true; break; } if(!su) printf("NO\n"); } } return 0; }
PS:对于二分查找来说最重要的是要有序,sort非常关键。
2、POJ 2785 4 Values whose Sum is 0
Time Limit: 15000MS
Memory Limit: 228000K
Accepted: 7472
Case Time Limit: 5000MS
Description
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
Source
Southwestern Europe 2005
题意
将上一题的三个数组变成了四个数组。上一个题不太会的可以那这个来练一下手。
Solution:
分别将A、B与C、D分别合成两个大数组,枚举其中一个再二分查找。本题的终止条件与上题不同。因为要找一共多少组,所以不要只找到一组就停止。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; long a[4003],b[4003],c[4003],d[4003]; long a1[16000003],a2[16000003]; long size,sum; //二分查找开始 void fi(long s) { long l=1,r=size,mid; while(l<r)//条件改变,如果用原来的条件会出错,卡掉部分解 { mid=(l+r)/2; if(a2[mid]+s<0) l=mid+1;//边界的更改也相应改变 else r=mid; } //查找相同的解有多少个,因为优先查找的比较小的所以l++ while(a2[l]+s==0&&l<=size) { sum++; l++; } } int main() { int n; while(scanf("%d",&n)!=EOF) { size=0; sum=0; for(int i=1;i<=n;i++) scanf("%ld%ld%ld%ld",&a[i],&b[i],&c[i],&d[i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a1[++size]=a[i]+b[j],a2[size]=c[i]+d[j]; sort(a1+1,a1+1+size);//有序化a1 sort(a2+1,a2+1+size);//有序化a2 for(int j=1;j<=size;j++) fi(a1[j]); printf("%ld\n",sum); } return 0; }
相关文章推荐
- POJ - 2785 4 Values whose Sum is 0(二分搜索)
- HDU - 2141:Can you find it?(二分搜索)
- poj 2785 4 Values whose Sum is 0 (简单二分)
- CSU-ACM2017暑假集训2-二分搜索 poj-2785-4 Values whose Sum is 0
- HDU-2141 Can you find it? 简单二分
- POJ 2785 4 Values whose Sum is 0(双向搜索+二分)
- POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)
- hdu杭电 2141 Can you find it? 【二分 N*logN】
- HDU 2141 Can you find it?【二分】
- POJ 2785 A Values whose Sum is 0 ( 拆合+排序+二分 )
- HDU - 2141 Can you find it?(二分)
- HDU 2141 Can you find it?(数组找数字,二分)
- 文章标题HDU 2141:Can you find it?(二分)
- 文章标题POJ 2785:4 Values whose Sum is 0?(二分)
- POJ - 2785 : 4 Values whose Sum is 0(二分、STL上下界函数)
- POJ 2785 4 Values whose Sum is 0(二分+折半枚举)
- HDU 2141 Can you find it?(二分)
- POJ 2785 4 Values whose Sum is 0(排序+二分搜索)
- 【HDU】-2141-Can you find it?(二分)
- poj 2785 4 Values Whose Sum is 0 --- 二分