【官方文档】
pair - C++ Reference:
This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second.
#include <bits/stdc++.h>
using namespace std;
int main(){
pair<string,double> p1; // default constructor
p1=make_pair("lightbulbs",0.99); // using make_pair (move)
cout<<"The price of "<<p1.first<<" is $"<<p1.second<<endl;
pair<string,double> p2("tomatoes",2.30); // value init
cout<<"The price of "<<p2.first<<" is $"<<p2.second<<endl;
p2.first="shoes"; // the type of first is string
p2.second=39.90; // the type of second is double
cout<<"The price of "<<p2.first<<" is $"<<p2.second<<endl;
pair<string,double> p3(p2); // copy constructor
cout<<"The price of "<<p3.first<<" is $"<<p3.second<<endl;
return 0;
}
/*
The price of lightbulbs is $0.99
The price of tomatoes is $2.3
The price of shoes is $39.9
The price of shoes is $39.9
*/
【题目来源】
http://acm.fzu.edu.cn/problem.php?pid=2231
http://poj.org/problem?id=1971
【题目描述】
在一个平面内给定n个点,任意三个点不在同一条直线上,用这些点可以构成多少个平行四边形?一个点可以同时属于多个平行四边形。
【输入格式】
多组数据(<=10)。每组数据第一行一个整数n(4<=n<=500)。接下来n行每行两个整数xi,yi(0<=xi,yi<=1e9),表示每个点的坐标。
【输出格式】
每组数据输出一个整数,表示用这些点能构成多少个平行四边形。
【算法分析】
利用平行四边形的对角线互相平分的性质求解。
注意:下面代码的目的主要是学习利用STL pair及map,但是可能会超时(Time Limit Exceeded)。
【算法代码】
#include <bits/stdc++.h>
using namespace std;
const int maxn=505;
int x[maxn],y[maxn];
typedef pair<int,int> p;
int main() {
int n;
while(~scanf("%d",&n)) {
int i,j,ans=0;
map<p,int> a;
a.clear();
for(i=0; i<n; i++) {
scanf("%d %d",&x[i],&y[i]);
for(j=0; j<i; j++) {
ans+=a[p(x[i]+x[j],y[i]+y[j])]++;
}
}
printf("%d\n",ans);
}
return 0;
}
/*
in1:
6
0 0
2 0
4 0
1 1
3 1
5 1
out1:
5
in2:
4
0 1
1 0
1 1
2 0
out2:
1
in3:
7
-2 -1
8 9
5 7
1 1
4 8
2 0
9 8
out3:
6
*/
【参考文献】
https://blog.csdn.net/Action_now_zj/article/details/79184803
https://cplusplus.com/reference/utility/make_pair/
https://cplusplus.com/reference/utility/pair/
https://blog.csdn.net/hnjzsyjyj/article/details/108929993
https://www.acwing.com/solution/content/3098/
https://blog.csdn.net/kangyan__/article/details/77572994
https://blog.csdn.net/qq_21057881/article/details/51228906