码迷,mamicode.com
首页 > 其他好文 > 详细

UVA10487 Closest Sums【暴力+二分】

时间:2019-02-13 22:54:34      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ica   which   can   term   ber   std   tps   ESS   sea   

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 integer n (1 < n ≤ 1000), which indicates, how many numbers are in the set of integer. Next n lines contain n numbers. Of course there is only one number in a single line. The next line contains a positive integer m giving the number of queries, 0 < m < 25. The next m lines contain an integer of the query, one per line.
????Input is terminated by a case whose n = 0. Surely, this case needs no processing.
Output
Output should be organized as in the sample below. For each query output one line giving the query value and the closest sum in the format as in the sample. Inputs will be such that no ties will occur.
Sample Input
5
3
12
17
33
34
3
1
51
30
3
1
2
3
3
1
2
3
3
1
2
3
3
4
5
6
0
Sample Output
Case 1:
Closest sum to 1 is 15.
Closest sum to 51 is 51.
Closest sum to 30 is 29.
Case 2:
Closest sum to 1 is 3.
Closest sum to 2 is 3.
Closest sum to 3 is 3.
Case 3:
Closest sum to 4 is 4.
Closest sum to 5 is 5.
Closest sum to 6 is 5.

问题链接UVA10487 Closest Sums
问题简述:(略)
问题分析
????给定n个数字,求这n个数字的两两之和中最接近于给定数的和。
????这是一个序列处理问题,可以暴力解决,也可以二分查找。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(二分)如下:

/* UVA10487 Closest Sums */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
int a[N], sum[N * N / 2];

int Binary_Search(int b, int k)
{
    int left = 0, right = k - 1, mid, ans;
    for(;;) {
        if(right - left == 1) {
            ans = (sum[right] - b) < (b - sum[left]) ? sum[right] : sum[left];
            break;
        }
        mid = (left + right) / 2;
        if(sum[mid] > b)  right = mid;
        if(sum[mid] < b)  left = mid;
        if(sum[mid] == b) {ans = b ; break;}
    }
    return ans;
}

int main()
{
    int n, caseno = 0, m, b, k;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        k = 0;
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
                sum[k++] = a[i] + a[j];
        sort(sum, sum + k);

        printf("Case %d:\n", ++caseno);

        scanf("%d", &m);
        for(int i = 1; i <= m; i++) {
            scanf("%d", &b);
            printf("Closest sum to %d is %d.\n", b, Binary_Search(b, k));
        }
    }

    return 0;
}

AC的C++语言程序(暴力)如下:

/* UVA10487 Closest Sums */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
int a[N];

int main()
{
    int n, caseno = 0, m, b;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        printf("Case %d:\n", ++caseno);

        scanf("%d", &m);
        for(int i = 1; i <= m; i++) {
            scanf("%d", &b);

            int minv = INT_MAX, t1, t2;
            for(int j = 0; j < n; j++)
                for(int k = j + 1; k < n; k++) {
                    int tmp = abs(a[j] + a[k] - b);
                    if( tmp < minv) {
                        minv = tmp;
                        t1 = a[j];
                        t2 = a[k];
                    }
                }

            printf("Closest sum to %d is %d.\n", b, t1+t2);
        }
    }

    return 0;
}

UVA10487 Closest Sums【暴力+二分】

标签:ica   which   can   term   ber   std   tps   ESS   sea   

原文地址:https://www.cnblogs.com/tigerisland45/p/10372039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!