1. 程式人生 > >【枚舉】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem D. Cutting Potatoes

【枚舉】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem D. Cutting Potatoes

題意 clas tag ble cpp rand ring ++i break

題意:有n個土豆,每個有體積V(i),你可以將每個土豆等分為不超過K份,問你最大塊和最小塊比值最小為多少。

直接枚舉切法,只有n*K種,然後保證其為最大塊,去算其他塊的切法,即讓其他塊切得盡可能大即可。O(n*n*K)。

#include<cstdio>
#include<cstring>
using namespace std;
int n,K,a[105],ans1=150,ans2=1,x[105],ansx[105];
int main(){
//	freopen("d.in","r",stdin);
	scanf("%d%d",&n,&K);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=K;++j){
			int tmp1=150,tmp2=1;
			x[i]=j;
			bool flag=1;
			for(int k=1;k<=n;++k){
				if(k!=i){
					x[k]=a[k]*j/a[i];
					if(a[k]*j%a[i]!=0){
						++x[k];
					}
					if(x[k]>K){
						flag=0;
						break;
					}
					if(a[k]*tmp2<x[k]*tmp1){
						tmp1=a[k];
						tmp2=x[k];
					}
				}
			}
			if(flag && a[i]*tmp2*ans2<ans1*j*tmp1){
				ans1=a[i]*tmp2;
				ans2=j*tmp1;
				memcpy(ansx,x,sizeof(x));
			}
		}
	}
	for(int i=1;i<n;++i){
		printf("%d ",ansx[i]);
	}
	printf("%d\n",ansx[n]);
	return 0;
}

【枚舉】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem D. Cutting Potatoes