STL 的应用

multiset 

map<int,multiset<int>> 可用于二维坐标对应的点(并能记录重点)

 

hdu 4022

 题目大意:在二维平面上,给一些点。有两个操作:给一个d,把所有x为d的点去掉,或把所有y为d的点去掉,问每次去掉的点。

 1 #include<iostream>
2 #include<cstdio>
3 #include<vector>
4 #include<string.h>
5 #include<cmath>
6 #include<queue>
7 #include<set>
8 #include<cstdlib>
9 #include<algorithm>
10 #include<map>
11 #include <iomanip>
12 #include <bitset>
13 #include<iterator>
14 using namespace std;
15 #define LL long long
16 const int MAX = 200010;
17 const int INF=100000000;
18 int main(){
19 int n,m;
20 while(scanf("%d%d",&n,&m)!=EOF){
21 if(!n&&!m) return 0;
22 map<int,multiset<int> > x;
23 map<int,multiset<int> > y;
24 while(n--){
25 int a,b;
26 scanf("%d%d",&a,&b);
27 x[a].insert(b);
28 y[b].insert(a);
29 }
30 while(m--){
31 int a,b;
32 scanf("%d%d",&a,&b);
33 if(!a) {
34 printf("%d\n",x[b].size());
35 for(multiset<int>::iterator i=x[b].begin();i!=x[b].end();i++)
36 y[*i].erase(b);
37 x[b].clear();
38 }
39 else{
40 printf("%d\n",y[b].size());
41 for(multiset<int>::iterator i=y[b].begin();i!=y[b].end();i++)
42 x[*i].erase(b);
43 y[b].clear();
44 }
45 }
46 printf("\n");
47 }
48 }

 

binary_search 

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们作用的range是已sorted。

binary_search试图在已排序的[first, last)中寻找元素value。如果[first, last)内有等价于value的元素,它会返回true,否则返回false,它不返回查找位置。

lower_bound它试图在已排序的[first,last)中寻找元素value。如果[first, last)具有等价于value的元素,lower_bound返回一个iterator指向其中第一个元素。如果没有这样的元素存在,它便返回假设这样的元素存在的话,会出现的位置。即指向第一个不小于value的元素。如果value大于[first, last)的任何一个元素,则返回last。

upper_bound它试图在已排序的[first,last)中寻找value,返回可安插value的最后一个合适的位置。如果value存在,lower_bound 返回的是指向该元素的iterator。相较之下upper_bound并不这么做,它返回value可被安插的最后一个合适位置。如果value存在,那么它返回的iterator将指向value的下一个位置,而非value自身。

equal_range的返回值本质上结合了lower_bound和upper_bound两者的返回值。其返回值是一对iterator i 和 j , 其中i是value可安插的第一个位置,j则是value可安插的最后一个位置。可以推演出:[i,j)中的每个元素都等价于value,而且[i, j)是[first, last)之中符合上述性质的一个最大子区间。  算法lower_bound返回该range的第一个iterator, 算法upper_bound返回该range的past-the-end iterator,算法equal_range则是以pair的形式将两者都返回。



poj 2002

 1 题意:在平面内给出n个点,问你这些点一共能组成几个不相等的正方形?
2
3 思路:几何 + 二分。先排序,然后枚举任意两点(x1,y1)(x2,y2),则如果存在点(x1+y1-y2,y1-x1+x2)(x2+y1-y2,y2-x1+x2)则它们能构成一个正方形(这里方向是确定的,否则还有一种可能)。所以用二分查询是否存在这两个点,有的话ans就+1。最后的ans要/2,因为正方形都重复算了一次。
4
5 源代码1:(172K,1563MS) 全STL。
6 #include<iostream>
7 #include<algorithm>
8 using namespace std;
9 const int Max = 1005;
10
11 struct data{
12 int x, y;
13 }node[Max];
14
15 bool cmp(const data &a, const data &b){ // 由于下面调用的binary_search(),故要用加const。
16 if(a.x == b.x) return a.y < b.y;
17 return a.x < b.x;
18 }
19
20 int main(){
21 int n, i, j;
22 while(scanf("%d", &n) && n){
23 for(i = 0; i < n; i ++)
24 scanf("%d%d", &node[i].x, &node[i].y);
25 sort(node, node + n, cmp);
26 // 先排序。这里自己要注意:x相等的y问题不用二维的二分,直接cmp,要理解清楚,就一顺序。
27 int ans = 0;
28 for(i = 0; i < n; i ++)
29 for(j = i + 1; j < n; j ++){
30 data tmp;
31 tmp.x = node[i].x + node[i].y - node[j].y;
32 tmp.y = node[i].y - node[i].x + node[j].x;
33 if(!binary_search(node, node + n, tmp, cmp)) // 学会STL的二分。
34 continue;
35 tmp.x = node[j].x + node[i].y - node[j].y;
36 tmp.y = node[j].y - node[i].x + node[j].x;
37 if(!binary_search(node, node + n, tmp, cmp))
38 continue;
39 ans ++;
40 }
41 printf("%d\n", ans/2);
42 }
43 return 0;
44 }


