题目描述
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分
裸贪心显然是错的,证明略。
但答案如果经过特殊构造,将使得退火效率降低,难以得到正确解,这时可以采取一个小技巧:对题目询问的区间进行小幅晃动。
对这道题而言就是略微调整w的范围。
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 ",ans); return 0; }