标签:os io for amp size ios poj algorithm
要吐血了,把k写成m,debug一个多小时,简直无情。。。
#include <iostream> #include<stdio.h> #include<string> #include<cstring> #include<cmath> #include<algorithm> #define N 22222 using namespace std; int a[N]; int sum[N]; int T; int m,k; int vis[N]; int sech(int mid) { int temp=0; int num=1; for(int i=1;i<=m;i++) if(temp+a[i]<=mid) temp+=a[i]; else { num++; temp=a[i]; } if(num<=k) return 1; return 0; } int main() { while(~scanf("%d",&T)) { while(T--) { int le=0,ri; sum[0]=0; scanf("%d%d",&m,&k); for(int i=1;i<=m;i++) { scanf("%d",&a[i]); if(a[i]>le) le=a[i]; sum[i]=sum[i-1]+a[i]; } ri=sum[m]; int mid; int ans; while(le<=ri) { mid=(ri+le)/2; if(sech(mid)) { ans=mid; ri=mid-1; } else le=mid+1; } memset(vis,0,sizeof vis); int temp=0; int x=k; int j; for(j=m;j>=1;j--) { if(x>j)//当人数大于书的本书时,一人翻译一本直到书被翻译完 { for(;j>=1;j--) vis[j]=1; break; } if(temp+a[j]<=ans) temp+=a[j]; else { vis[j]=1; temp=a[j]; x--; } } for(int i=1;i<=m;i++) { if(i==1) printf("%d",a[i]); else printf(" %d",a[i]); if(vis[i]) printf(" /"); } printf("\n"); } } return 0; }
POJ1505Copying Books,布布扣,bubuko.com
标签:os io for amp size ios poj algorithm
原文地址:http://blog.csdn.net/wust_zjx/article/details/38336231