CF389C Fox and Box Accumulation
阿新 • • 發佈:2021-11-18
不知道為什麼洛谷上沒有這道題。。。
題目大意
給出 \(n\) 個數,分別為 \(a_1,a_2,\cdots,a_n\)。
\(a_i\) 表示第 \(i\) 個箱子上最多能 疊 \(a_i\) 個箱子。
疊是指這樣:
形如上圖這樣的我們稱為一堆。
求最少需要多少堆把所有箱子都疊起來。
題目分析
考慮貪心。
我們可以先將 \(a\) 陣列升序排列。
對於每個數,我們依次遍歷所有數,如果有一個箱子能夠疊在當前這一堆的最下面(即 \(a_i\) 大於當前這一堆箱子數量),那麼立刻處理,並且刪除掉這個數。
答案就是能夠進行多少次這個操作。
我們如果使用陣列的話,非常不方便完成刪除操作,所以可以使用 \(\rm STL\)
multiset
。
程式碼
//2021/11/17 //2021/11/18 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <climits>//need "INT_MAX","INT_MIN" #include <set> #define enter() putchar(10) #define debug(c,que) cerr<<#c<<" = "<<c<<que #define cek(c) puts(c) #define blow(arr,st,ed,w) for(register int i=(st);i<=(ed);i++)cout<<arr[i]<<w; #define speed_up() std::ios::sync_with_stdio(false) namespace Newstd { inline int read() { char c; bool flag=false; while((c=getchar())<'0' || c>'9') { if(c=='-') flag=true; } int res=c-'0'; while((c=getchar())>='0' && c<='9') { res=(res<<3)+(res<<1)+c-'0'; } return flag?-res:res; } inline void print(int x) { if(x<0) { putchar('-');x=-x; } if(x>9) { print(x/10); } putchar(x%10+'0'); } } using namespace Newstd; using namespace std; int n; multiset<int>st; int main(void) { n=read(); for(register int i=1;i<=n;i++) { int x=read(); st.insert(x); } int ans(0); while(!st.empty()) { int cnt(0); multiset<int>::iterator it=st.begin(); while(it!=st.end()) { if((*it)>=cnt) { cnt++; st.erase(it++); } else { it++; } } ans++; } printf("%d\n",ans); return 0; }