当前位置: 首页 > news >正文

5.5刷题map和set的使用

P5250 【深基17.例5】木材仓库

#include<bits/stdc++.h>
using namespace std;
void solve(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, length, type;set<int> a;set<int>::iterator it, itup;cin >> n;for(int i = 0; i < n; i++){cin >> type >> length;it = find(a.begin(), a.end(), length);if(type == 1){if(it == a.end())a.insert(length);else cout << "Already Exist" << endl;}else if(type == 2){if(a.empty())cout << "Empty" << endl;else if(it != a.end()){cout << length << endl;a.erase(it);}else{itup = upper_bound(a.begin(), a.end(), length);if(itup == a.end()){--itup;cout << *itup << endl;a.erase(itup);}else if(itup == a.begin()){cout << *itup << endl;a.erase(itup);}else{it = --itup;itup++;if(length - *it <= *itup - length){cout << *it << endl;a.erase(it);}else{cout << *itup << endl;a.erase(itup);}}}}}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}

P5266 【深基17.例6】学籍管理

#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;string s;map<string, int> m;while(n--){int x; cin >> x;if(x == 1){int score;cin >> s >> score;m[s] = score;cout << "OK" << endl;}else if(x == 2){cin >> s;if(m.count(s))cout << m[s] << endl;else cout << "Not found" << endl;}else if(x == 3){cin >> s;if(m.count(s)){m.erase(s);cout << "Deleted successfully" << endl;}else cout << "Not found" << endl;}else{cout << m.size() << endl;}}} 
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}

P1102 A-B 数对

#include<bits/stdc++.h>
using namespace std;void solve(){int n, c;cin >> n >> c;unordered_map<int, int> um;while(n--){int x; cin >> x;um[x]++;}long long sum = 0;for(unordered_map<int, int>::iterator it = um.begin(); it != um.end(); it++){int lenb = it -> second;int a = it -> first + c;if(um.count(a)){int lena = um[a];sum += (long long)lena * lenb;} }cout << sum << endl;
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}

P3879 [TJOI2010] 阅读理解

#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;map<string, set<int> > a;for(int i = 1; i <= n; i++){int x; cin >> x;for(int j = 1; j <= x; j++){string s; cin >> s;a[s].insert(i);}}int q; cin >> q;while(q--){string s; cin >> s;set<int> se = a[s];for(set<int>::iterator is = se.begin(); is != se.end(); is++){cout << *is << " ";}cout << endl;}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}

P1918 保龄球

#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;map<int, int> mp;for(int i = 1; i <= n; i++){int x; cin >> x;mp[x] = i;}int q; cin >> q;while(q--){int y; cin >> y;if(mp.count(y))cout << mp[y] << endl;else cout << 0 << endl;}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}

P4305 [JLOI2011] 不重复数字

#include<bits/stdc++.h>
using namespace std;void solve(){int t; cin >> t;map<int, int> mp;while(t--){mp.clear();int n; cin >> n;for(int i = 1; i <= n; i++){int x; cin >> x;if(!mp[x]){cout << x << " ";mp[x] = 1;}}cout << endl; }
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
}

http://www.xdnf.cn/news/287623.html

相关文章:

  • 笔试专题(十五)
  • 3小时超快速入门Python
  • 字符串,数组,指针之间的关系
  • Python实现自动驾驶中的车道检测算法:从理论到实践
  • win10开了移动热点,手机无法连接,解决办法(chatgpt版)
  • 手机SIM卡打电话时识别对方按下的DTMF按键(二)
  • SpringBoot整合RabbitMQ(Java注解方式配置)
  • CMake基础介绍
  • D. Pythagorean Triples 题解
  • 手机打电话时由对方DTMF响应切换多级IVR语音应答(一)
  • \documentclass[lettersize,journal]{IEEEtran}什么意思
  • 机器人强化学习入门学习笔记(二)
  • DeepSeek-Prover-V2:数学定理证明领域的新突破
  • Dify网页版 + vllm + Qwen
  • Matlab自学笔记五十三:保存save和载入load
  • 杨校老师竞赛课之C++备战蓝桥杯初级组省赛
  • Python爬虫实战:获取优美图库各类高清图片,为用户提供设计素材
  • 洛谷 P9007 [入门赛 #9] 最澄澈的空与海 (Hard Version)
  • 【从零开始学习微服务 | 第一篇】单体项目到微服务拆分实践
  • 本地MySQL连接hive
  • ASP.NET Core 请求限速的ActionFilter
  • 算法中的数学:质数(素数)
  • 30天通过软考高项-第十一天
  • CodeBlocks25配置wxWidgets3.2
  • 004-nlohmann/json 快速认识-C++开源库108杰
  • 地埋式燃气泄漏检测装置与地下井室可燃气体检测装置有什么区别
  • 专业课复习笔记 4
  • Vue中的过滤器参数:灵活处理文本格式化
  • 5月5日日记
  • 基于 HTML5 Canvas 实现图片旋转与下载功能