1. 程式人生 > >[POJ 2785] 4 Values whose Sum is 0

[POJ 2785] 4 Values whose Sum is 0

nal () exce cto size val int tac fine

[題目鏈接]

http://poj.org/problem?id=2785

[算法]

中途相遇法

[代碼]

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include 
<cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include
<iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include
<cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 4010 int i,j,n,ans,l,r; vector< int > x,y; int a[MAXN],b[MAXN],c[MAXN],d[MAXN]; int main() { while (scanf("%d",&n) != EOF) { for (i = 1; i <= n; i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); x.clear(); y.clear(); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { x.push_back(a[i] + b[j]); } } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { y.push_back(c[i] + d[j]); } } sort(x.begin(),x.end()); sort(y.begin(),y.end()); ans = 0; for (i = 0; i < x.size(); i++) { l = lower_bound(y.begin(),y.end(),-x[i]) - y.begin(); r = upper_bound(y.begin(),y.end(),-x[i]) - y.begin(); r--; if (y[r] == -x[i]) ans += r - l + 1; else continue; } printf("%d\n",ans); } return 0; }

[POJ 2785] 4 Values whose Sum is 0