首先头文件,
#include<map>
反正我用
#include<bits/stdc++.h>
然后就是创建一个map:
map<string,int>mymap;//类似于一个字典的映射关系,string和int可以改成其他的类型
mymap是你定义的map的名字,可以自己改
string可以改为pair<int,int>来表示两个数之间存在关系
再就是添加元素啦:
//第一种方法是用类似于数组的方法添加:
mymap[0]=hihihi;
//第二种是用insert插入:
mymap.insert(pair<int,string>(1,"hahaha"));
//其它的方法:http://t.zoukankan.com/wuchunming-p-3780677.html
map排序:
//先定义一个结构体来规定排序方式
struct rule{
bool operator()(int a,int b){
return a>b;
}
};
//再将此排序方式加入到map定义中:
map<char,int,rule>a;
然后就是输出啦:
for(auto it=mymap.begin();it!=mymap.end();it++)
cout<<it->first<<" "<<it->second;
map呢有一个自动根据key值排序的功能,也就是it->first,这个很好用哦
看例题:
问题 E: 8.4.4 硬木种类-2
题目描述
某国有数百种硬木树种,该国自然资源部利用卫星成像技术编制了一份特定日期每棵树的物种清单。计算每个物种张所有种群的百分比。
输入
输入包括每棵树的物种清单,每行一棵树。物种名称不超过30个字符,不超过10000种,不超过1000000棵树。
输出
按字母顺序输出植物种群中代表的每个物种的名称,然后是占所有种群的百分比,保留小数点后4位。
样例输入 复制
Red Alder Ash Aspen Basswood Ash Beech Yellow Birch Ash Cherry Cottonwood Ash Cypress Red Elm Gum Hackberry White Oak Hickory Pecan Hard Maple White Oak Soft Maple Red Oak Red Oak White Oak Poplan Sassafras Sycamore Black Walnut Willow
样例输出 复制
Ash 13.7931 Aspen 3.4483 Basswood 3.4483 Beech 3.4483 Black Walnut 3.4483 Cherry 3.4483 Cottonwood 3.4483 Cypress 3.4483 Gum 3.4483 Hackberry 3.4483 Hard Maple 3.4483 Hickory 3.4483 Pecan 3.4483 Poplan 3.4483 Red Alder 3.4483 Red Elm 3.4483 Red Oak 6.8966 Sassafras 3.4483 Soft Maple 3.4483 Sycamore 3.4483 White Oak 10.3448 Willow 3.4483 Yellow Birch 3.4483
map直接秒杀!
#include<bits/stdc++.h>
using namespace std;
int main(){
map<string,int>mymap;
string temp;
int num=0;
while(getline(cin,temp))
{
mymap[temp]++;
num++;
}
for(auto it=mymap.begin();it!=mymap.end();it++)
{
cout<<it->first<<" ";
cout<<fixed<<setprecision(4)<<it->second*1.0/num*100<<endl;
}
}
例题:
C - FF
Problem Statement
Takahashi runs an SNS "Twidai," which has
N users from user 1 through user
N. In Twidai, users can follow or unfollow other users.
Q operations have been performed since Twidai was launched. The i-th (1≤i≤Q) operation is represented by three integers Ti, Ai, and
Bi, whose meanings are as follows:
- If
Ti=1: it means that user Ai follows user Bi. If user Ai is already following user
- Bi at the time of this operation, it does not make any change.
- If
- Ti=2: it means that user Ai unfollows user Bi. If user Ai is not following user
- Bi at the time of this operation, it does not make any change.
- If
- Ti=3: it means that you are asked to determine if users Ai and Bi are following each other. You need to print
Yes
if user Ai is following user Bi and user Bi is following user- Ai, and
No
otherwise.
When the service was launched, no users were following any users.
Print the correct answers for all operations such that
Ti=3 in ascending order ofi.
Constraints
- Ai, and
- 2≤N≤109
- 1≤Q≤2×105
- Ti=1,2,3 (1≤i≤Q)
- 1≤Ai≤N (1≤i≤Q)
- 1≤Bi≤N (1≤i≤Q)
- Ai=Bi (1≤i≤Q)
- There exists
- i (1≤i≤Q) such that
- Ti=3.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
Q
T1
A1
B1
T2
A2
B2
⋮
TQ
AQ
BQ
Output
Print
X lines, where X is the number of i's (1≤i≤Q) such that Ti=3. The j-th (1≤j≤X) line should contain the answer to the j-th operation such thatTi=3.
Sample Input 1
Copy
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
Sample Output 1
Copy
No Yes No No
Twidai has three users. The nine operations are as follows.
- User
- 2. No other users are following or followed by any users.
- Determine if users
- 1 and 2 are following each other. User 1 is following user 2, but user 2 is not following user
- 1, so
No
is the correct answer to this operation. - User
- 2 follows user
- 1.
- Determine if users
- 1 and 2 are following each other. User 1 is following user 2, and user 2 is following user
- 1, so
Yes
is the correct answer to this operation. - User
- 2 follows user
- 3.
- User
- 3 follows user
- 2.
- Determine if users
- 1 and 3 are following each other. User 1 is not following user 3, and user 3 is not following user
- 1, so
No
is the correct answer to this operation. - User
- 1 unfollows user
- 2.
- Determine if users
- 1 and 2 are following each other. User 2 is following user 1, but user 1 is not following user
- 2, so
No
is the correct answer to this operation.
Sample Input 2
Copy
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
Sample Output 2
Copy
Yes No
A user may follow the same user multiple times.
Sample Input 3
Copy
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
Sample Output 3
Copy
No No No No Yes Yes No No No Yes Yes
map直接秒杀:
#include<bits/stdc++.h> using namespace std; int main() { int n,q; cin>>n>>q; map<pair<int,int>,bool>mp; while(q--) { int flag=0; cin>>flag; if(flag==1) { int a,b; cin>>a>>b; mp[{a,b}]=1; } if(flag==2) { int a,b; cin>>a>>b; mp[{a,b}]=0; } if(flag==3) { int a,b; cin>>a>>b; if(mp[{a,b}]&&mp[{b,a}]) cout<<"Yes"<<endl; else cout<<"No"<<endl; } } }
- 2, so