SG Value
problem ' s Link:
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1554
Mean:
A reusable collection, initially empty, each time a value is inserted, asking the SG value of the set (the minimum value that the combination of numbers in the collection cannot be reached).
Analyse:
The method is very ingenious.
Assuming that the minimum value currently not reached is V, for a new value to be inserted x
1) If x>v, then the value of V will not change, we use a priority queue (from small to large) x into the team;
2) If x<=v, then the value of V will be changed to V+x, and then determine the elements in the queue, the elements in the queue as the new value x to handle, perform 1, 2 operation, so that the V has always been the minimum value can not be reached.
Time Complexity:o (n)
Source Code:
//Memory Time//1347K 0MS//by:crazyacking//2015-03-29-18.44#include <map>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<cstdlib>#include<cstring>#include<climits>#include<iostream>#include<algorithm>#defineMAXN 1000010#defineLL Long Longusing namespacestd; Priority_queue<LL,vector<LL>,greater<LL> >Q;intMain () {Ios_base::sync_with_stdio (false); Cin.tie (0); //freopen ("C:\\users\\devin\\desktop\\cin.cpp", "R", stdin);//freopen ("C:\\users\\devin\\desktop\\cout.cpp", "w", stdout);LL t; while(cin>>t) {LL v=0;//biggest num while(!q.empty ()) Q.pop (); while(t--) {LL type; CIN>>type; if(type==1)//Insert{LL num; CIN>>num; if(num>v+1) {q.push (num); } Else{v+=num; while(!Q.empty ()) {LL tmp=Q.top (); if(tmp<=v+1) {v+=tmp; Q.pop (); } Else Break; } } } Elsecout<<v+1<<Endl; } } return 0; } /* */ /************************************************************** problem:1554 user:crazyacking language:c+ + result:accepted time:396 Ms memory:1996 KB ***************************************************************< /c7>*/
View Code
Math---CSU 1554:sg Value