[POJ 2785] 4 Values whose Sum is 0
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 2785] 4 Values whose Sum is 0相关的知识,希望对你有一定的参考价值。
[题目链接]
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 ",ans); } return 0; }
以上是关于[POJ 2785] 4 Values whose Sum is 0的主要内容,如果未能解决你的问题,请参考以下文章
poj 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 :POJ - 2785
4 Values whose Sum is 0 POJ - 2785
POJ 2785 4 Values whose Sum is 0