2021算法竞赛入门班第三节课【堆、栈、队列、并查集】等习题
目录
- 新建 Microsoft Office Word 文档【小根堆】
- 加边的无向图【并查集】
- 好串【栈 / 括号匹配】
- [NOIP2004]合并果子【小根堆】
- DongDong认亲戚【并查集】
新建 Microsoft Office Word 文档【小根堆】
https://ac.nowcoder.com/acm/problem/17889
用小根堆来维护一个删除掉的编号,即现在剩余的编号。在用一个编号存目前最大的编号。
当小根堆内有元素就拿堆里的,没有的话就向后延申。
#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int>mp;
int n,idx=1;
priority_queue<int,vector<int>,greater<int> >heap;
int main(void)
{cin>>n;for(int i=1;i<=n;i++){string op; cin>>op;if(op=="New") {if(heap.size()) {cout<<heap.top()<<endl;mp[heap.top()]++,heap.pop();}else {cout<<idx<<endl;mp[idx++]=1;}}else {int x; cin>>x;if(mp[x]) {puts("Successful");heap.push(x);}else puts("Failed");mp[x]=0;}}return 0;
}
加边的无向图【并查集】
https://ac.nowcoder.com/acm/problem/14685
就是连通块的数量减1。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int p[N],n,m;
int find(int x)
{if(x!=p[x]) p[x]=find(p[x]);return p[x];
}
int main(void)
{cin>>n>>m;for(int i=1;i<=n;i++) p[i]=i;for(int i=1;i<=m;i++){int a,b; cin>>a>>b;p[find(b)]=find(a);}map<int,int>mp;for(int i=1;i<=n;i++) mp[find(i)]++;cout<<mp.size()-1;return 0;
}
好串【栈 / 括号匹配】
https://ac.nowcoder.com/acm/problem/21874
很常见的一种括号匹配模型模型,不过这里的括号是ab
而不是()
而已。
#include<bits/stdc++.h>
using namespace std;
stack<char>st;
int main(void)
{string s; cin>>s;for(int i=0;i<s.size();i++){if(s[i]=='a'|| !st.size()) st.push(s[i]);else{if(st.top()=='a'&&s[i]=='b') st.pop();else st.push(s[i]);}}if(st.size()) cout<<"Bad";else cout<<"Good";return 0;
}
[NOIP2004]合并果子【小根堆】
https://ac.nowcoder.com/acm/problem/16663
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int>>heap;
int n,x;
long long int sum;
int main(void)
{cin>>n;for(int i=0;i<n;i++) cin>>x,heap.push(x);while(heap.size()>1) {auto a=heap.top(); heap.pop();auto b=heap.top(); heap.pop();sum+=a+b;heap.push(a+b);}cout<<sum<<endl;return 0;
}
DongDong认亲戚【并查集】
https://ac.nowcoder.com/acm/problem/23803
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
unordered_map<string,int>hush;
int p[N],idx;
int find(int x)
{if(x!=p[x]) p[x]=find(p[x]);return p[x];
}
int get(string x)
{if(!hush.count(x)) hush[x]=idx,p[idx]=idx,idx++; return hush[x];
}
int n,m;
int main(void)
{cin>>n>>m;for(int i=0;i<n;i++){string s; cin>>s;get(s);}while(m--){int op; string s1,s2; cin>>op>>s1>>s2;if(op==1){p[find(get(s2))]=find(get(s1));}else{if(find(get(s1))==find(get(s2))) cout<<1<<endl;else cout<<0<<endl;}}return 0;
}
2021算法竞赛入门班第三节课【堆、栈、队列、并查集】等习题相关推荐
- 2021算法竞赛入门班第九节课【线段树】练习题
题单地址:https://ac.nowcoder.com/acm/problem/collection/1265?asc=true&order=difficulty 目录 情人节的电灯泡[二维 ...
- 2021算法竞赛入门班第八节课【数学】习题
目录) 最大公约数(lcm) [NOIP2011]计算系数[组合数] 大水题[容斥定理] 最大公约数(lcm) https://ac.nowcoder.com/acm/problem/16710 #i ...
- 2021算法竞赛入门班第二节课【递归、分治、二分】练习题
目录 华华给月月准备礼物[二分] The Biggest Water Problem[模拟] Bits[递归模拟 / 未完成] [NOIP2004]FBI树[树的后序遍历] [USACO 2009 D ...
- 2021算法竞赛入门班第一节课【枚举、贪心】习题
目录 [USACO 2007 Jan S]保护花朵[经典贪心] [NOIP2005]校门外的树[区间合并] [NOIP2006]明明的随机数[签到] [HNOI2003]激光炸弹[二维前缀和] 铺地毯 ...
- 2021算法竞赛入门班第一节课枚举贪心习题
文章目录 1.明明的随机数 2.回文日期 3.校门外的树 4.数学考试 5.Subsequence 6.字符串 7.丢手绢 8.拼数 9.纪念品分组 10.国王的游戏 11.铺地毯 12.「土」巨石滚 ...
- 2021算法竞赛入门班第十节课【字符串】练习题
目录 救救企鹅[KMP] 救救企鹅[KMP] https://ac.nowcoder.com/acm/problem/20862 KMP匹配,记录匹配的开始下标. #include<bits/s ...
- 2021算法竞赛入门班第七节课【图论】练习题
目录 挖沟[最小生成树板子题] 公交线路[最短路板子题] 道路建设[最小生成树] 挖沟[最小生成树板子题] https://ac.nowcoder.com/acm/problem/17509 #inc ...
- 2021算法竞赛入门班第四节课【搜索】练习题
目录 Jelly[简单bfs] maze[建图求最短路] wyh的迷宫[BFS] 幸运数字Ⅱ[爆搜] 迷宫[变种的bfs] 走出迷宫[模板dfs] 魔法数字[状态转移bfs] Jelly[简单bfs] ...
- 算法竞赛入门经典(第二版)-刘汝佳-第六章 数据结构基础 习题(12/14)
文章目录 说明 习题 习6-1 UVA 673 平衡的括号 习6-2 UVA 712 S - 树 习6-3 UVA 536 二叉树重建 习6-4 UVA 439 骑士的移动 习6-5 UVA 1600 ...
最新文章
- Flink三种运行模式安装部署
- 什么是Spring 框架?Spring 框架有哪些主要模块?
- python 写txt 换行_python中写入txt文件需要换行,以及\r 和\n
- socket自定义框架
- Linux应急响应实战
- python读取文件最后几行_如何用python获取文件的最后一行,文件可能会比较大
- 如何用matlab画正态分布曲线
- 《Android开发精要》读书笔记——Android的系统架构
- mysql的权限管理
- C语言万年历设计制作
- python豆瓣250爬取
- Air Jordan 32 Performance Review
- Windows Server 2012 R2 更改系统语言
- 《甄嬛传》影评(整理)
- 2020最难求职年,程序员职场面试 “防坑指南” ,全员必备!
- 如何查询微信安全情况?域名被微信拦截的因素?
- (转)马邑之战与项目管理
- 《python数据分析与挖掘实战》笔记第5章
- 一些有用的Web或者手机UI设计工具
- 综合案例:播放器 学习