- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####*******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define S 10003
- using namespace std;
- typedef long long LL;
- vector <int> Number, srt;
- int BS(int low, int high, int desiredN){
- int mn = INT_MAX, numB, dis;
- while(low <= high){
- int mid = (low+high)/2;
- dis = abs(desiredN-srt[mid]);
- if(mn > dis)mn = dis, numB = srt[mid];
- if(srt[mid] == desiredN)return desiredN;
- if(srt[mid] > desiredN)high = mid-1;
- else low = mid+1;
- }
- return numB;
- }
- void Do(){
- int n, N, cs = 0;
- while(sc(n) == 1){
- if(!n)break;
- for(int i = 0; i < n; i++)sc(N), Number.push_back(N);
- for(int i = 0; i < n; i++){
- for(int j = i+1; j < n; j++){
- srt.push_back(Number[i]+Number[j]);
- }
- }
- sort(srt.begin(), srt.end());
- int q, DN;
- sc(q);
- printf("Case %d:\n", ++cs);
- for(int i = 0; i < q; i++){
- sc(DN);
- int ans = BS(0, srt.size()-1, DN);
- printf("Closest sum to %d is %d.\n", DN, ans);
- }
- Number.clear(), srt.clear();
- }
- }
- int main(){
- ios_base::sync_with_stdio(0); cin.tie(0);
- #ifndef ONLINE_JUDGE
- ///freopen("inp","r",stdout);
- ///freopen("contest.txt","w",stdout);
- #endif
- Do();
- return 0;
- }
Friday, June 5, 2015
UVa - 10487 - Closest Sums
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment