ZROI#570
题目链接
这个题其实我感觉\(A\)的很谜...我感觉复杂度是不太对的,但是\(dalao\)们说利用单调性找中点,最后的均摊复杂度是\(O(n^2)\)的.
大体思路就是:
先把所有点排一遍序.
再枚举两个点\(i,j(i \le j)\),然后计算出它们的中点坐标,由于排完序以后中点一定坐落在它们中间.
就从\(i\)开始往后扫,扫到就答案\(+1\),由于在\(i\)不变的情况下,\(j\)一直向右的话它们的中点也一定向右移动.
根据这个单调性,每次只需要在\(i\)变动的时候重置指针就行了,最后的复杂度就是\(O(n^2)\)
\(Code:\)
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define pb push_back
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
template < class T >
inline T read () {
T x = 0 , f = 1 ; char ch = getchar () ;
while ( ch < '0' || ch > '9' ) {
if ( ch == '-' ) f = - 1 ;
ch = getchar () ;
}
while ( ch >= '0' && ch <= '9' ) {
x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
ch = getchar () ;
}
return f * x ;
}
const int N = 1e4 + 100 ;
struct pairs {
int x , y ;
bool friend operator < (pairs a , pairs b) { return ( a.x < b.x || ( a.x == b.x && a.y < b.y ) ) ; }
bool friend operator == (pairs a , pairs b) { return ( a.x == b.x && a.y == b.y ) ; }
} p[N] ;
int n , ans ;
int main (int argc , char * argv[] ) {
n = rint () ;
rep ( i , 1 , n ) p[i].x = rint () , p[i].y = rint () ;
std::sort ( p + 1 , p + n + 1 ) ;
rep ( i , 1 , n ) {
int k = i ;
rep ( j , i + 1 , n ) {
pairs tmp ;
tmp.x = p[i].x + p[j].x ;
tmp.y = p[i].y + p[j].y ;
if ( ( tmp.x & 1 ) || ( tmp.y & 1 ) ) continue ;
tmp.x >>= 1 ; tmp.y >>= 1 ;
while ( p[k] < tmp ) ++ k ;
if ( p[k] == tmp ) ++ ans ;
}
}
printf ("%d\n" , ans ) ;
system ("pause") ; return 0 ;
}
May you return with a young heart after years of fighting.