码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2785 4 Values whose Sum is 0(折半搜索)

时间:2017-10-02 14:28:35      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:names   span   cst   tar   bsp   code   target   clu   std   

题目链接:http://poj.org/problem?id=2785

题意:4个集合里各取一个数使得之和为0,问有多少种取法

题解:暴力4个for会超时,所以两个合并一下,然后搜索呗

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int N=4444;
 7 int A[N],B[N],C[N],D[N],E[N*N];
 8 
 9 int main(){
10     int n,ans=0;
11     scanf("%d",&n);
12     for(int i=0;i<n;i++) scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
13     for(int i=0;i<n;i++){
14         for(int j=0;j<n;j++){
15             E[i+j*n]=(A[i]+B[j]);
16         }
17     }
18     sort(E,E+n*n);
19     for(int i=0;i<n;i++){
20         for(int j=0;j<n;j++){
21             int cd=-(C[i]+D[j]);
22             ans+=(upper_bound(E,E+n*n,cd)-lower_bound(E,E+n*n,cd));
23         }
24     }
25     printf("%d\n",ans);
26     return 0;
27 }

 

POJ 2785 4 Values whose Sum is 0(折半搜索)

标签:names   span   cst   tar   bsp   code   target   clu   std   

原文地址:http://www.cnblogs.com/Leonard-/p/7620059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!