ZWU暑假ACM集训第二周周记

第二周的重点是queue,priority_queue,Set,Map以及数组和字符串的使用。

queue和priority_queue

队列(queue)是一种具有先进入队列的元素一定先出的性质的数据结

队列和栈的区别: 栈只能知道最后插进去的元素 队列可以知道最先和
最后插进去的元素 然后栈是后进先出 队列是先进后出
•Queue的有关操作
•Q.front()返回队首元素
•Q.back()返回队尾元素 
•Q.push()在队尾插入元素
•Q.pop()弹出队首元素
•Q.empty()队列是否为空 
•Q.size()返回队列中元素的数量 

而优先队列priority_queue则是会将queue中的数据排序后,每次弹出最大或最小(取决于你要用哪种)的数据。堆是一颗树 ,还是一颗完美二叉树他的时间复杂度是O(logn)

这是他的定义方法:priority_queue<int, vector<int>, greater<int>>

例题

P3378 【模板】堆 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

 

#include<bits/stdc++.h>

using namespace std;
priority_queue<int, vector<int>, greater<int> > q;

int main(){
	int temp;
	int n;
	cin >> n;
	while(n--){
		cin >>temp; 
		if(temp == 1){
			int a;
			cin >> a;
			q.push(a);
		}else if(temp == 2){
			cout << q.top() << endl;
		}else if(temp == 3){
			q.pop();
		}
	}
}

Set  Map

Set 集合
•set是一个容器 以红黑树的数据结构来实现 
•set中的元素按照一定的顺序进行存储与访问, 而且每个元素只会在set里出现一次 其中元素
默认是按照升序进行排序的, 但也可以通过自定义比较函数来实现其他的排序方式
•我们不用理解set的实现原理 只用把set当作数学上的一个集合就行 
•时间复杂度O(logn)
•Set一般有什么用 去重  排序
•St.insert(x)当set里没有等价元素时,将x插入到set里 push()
•St.erase(x)从set中删除指定元素 如果你删掉一个不在集合内的元素 那么对这个集合没有影
响 相当于没有进行这次操作 
•St.clear() 清空set容器 
•St.count(x)返回set内x元素的数量 因为最多存在一个 所以返回值要么1要么0
•st.empty()判断set是否为空 如果为空的话 那么返回1 不然返回0 
•St.size()返回set内元素的个数 

初始化set:set<T>st, T为数据类型, 可以是int, double,char,string等等,也可以是自己定的结构体,但是一个set里数据的数据类型都得相同。

Set里的元素默认是从小到大排序的 怎么样设置从大到小排序
Set<int>st, set<int, greater<int> >st;
遍历set  for(auto x : st)


Map Olog(n)
•Map是一种基于红黑树的容器 支持快速的插入查找和删除操作,并且保
证了内部元素的有序性 其中每一个元素都有一个key和一个相关联的
val组成
•时间复杂度Olog(n)
•Map<T, T> mp; int , double, 
•//Mp.find(); 
•//Mp.insert({key, val}); 
•Mp.erase(key)删除一个key //释放空间
•Mp.count(key) 统计这个下标出现了多少次 
•Mp.size();
•Mp.clear();
•MP.empty();

map里的元素默认是从小到大排序的 怎么样设置从大到小排序
map<int, int, greater<int>>mp;
•遍历map for(auto [x = key, c = val] : mp)

例题:第 k 小整数 - 洛谷

#include<bits/stdc++.h>

using namespace std;

int main(){
	int n,k;
	cin >> n >> k;
	set<int> s;
	vector<int> v;
	for(int i = 0; i < n; i++){
		int x;
		cin >> x;
		s.insert(x);
	}
	for(auto p:s){
		v.push_back(p);
	}
	if(k > v.size()){
		cout <<"NO RESULT";
	}else{
		cout << v[k-1];
	}
}

先使用set使数据去重并且排序,然后访问第k个大的数据即可。

Shopping - HDU 2648 - Virtual Judge

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

int main(){
	int n;
	while(cin >> n){
		map<string,int> ma;
		string namec[100000];
		for(int i = 0; i < n; i++){
			string temp;
			cin >> temp;
			ma.insert({temp,0});
			namec[i] = temp;
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; i++){
			for(int j = 0; j < n; j++){
				int price;
				string name;
				cin >> price >> name;
				ma[name] += price;
			}
			int rank = 1;
			map<string,int>::iterator it;
			for (it = ma.begin();it != ma.end();it++)
	            {
	                if (it->second > ma["memory"]) rank++;
	            }
	            cout << rank << endl;
		}
	}
}

 使用map键值对,将商品名和价格联系起来,通过价格叠加并排序之后找到memory所在排名输出即可。

数组和string的使用

字符串读取到回车

 

例题 :

Problem - 1722A - Codeforces

#include<bits/stdc++.h>
using namespace std;

void solve(){
	int n;
    string s,t="Timur";
	cin>>n>>s;
	if(n!=5){
		cout<<"NO"<<endl;
		return;
	}
	sort(s.begin(),s.end());
	sort(t.begin(),t.end());
	if(s==t){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
}

int main(){
	int t;
	cin>>t;
	while(t--){
	    solve();
	}
}

使用string定义,排序啥的都方便许多。

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值