UVa 10487 Closest Sums:遍曆&二分尋找

來源:互聯網
上載者:User

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

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

思路:可以直接O(N^2)枚舉最接近的數,但是不妨排序後,對每個數a[i],在a[i+1]~a[n-1]之間二分尋找與a[i]之和最接近query的數,複雜度O(Nlog N)

完整代碼:

/*0.012s*/    #include<cstdio>  #include<cstdlib>  #include<algorithm>  using namespace std;  #define sf scanf  #define pf printf  const int INF=0x3f3f3f3f;      int a[1005];  int cas = 0, n, m, query, closest, diff, i, j;      inline void find()  {      if (j == n || j != i + 1 && diff - a[j - 1] < a[j] - diff)      {          if (diff - a[j - 1] < abs(closest - query))              closest = a[i] + a[j - 1];      }      else    {          if (a[j] - diff < abs(closest - query))              closest = a[i] + a[j];      }  }      int main()  {      while (sf("%d", &n), n)      {          for (i = 0; i < n; ++i)              sf("%d", &a[i]);          sort(a, a + n);          sf("%d", &m);          pf("Case %d:\n", ++cas);          while (m--)          {              sf("%d", &query);              closest = INF;              for (i = 0; i < n - 1; ++i)              {                  diff = query - a[i];                  j = lower_bound(a + i + 1, a + n, diff) - a;                  find();              }              pf("Closest sum to %d is %d.\n", query, closest);          }      }      return 0;  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.