简单二元组——C++map的简单应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kwong_young/article/details/80618142

昨天做了一题,用Hash搞了一个小时,最后才90分。。    今天改用map,不到10分钟AC。。

关于某项值记录个数的情况,用map真的很好用

废话不多说先上题目


题目

互补二元组

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

给定N个整数二元组(X1, Y1), (X2, Y2), ... (XN, YN)。  

请你计算其中有多少对二元组(Xi, Yi)和(Xj, Yj)满足Xi + Xj = Yi + Yj且i < j。

输入

第一行包含一个整数N。  

以下N行每行两个整数Xi和Yi。  

对于70%的数据,1 ≤ N ≤ 1000    

对于100%的数据,1 ≤ N ≤ 100000  -1000000 ≤ Xi, Yi ≤ 1000000

输出

一个整数表示答案。

样例输入
5  
9 10  
1 3  
5 5  
5 4    
8 6
样例输出
2


思路很简单就是等式做一下变换,对于每个数组对,看他们的差的相反数在不在Map里就可以了。

代码

#include <map>
#include <iostream>


using namespace std;


int main(){
	int n;
	cin>>n;
	int i;
	map<int,int> maze;
	int a,b,c;
	long count = 0;
	for(i=0;i<n;i++){
		cin>>a>>b;
		c = a - b;
		if(maze.find(-c)!=maze.end()){
			count += maze[-c];
		}
		if(maze.find(c)!=maze.end()){
			maze[c] ++;
		}else{
			maze[c] = 1;
		}
		
	}
	cout<< count <<endl;
	return 0;
}


下面再放一个用Hash做的,只过了90分的答案。   没完全对不知道为什么


#include<iostream>

using namespace std;
#define MAX 1<<29
struct node{
	long num;
	long count;
};
void has(node *a,long n){
	int add = (n+2000000) % 100005;
	if(a[add].num == MAX){
		a[add].num = n;
		a[add].count++;	
	}else {
		while(a[add].num!=MAX){
			if(a[add].num == n){
				a[add].count++;
				return;
			}
			add= (add+1)%100005; 
		} 
		a[add].num = n;
	}
}
long search(node *a,long n){
	int c = (-n+2000000) % 100005;
	long count = 0;
	while(a[c].num!=MAX){
		if(a[c].num == -n){
			count = a[c].count;
			break;
		}else{
			c= (c+1)%100005;
		}
	}
	return count;
}
int main() {
//	freopen("aa.txt","r",stdin);
	node N[100020];
	int n;
	cin >> n;
	long a,b,c;
	for(int i =0;i<=100005;i++){
		N[i].count = 0;
		N[i].num = MAX ;
	}
	unsigned long long count = 0;
	for(int i = 0 ; i < n;i++){
		cin >> a >> b;
		c= a - b;
		has(N,c);
	}
	for(int i = 0 ;i <= 100005 ;i++){
		if(N[i].num == MAX) continue;
		if(N[i].num == 0){
			count += (N[i].count*(N[i].count-1))/2;
			continue; 
		}
		
		count += N[i].count * search(N,N[i].num);
		N[i].num = MAX;
	}
	cout<<count<<endl;
	return 0;
}


最后在放一下map的基本用法

转载于C/C++——map的基本操作总结

标准库map类型是一种以键-值(key-value)存储的数据类型。以下分别从以下的几个方面总结:

  • map对象的定义和初始化
  • map对象的基本操作,主要包括添加元素,遍历等

1、pair类型

1.1、pair类型的定义和初始化

pair类型是在有文件utility中定义的,pair类型包含了两个数据值,通常有以下的一些定义和初始化的一些方法:

  • pair<T1, T2> p;
  • pair<T1, T2> p(v1, v2);
  • make_pair(v1, v2)

上述第一种方法是定义了一个空的pair对象p,第二种方法是定义了包含初始值为v1和v2的pair对象p。第三种方法是以v1和v2值创建的一个新的pair对象。

1.2、pair对象的一些操作

除此之外,pair对象还有一些方法,如取出pair对象中的每一个成员的值:

  • p.first
  • p.second

例如:

#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;

int main(){
        pair<int, string> p1(0, "Hello");
        printf("%d, %s\n", p1.first, p1.second.c_str());
        pair<int, string> p2 = make_pair(1, "World");
        printf("%d, %s\n", p2.first, p2.second.c_str());
        return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2、map对象的定义和初始化

map是键-值对的组合,有以下的一些定义的方法:

  • map<k, v> m;
  • map<k, v> m(m2);
  • map<k, v> m(b, e);

上述第一种方法定义了一个名为m的空的map对象;第二种方法创建了m2的副本m;第三种方法创建了map对象m,并且存储迭代器b和e范围内的所有元素的副本。

map的value_type是存储元素的键以及值的pair类型,键为const。

3、map对象的一些基本操作

3.1、map中元素的插入

在map中元素有两种插入方法:

  • 使用下标
  • 使用insert函数

在map中使用下标访问不存在的元素将导致在map容器中添加一个新的元素。

insert函数的插入方法主要有如下:

  • m.insert(e)
  • m.insert(beg, end)
  • m.insert(iter, e)

上述的e一个value_type类型的值。beg和end标记的是迭代器的开始和结束。

两种插入方法如下面的例子所示:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 10; i ++){
                mp[i] = i;
        }
        for (int i = 10; i < 20; i++){
                mp.insert(make_pair(i, i));
        }
        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d-->%d\n", it->first, it->second);
        }
        return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.2、map中元素的查找和读取

注意:上述采用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。

因此,若只是查找该元素是否存在,可以使用函数count(k),该函数返回的是k出现的次数;若是想取得key对应的值,可以使用函数find(k),该函数返回的是指向该元素的迭代器。

上述的两个函数的使用如下所示:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        if (mp.count(0)){
                printf("yes!\n");
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it_find;
        it_find = mp.find(0);
        if (it_find != mp.end()){
                it_find->second = 20;
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }
        return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.3、从map中删除元素

从map中删除元素的函数是erase(),该函数有如下的三种形式:

  • m.erase(k)
  • m.erase(p)
  • m.erase(b, e)

第一种方法删除的是m中键为k的元素,返回的是删除的元素的个数;第二种方法删除的是迭代器p指向的元素,返回的是void;第三种方法删除的是迭代器b和迭代器e范围内的元素,返回void。

如下所示:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        mp.erase(0);

        mp.erase(mp.begin());

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }


        return 0;
}

猜你喜欢

转载自blog.csdn.net/Kwong_young/article/details/80618142
今日推荐