标签:long ems cin main str div scanf oid 多个
题意:输入t表示有多个样例,输入n,group表示有n个数分为group组使每组和最小
#include<iostream> #include<string.h> using namespace std; #define ll long long const int N = 500 + 5; ll a[N]; int vis[N]; ll num,m,group; int solve(int d) { ll sum=0; int k=1; for(int i=0;i<num;i++) { if(a[i]+sum<=d) sum+=a[i]; else { sum=a[i]; k++; } } if(k<=group) return 1; else return 0; } void out() { memset(vis,0,sizeof(vis)); int k=1; ll sum=0; for(int i=num-1;i>=0;i--) { if(sum+a[i]<=m) { sum+=a[i]; } else { k++; sum=a[i]; vis[i]=1; } } for(int i=0;i<num&&k<group;i++) { if(!vis[i]) { vis[i]=1; k++; } } for(int i=0;i<num-1;i++) { cout<<a[i]<<" "; if(vis[i]) cout<<"/ "; } cout<<a[num-1]<<endl; } int main() { int t; scanf("%d",&t); while(t--) { ll sum=0;m=0; cin>>num>>group; for(int i=0;i<num;i++) { cin>>a[i]; sum+=a[i]; if(m<a[i]) m=a[i]; } int mid=(m+sum)/2; while(sum>m) { if(solve(mid)) { sum=mid; } else m=mid+1; mid=(sum+m)/2; } // cout<<mid<<endl; out(); } return 0; }
标签:long ems cin main str div scanf oid 多个
原文地址:http://www.cnblogs.com/renwjing/p/7392334.html