當(dāng)前位置:
首頁 >
csu 1554: SG Value 思维题
發(fā)布時(shí)間:2025/7/25
38
豆豆
生活随笔
收集整理的這篇文章主要介紹了
csu 1554: SG Value 思维题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554
這題在比賽的時(shí)候居然沒想出來,然后發(fā)現(xiàn)居然是做過的題目的變種!!!!
先不考慮插入操作,就給定一堆數(shù)字,求出不能組成的最小的那個(gè)正數(shù)。
思路是,排序,然后維護(hù)一個(gè)區(qū)間[L, R]表示當(dāng)前能組合成的數(shù)字。比如1、2就能組合成[1, 3]的所有數(shù)字。
那么下一個(gè)數(shù)a_i,我們需要其不能大于R + 1,否則會(huì)斷,R + 1就是不能組合成的最小數(shù)字,比如a_i = 5就GG。
那么這題增加了插入操作。
我們只需要維護(hù)其遞增排序,查詢的時(shí)候,按照上面思路查詢就好了。
比如1、2、5,那么前兩個(gè)數(shù)可以組合成[1, 3],就可以把那兩個(gè)數(shù)字pop掉了,不用重復(fù)計(jì)算。
#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?
轉(zhuǎn)載于:https://www.cnblogs.com/liuweimingcprogram/p/6738258.html
總結(jié)
以上是生活随笔為你收集整理的csu 1554: SG Value 思维题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 27、Label 自适应文本 xib
- 下一篇: 为更好提高客户对产品的满意度需要搞好站点