math --- CSU 1554: SG Value

 SG Value

Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1554


 

Mean: php

一個可重集合,初始爲空,每次插入一個值,詢問這個集合的SG值(集合中的數字組合相加不能達到的最小值)是多少。ios

analyse:ide

這題方法很巧妙。spa

假設當前不能達到的最小值爲v,對於要插入的一個新值xcode

1)若是x>v,那麼v的值不會改變,咱們用一個優先隊列(從小到大)將x進隊;blog

2)若是x<=v,那麼v的值會改變爲v+x,而後再判斷隊列中的元素,把隊列中的元素看成新值x來處理,執行一、2操做,這樣就能保證v一直是不能達到的最小值。隊列

 

Time complexity: O(n)ci

 

Source code: string

 

//  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> 
#define MAXN 1000010 
#define LL long long 
using namespace std; 
  
priority_queue<LL,vector<LL>,greater<LL> > q; 
int main() 
{ 
        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; 
                                        } 
                                } 
                        } 
                        else cout<<v+1<<endl; 
                } 
        } 
        return 0; 
} 
/* 
  
*/
  
/************************************************************** 
    Problem: 1554 
    User: crazyacking 
    Language: C++ 
    Result: Accepted 
    Time:396 ms 
    Memory:1996 kb 
****************************************************************/
View Code
相關文章
相關標籤/搜索