uva-10487 - Closest Sums
来源:互联网 发布:室内三维设计软件 编辑:程序博客网 时间:2024/05/21 02:48
暴力枚举后去重最后二分加判断找答案
#include<iostream>#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<algorithm>using namespace std;int main(){int count=0;int t,m,i,n,j;int a[1010];while(cin>>n&&n){printf("Case %d:\n",++count);vector<int>box;for(i=0;i<n;i++)cin>>a[i];for(i=0;i<n;i++)for(j=i+1;j<n;j++)box.push_back(a[i]+a[j]);sort(box.begin(),box.end());box.erase(unique(box.begin(),box.end()),box.end());cin>>m;n=box.size();while(m--){cin>>t;i=lower_bound(box.begin(),box.end(),t)-box.begin();if(i==n)i--;else if(i>0)if(abs(box[i]-t)>abs(box[i-1]-t))i--;printf("Closest sum to %d is %d.\n",t,box[i]);}}return 0;}
Problem D
Closest Sums
Input: standard input
Output: standard output
Time Limit: 3 seconds
Given is a set of integers andthen a sequence of queries. A query gives you a number and asks to find a sum oftwo 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 singleline. The next line contains a positive integerm giving the number ofqueries, 0 < m < 25. The next m lines contain aninteger 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 samplebelow. For each query output one line giving the query value and the closestsum 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.
Piotr Rudnicki
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- uva 10487 - Closest Sums
- uva 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- uva 10487 - Closest Sums
- UVA 10487 - Closest Sums
- uva 10487 Closest Sums
- uva 10487 Closest Sums
- uva 10487 Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa-10487-Closest Sums
- UVA - 10487 Closest Sums
- uva-10487 - Closest Sums
- UVA - 10487 Closest Sums
- 主元素问题
- 【手把手教你做智能车】第六节-UART串口通信
- 对于电话号码国家代码+86的处理
- sublime text 3 快捷键大全以及配置编译环境
- js的Prototype属性详解
- uva-10487 - Closest Sums
- 第九周项目2 分数的累加
- CE Boot Architecture
- Repeater 时间格式化
- Makefile内置变量,递归式变量,直接展开式变量,条件赋值,追加赋值
- 经营操作系统的一些思路
- WINCE的pb过期解决办法
- 友盟社交分享中的那些坑
- net.sf.json.JSONException: There is a cycle in the hierarchy!---数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