csu 1554: SG Value 思维题
生活随笔
收集整理的這篇文章主要介紹了
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 思维题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 27、Label 自适应文本 xib
- 下一篇: JS三种简单排序算法