欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

csu 1554: SG Value 思维题

发布时间:2025/7/25 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 csu 1554: SG Value 思维题 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554

这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!!

先不考虑插入操作,就给定一堆数字,求出不能组成的最小的那个正数。

思路是,排序,然后维护一个区间[L, R]表示当前能组合成的数字。比如1、2就能组合成[1, 3]的所有数字。

那么下一个数a_i,我们需要其不能大于R + 1,否则会断,R + 1就是不能组合成的最小数字,比如a_i = 5就GG。

那么这题增加了插入操作。

我们只需要维护其递增排序,查询的时候,按照上面思路查询就好了。

比如1、2、5,那么前两个数可以组合成[1, 3],就可以把那两个数字pop掉了,不用重复计算。

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL;#include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <bitset> multiset<LL>ss; int n; void work() {LL R = 0;ss.clear();for (int i = 1; i <= n; ++i) {int op;cin >> op;if (op == 2) {while (ss.size() && *ss.begin() <= R + 1) {R = R + *ss.begin();ss.erase(ss.begin());}cout << R + 1 << endl;} else {LL val;cin >> val;ss.insert(val);}} }int main() { #ifdef localfreopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endifwhile (cin >> n) work();return 0; } View Code

 

转载于:https://www.cnblogs.com/liuweimingcprogram/p/6738258.html

总结

以上是生活随笔为你收集整理的csu 1554: SG Value 思维题的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。