STL pair 常见构造方法及实例赏析

【官方文档】
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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值