[USACO12MAR] 摩天大樓裏的奶牛 Cows in a Skyscraper
題目描述
A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don‘t like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.
The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.
給出n個物品,體積為w[i],現把其分成若幹組,要求每組總體積<=W,問最小分組。(n<=18)
題目解析
模擬退火
話說啊,貪心是錯的,雖然一眼看上去是沒有問題的。貪心:75分
裸貪心顯然是錯的,證明略。 但是我們發現了一個有趣的事情,把奶牛的重量從大到小排個序貪心,在一般強度的數據下是正確的,有點像給罐子裏先放石頭再放沙子再放水比先放水再放沙子石頭要更好一樣。外加這題數據很小,n^2的貪心是可以執行1e5級別次的。 綜上,退火。
但答案如果經過特殊構造,將使得退火效率降低,難以得到正確解,這時可以采取一個小技巧:對題目詢問的區間進行小幅晃動。
Code
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<ctime> using namespace std; const int MAXN = 25; int n,w,ans,tim; int a[MAXN],s[MAXN]; double T,e; bool cmp(int x,int y) { return x > y; } inline bool getposs() { T *= e; if(rand() % 10000 < T) return false; else return true; } inline void clean() { T = 9000, e = 0.9; memset(s,0,sizeof(s)); tim = 0; return; } int main() { srand(time(NULL)); scanf("%d%d",&n,&w); w *= 1.005; for(int i = 1;i <= n;i++) { scanf("%d",&a[i]); } sort(a+1,a+1+n,cmp); bool flag = false; int cnt = 200000; ans = 0x3f3f3f3f; while(cnt--) { clean(); for(int i = 1;i <= n;i++) { flag = false; for(int j = 1;j <= tim;j++) { if(w - s[j] >= a[i] && getposs()) { s[j] += a[i]; flag = true; break; } } if(!flag) s[++tim] += a[i]; } ans = min(ans,tim); } printf("%d\n",ans); return 0; }
[USACO12MAR] 摩天大樓裏的奶牛 Cows in a Skyscraper