UVa 10125 Sumsets (折半枚舉&二分尋找)

來源:互聯網
上載者:User

10125 - Sumsets

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1066

Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.

Input

Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed by the elements of S, one per line. Each element of S is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each S, a single line containing d, or a single line containing "no solution".

Sample Input

52 3 5 7 1252 16 64 256 10240

Output for Sample Input

12no solution

思路見代碼注釋。

複雜度:O(n^2 log n)

/*0.072s*/    #include<bits/stdc++.h>  using namespace std;      struct node  {      int a, b, sum; ///注意已經保證了a<b;      bool operator < (const node& a) const    {          return sum < a.sum;      }  } sum[499505], tmpsum;  int a[1005], tmp[4], n, c, ans, low, upp;      inline bool notequal()///判斷這4個數是否互不相同  {      int i,j;      for (i = 0; i < 3; ++i)          for (j = i + 1; j < 4; ++j)              if (tmp[i] == tmp[j]) return false;      return true;  }      bool solve()  {      int i,j,k;      for (i = n - 1; i >= 0; --i)///從S中最大的枚舉d          for (j = 0; j < n; ++j)///枚舉c          {              if (j == i) continue;              tmp[0] = a[i], tmp[1] = a[j];///a[i]是d,a[j]是c              tmpsum = (node){a[i], a[j], a[i] - a[j]};///d-c              low = lower_bound(sum, sum + c, tmpsum) - sum, upp = upper_bound(sum, sum + c, tmpsum) - sum;///二分找d-c              for (k = low; k < upp; ++k)              {                  tmp[2] = sum[k].a, tmp[3] = sum[k].b;///a和b                  if (notequal())                  {                      ans = a[i];                      return true;                  }              }          }      return false;  }      int main()  {      int i,j;      while (scanf("%d", &n), n)      {          for (i = 0; i < n; ++i) scanf("%d", &a[i]);          if (n < 4)          {              puts("no solution");              continue;          }          sort(a, a + n);          c = 0;          for (i = 0; i < n - 1; ++i)              for (j = i + 1; j < n; ++j)                  sum[c++] = (node){a[i], a[j], a[i] + a[j]};///枚舉S中任意兩個元素之和,並排序          sort(sum, sum + c);          if (solve()) printf("%d\n", ans);          else puts("no solution");      }      return 0;  }

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

聯繫我們

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