Closest Sums - UVa10487 二分

来源:互联网 发布:图片生成视频软件 编辑:程序博客网 时间:2024/05/21 07:12

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 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.

题意:给你n个数,有m次查询,每次给你一个数k,求上面n个数其中两个数的和最接近k的是多少。

思路:先求出两个数的所有可能的和,排序后二分找到小于等于k的,和大于等于k的,然后找其中一个最接近k的作为答案。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>using namespace std;int t,n,m,num[1010],sum[1000010],tot;int find1(int ret){    int l=1,r=tot,mi;    while(l<r)    {        mi=(l+r+1)/2;        if(sum[mi]<=ret)          l=mi;        else          r=mi-1;    }    return l;}int find2(int ret){    int l=1,r=tot,mi;    while(l<r)    {        mi=(l+r)/2;        if(sum[mi]>=ret)          r=mi;        else          l=mi+1;    }    return l;}int main(){    int i,j,k,num1,num2;    while(~scanf("%d",&n) && n>0)    {        for(i=1;i<=n;i++)           scanf("%d",&num[i]);        tot=0;        for(i=1;i<=n;i++)           for(j=i+1;j<=n;j++)              if(num[i]!=num[j])              sum[++tot]=num[i]+num[j];        sort(sum+1,sum+1+tot);        scanf("%d",&m);        printf("Case %d:\n",++t);        for(i=1;i<=m;i++)        {            scanf("%d",&k);            num1=sum[find1(k)];            num2=sum[find2(k)];            if(abs(num1-k)>abs(num2-k))              num1=num2;            printf("Closest sum to %d is %d.\n",k,num1);        }    }}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买家号账户体检中心违规怎么办 支付宝充错手机账号怎么办 美团恶意差评怎么办 买家好评后追加差评怎么办 宝贝吃了一个金币怎么办 店铺微淘等级l1怎么办 淘宝占内存2个g怎么办 淘宝太占空间了怎么办 支付宝占内存大怎么办 苹果手机储存空间不足怎么办 小米平板电脑储存空间不足怎么办 ipad2很卡反应慢怎么办 ipadmini很卡反应慢怎么办 手机酷狗音乐文件不支持怎么办 2018款ipad闪退怎么办 ipad开不了机了怎么办 淘宝盖楼上限了怎么办 交了学费做微淘客却加不到人怎么办 微淘客交首付不想做了怎么办 蚂蚁微客二维码推广怎么办 游拍主播申请手机号被注册怎么办 淘宝客不给力怎么办 淘宝买家确认收货超时怎么办 淘宝没收到货退款卖家不处理怎么办 微博红包都是字怎么办 500个访客没转化怎么办 店铺动态评分是0怎么办 京东店铺评分低怎么办 被淘宝主播屏蔽怎么办 在淘宝客推广后退款怎么办 生产出现异常时你应该怎么办 违规后的店铺没访客怎么办 淘宝少发货店家不承认怎么办 淘宝买东西店家不发货怎么办 淘宝店家拒绝同意退款怎么办 被淘宝店家骂了怎么办 淘宝买家骂店家骚扰店家怎么办? 不想开淘宝店了怎么办 我是客服经常有客户骂人怎么办 淘宝直播前期没人看怎么办 淘宝被投诉商标侵权怎么办