uva 10487 - Closest Sums

点击打开链接

题目意思:    先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i],输出

解题思路:    1:二分查找
                      2:由于数据很大,所以直接硬搞肯定是不行的,那么我们选择二分查找。首先先求出这些元素组成的所有的和,保存到数组sum里面,然后对sum排序,最后进行二分查找,找到距离目标最近的sum[i]输出即可

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <set>
using namespace std;
#define MAXN 1010

int n , m , len;
int num_n[MAXN] ,num_m[MAXN];
int sum[1000010];
//二分查找
void Binary_Search(int a){
    int left , right , mid , ans;
    left = 0 ; right = len-1;
    while(1){
        if(right-left == 1){
            ans = (sum[right]-a)<(a-sum[left])?sum[right]:sum[left];
            break;
        }
        mid = (left+right)/2;
        if(sum[mid] > a)  right = mid;
        if(sum[mid] < a)  left = mid;
        if(sum[mid] == a) {ans = a ; break;}
    }
    printf("Closest sum to %d is %d.\n" , a , ans);
}

void solve() {
    int i , j , k;
    for(i = 0 , k = 0 ; i < n ; i++){
        for(j = i+1 ; j < n ; j++)
            sum[k++] = num_n[i]+num_n[j];
    }
    len = k ; sort(sum , sum+k);
    for(i = 0 ; i < m ; i++)
        Binary_Search(num_m[i]);
}

int main() {
    //freopen("input.txt" , "r" , stdin);
    int cnt = 1;
    while(scanf("%d%*c" , &n) && n){
        for(int i = 0 ; i < n ; i++)
            scanf("%d%*c" , &num_n[i]);
        scanf("%d%*c" , &m);
        for(int i = 0 ; i < m ; i++)
            scanf("%d%*c" , &num_m[i]);
        printf("Case %d:\n" , cnt++);
        solve();
    }
    return 0;
}
时间: 2024-11-10 10:40:54

uva 10487 - Closest Sums的相关文章

UVa 10487 Closest Sums:遍历&amp;amp;二分查找

10487 - Closest Sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 Given is a set of integers and then a sequence of queries. A query gives you a

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: 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 di

UVa 10245:The Closest Pair Problem

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1186 [原题] Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

UVa 10013 Super long sums:简单高精度

10013 - Super long sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=954 The Problem The creators of a new programming language D++ have found out that

UVa 10616 Divisible Group Sums:DFS以及DP

10616 - Divisible Group Sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1557 思路:用DFS+记忆化搜索枚举组合,注意数字有<0的. return dp[i][cnt][sum] = f(i + 1, cnt +

uva 10245 - The Closest Pair Problem

点击打开链接uva 10245 题目意思:   给定N个点,找到所有点中距离最小的 解题思路: 1:递归+分治 <网上大牛的解释如下> 2在二维空间里,可用分治法求解最近点对问题.预处理:分别根据点的x轴和y轴坐标进行排序,得到X和Y,很显然此时X和Y中的点就是S中的点.            1情况(1):点数小于等于三时:                                  2情况(2):点数大于三时:            3首先划分集合S为SL和SR,使得SL中的每一个点

【UVA 11997 K Smallest Sums】优先级队列

来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定k个整数数组,各包含k个元素.在每个数组中取一个元素加起来,可以得到kk个和,求这些和中最小的k个值(不去重). 数据范围:k [2, 750] 思路:暴力枚举k^k不可取. "先来看问题的简化版:给出两个长度为k的数组A和B,分别在A和B中任取一个数并相加,可以得到k^2个和,求这些和中最小的k个

UVa 196:Spreadsheet

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=132 题目类型: 搜索, 拓扑排序 题目: In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a

UVa 10603:Fill,经典倒水问题+隐式图搜索+dfs

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544 类型: 隐式图搜索 原题: There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater th