【题目描述】
给定一个点阵,求五元组(a,b,c,d,e)满足 Xa=Xb
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1000+5;
bool a[maxn][maxn];
int n,xx,yy;
int ans;
int main()
{
//freopen("w.in","r",stdin);
//freopen("w.out","w",stdout);
scanf("%d",&n);
while(n--)
{
int x,y;
scanf("%d%d",&x,&y);
a[x][y]=1;
xx=max(xx,x);
yy=max(yy,y);
}
for(int i=0;i<=xx-2;i++)
for(int j=0;j<=yy;j++)
for(int l=i+2;l<=xx;l++)
for(int r=j;r<=yy;r++)
for(int k=i+1;k<l;k++)
for(int h=j+1;h<r;h++)
{
if(a[i][j]==1&&a[i][r]==1&&a[l][j]==1&&a[l][r]==1&&a[k][h]==1)
ans++;
}
cout<<ans<<endl;
//fclose(stdin);
//fclose(stdout);
return 0;
}
30分 五重循环
60分/100分
其实我们要统计的是每一个矩形内点的个数,于是枚举两个点作为对角线,查看另一条对角线是否有点,如果有,二维前缀和查询矩形内点的个数。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define md
#define N 1010
using namespace std;
struct point { int x,y;} p[N];
int vis[N][N],hv[N][N];
int main()
{
freopen("w.in","r",stdin); freopen("w.out","w",stdout);
int n,ans=0;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
hv[p[i].x][p[i].y]=vis[p[i].x][p[i].y]=1;
}
for (int i=1;i<=1000;i++)
for (int j=1;j<=1000;j++)
hv[i][j]+=hv[i][j-1]+hv[i-1][j]-hv[i-1][j-1];
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (p[i].x<p[j].x&&p[i].y<p[j].y)
{
int x1=p[i].x,x2=p[j].x,y1=p[i].y,y2=p[j].y;
if (vis[x1][y2]&&vis[x2][y1]) ans+=hv[x2-1][y2-1]-hv[x1][y2-1]-hv[x2-1][y1]+hv[x1][y1];
}
printf("%d\n",ans);
fclose(stdout);
return 0;
}