uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums

题目大意:给出一组数据,再给出m个查询的数字。要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和。

解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值。

代码:

#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;

const int N = 1005;
int s[N];
int n, q;

int min (const int a, const int b) {

	if (a == 0)
		return b;
	return abs(a - q) < abs (b - q)? a: b;
}

int search () {

	int l = 0;
	int r = n - 1;
	int sum = 0;
	while (l <= n - 1 && r >= 0 && l < r) {

		if (s[l] + s[r] < q) {

			sum = min (sum, s[l] + s[r]);
			l++;
		}
		else if (s[l] + s[r] > q) {

			sum = min (sum , s[l] + s[r]);
			r--;
		}
		else
			return s[l] + s[r];
	}
	return sum;
}

int main () {

	int m;
	int t = 0;
	while (scanf ("%d", &n), n) {

		for (int i = 0; i < n; i++)
			scanf ("%d", &s[i]);
		sort (s, s + n);
		scanf ("%d", &m);
		printf ("Case %d:\n", ++t);
		for (int i = 0; i < m; i++) {

			scanf ("%d", &q);
			printf ("Closest sum to %d is %d.\n", q, search ());
		}
	}
	return 0;
}

uva:10487 - Closest Sums(二分查找),布布扣,bubuko.com

时间: 2024-10-06 14:57:48

uva:10487 - Closest Sums(二分查找)的相关文章

UVA 10487 Closest Sums(二分)

UVA 10487 Closest Sums Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple cases. Each

UVA - 10487 - Closest Sums (二分求解)

传送:UVA - 10487 10487 Closest Sums Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple c

uva 10487 Closest Sums (遍历&amp;二分查找&amp;&amp;双向查找)

题目大意:先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i]: 挨个计算每个属于其他数之间的sum,然后排序: 查找时有两种方法:二分查找&&双向查找:当然二分查找的效率比后者高了很多,但是都能AC. 提供一条新思路,并不一定非要用二分. 双向查找: #include<stdio.h> #include<algorithm> #include<stdlib.h> using name

UVA Solve It(二分查找)

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and ter

UVA Closest Sums(二分查找)

Problem D Closest Sums Input: standard input Output: standard output Time Limit: 3 seconds Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is clo

UVA10487 Closest Sums【暴力+二分】

Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple cases. ????Each case starts with an

UVa 1152 和为0的4个值(二分查找)

https://vjudge.net/problem/UVA-1152 题意:给定4个n元素集合A,B,C,D,要求分别从中选取一个元素a,b,c,d,使得a+b+c+d=0.问有多少种取法. 思路:直接暴力枚举的话是会超时的.可以选把a+b的值枚举出来存储,c和d的值也一样并排序,这样就可以在c和d中进行二分查找了. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int ma

UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 typedef long long LL; 6 7 const int maxn = 10

UVA 10341 (二分查找+精度)

题意: 给你一个关于x的方程,给出变量的值,求出x: Problem F Solve It Input:standard input Output:standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) +s*tan(x) +t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of mult