題意:初始有N顆菜重量分別是w1,...,wn,問你最少切幾刀,使得這些菜的最輕和最重的比例符合
思路:師兄做法,考慮到答案說了小於500次,那我們可以用一個優先隊列把這些菜記錄起來,然後每次選取最重的
菜開始和最輕的比較,如果符合比例就退出來,否則我們就把這個菜i切成++cut[i]份(初始爲1),切的
時候是平分,因爲切的不均,就會產生大的和小的,會影響結果。所以貪心切。
AC代碼:
PS:做法麻煩,僅供參考
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <math.h>
#include <string.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=5005;
pair<float,float> a[maxn];
float cut[maxn];
int main()
{
float t;
int n;
cin>>t>>n;
for(int i=0; i<n; i++)
{
scanf("%f",&a[i].first);
a[i].second=1;
cut[i]=a[i].first;
}
float*minn,*maxx;
maxx=max_element(cut,cut+n);
minn=min_element(cut,cut+n);
int pos=0;
int ans=0;
while(*minn/(*maxx)<=t)
{
//cout<<*maxx<<' '<<*minn<<' '<<(*minn/(*maxx))<<endl;
pos=maxx-cut;
//cout<<"pos"<<pos<<endl;
int num=++a[pos].second;
*maxx=a[pos].first/num;
maxx=max_element(cut,cut+n);
minn=min_element(cut,cut+n);
ans++;
}
cout<<ans<<endl;
return 0;
}