Can you find it? hdu2141 二分尋找

來源:互聯網
上載者:User

/*
   這個題目分在搜尋的地方,最近一直在寫bfs dfs, 腦子中看見題目就感覺是搜,
   其實這個題目很簡單的,排序 + 二分尋找就OK了
   二分一直沒有寫過,都是在書本上看理論知識,理論聯絡實際,
   這才是最重要的
*/
#include<iostream>//2395029 2010-04-28 18:42:18 Accepted 2141 390MS 1276K 1076 B C++ 悔惜晟
#include<algorithm>
#include<cstdio>
using namespace std;

int a[505], b[505], c[505];
int sumab[250005];
int l, n, m, s;
bool flag;
void search(int x)
{
 int begin;
 int end;
 int mid;
 begin = 1;
 end = l * n;
 while(begin <= end)
 {
  mid = (begin + end) / 2;
  if(sumab[mid] == x)
  {
   flag = true;
   break;
  }
  else if(sumab[mid] < x)
   begin = mid + 1;
  else
   end = mid - 1;
 }
}
int main()
{
 int i, j,  t;
 t = 1;
 int count;
 while(cin>>l>>n>>m)
 {
  for(i = 1; i <= l; i++)
   cin>>a[i];
  for(i = 1; i <= n; i++)
   cin>>b[i];
  for(i = 1; i <= m; i++)
   cin>>c[i];
  count = 1;
  for(i = 1; i <= l; i++)
  for(j = 1; j <= n; j++)
   sumab[count++] = a[i] + b[j];
  sort(&sumab[1], &sumab[1] + l *n);
  printf("Case %d:/n", t++);
  cin>>s;
  int x;
  while(s--)
  {
   cin>>x;
   flag = false;
   for(i = 1; i <= m; i++)
   {
    int ss = x - c[i];
    search(ss);
    if(flag)
     break;

   }
   if(i <= m)
    printf("YES/n");
   else
    printf("NO/n");
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.