演示binary_search

 1 /**************************
2 *范例编号:17_02
3 *范例说明:演示binary_search的
4 * 功能与使用方法
5 ***************************/
6 #include <list>
7 #include <vector>
8 #include <algorithm>
9 #include <iostream>
10 bool mod_lesser ( int elem1, int elem2 ){
11 if (elem1 < 0)
12 elem1 = - elem1;
13 if (elem2 < 0)
14 elem2 = - elem2;
15 return elem1 < elem2;
16 }
17 int main( ){
18 using namespace std;
19 list <int> L;
20 list <int>::iterator Iter;
21 bool b1, b2;
22 L.push_back( 50 );
23 L.push_back( 10 );
24 L.push_back( 30 );
25 L.push_back( 20 );
26 L.push_back( 25 );
27 L.push_back( 5 );
28 L.sort( );
29 cout << "L = ( " ;
30 for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
31 cout << *Iter << " ";
32 cout << ")" << endl;
33 b1 = binary_search( L.begin( ), L.end( ), 10 );
34 if ( b1 )
35 cout << "存在元素等于10." << endl;
36 else
37 cout << "没有元素等于10." << endl;
38 L.sort ( greater<int> ( ) );
39 b2=binary_search(L.begin(),L.end(),
40 10,greater<int>());
41 if ( b2 )
42 cout << "存在元素在大于的意义下等价于10"<< endl;
43 else
44 cout << "没有元素在大于的意义下等价于10"<< endl;
45 vector <int> v1;
46 vector <int>::iterator Iter1;
47 int i;
48 for ( i = -2 ; i <= 4 ; i++ )
49 v1.push_back( i );
50 sort ( v1.begin ( ) , v1.end ( ) , mod_lesser );
51
52 cout << "按照绝对值大小, vector v1 = ( " ;
53 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
54 cout << *Iter1 << " ";
55 cout << ")" << endl;
56 bool b3=binary_search(v1.begin(),v1.end(),
57 -3,mod_lesser);
58 if ( b3 )
59 cout << "存在元素在绝对值相等的意义下等价于-3" << endl;
60 else
61 cout << "没有元素在绝对值相等的意义下等价于-3" << endl;
62 }

演示lower_bound

 1 /**************************
2 *范例编号:17_18
3 *范例说明:演示lower_bound的
4 * 功能与使用方法
5 ***************************/
6 #include <vector>
7 #include <algorithm>
8 #include <functional>
9 #include <iostream>
10 bool mod_lesser ( int elem1, int elem2 )
11 {
12 if ( elem1 < 0 )
13 elem1 = - elem1;
14 if ( elem2 < 0 )
15 elem2 = - elem2;
16 return elem1 < elem2;
17 }
18 int main( )
19 {
20 using namespace std;
21 vector <int> v1;
22 vector <int>::iterator Iter1, Result1;
23 int i;
24 for ( i = -1 ; i <= 4 ; i++ )
25 v1.push_back( i );
26 int ii;
27 for ( ii =-3 ; ii <= 0 ; ii++ )
28 v1.push_back( ii );
29 sort ( v1.begin ( ) , v1.end ( ) );
30 cout << "原始向量 v1 = ( " ;
31 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
32 cout << *Iter1 << " ";
33 cout << ")." << endl;
34 vector <int> v2 ( v1 );
35 vector <int>::iterator Iter2, Result2;
36 sort ( v2.begin ( ) , v2.end ( ) , greater<int> ( ) );
37 cout << "原始向量v2 = ( " ;
38 for ( Iter2 = v2.begin ( ) ; Iter2 != v2.end ( ) ; Iter2++ )
39 cout << *Iter2 << " ";
40 cout << ")." << endl;
41 vector <int> v3 ( v1 );
42 vector <int>::iterator Iter3, Result3;
43 sort ( v3.begin ( ) , v3.end ( ) , mod_lesser );
44 cout << "原始向量 v3 = ( " ;
45 for ( Iter3 = v3.begin ( ) ; Iter3 != v3.end ( ) ; Iter3++ )
46 cout << *Iter3 << " ";
47 cout << ")." << endl;
48 Result1 = lower_bound ( v1.begin ( ) , v1.end ( ) , 3 );
49 cout << "v1中值为3的下界元素是: "
50 << *Result1 << "." << endl;
51 Result2 = lower_bound ( v2.begin ( ) , v2.end ( ) , 3, greater <int> ( ) );
52 cout << " v2中值为3的下界元素是:"
53 << *Result2 << "." << endl;
54 Result3 = lower_bound ( v3.begin ( ) , v3.end ( ) , 3, mod_lesser );
55 cout << " v3中值为3的下界元素是:"
56 << *Result3 << "." << endl;
57 }


引用文献:

http://blog.sina.com.cn/s/blog_6635898a0100lxpx.html

http://www.cppblog.com/panther/archive/2011/11/04/42635.html

 

posted @ 2012-03-12 16:52  HaoHua_Lee  阅读(170)  评论(0编辑  收藏  举报