给被第四点卡住的人的提示

P1182 数列分段 Section II

**为什么我的l设为1也过了** ```cpp #include<bits/stdc++.h> using namespace std; int n,m,a[101000],ans=0x7fffffff; bool check(int x) { int tot=0,k=m; for(int i=1;i<=n;i++) { tot+=a[i]; if(tot>x) { k--; tot=0; i--; } if(k==0) return false;//增大 } return true;//满足,要减小 } int main() { ios::sync_with_stdio(false); //freopen("in.txt","r",stdin); cin>>n>>m; int l=1,r=0;//我这里l是设为1的 for(int i=1;i<=n;i++) { cin>>a[i]; r+=a[i]; } while(l<=r) { int mid=(l+r)/2; if(check(mid)) { r=mid-1; ans=min(ans,mid); } else l=mid+1; } cout<<ans; } ```
by GadTD @ 2017-08-26 20:23:44


谢谢大佬请示
by _wc_ @ 2017-10-02 11:03:54


还是过不了是怎么回事@[xeonz1](/space/show?uid=42139) ```cpp #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int n,m; int a[100000+5]; bool check(int x){//检查x点,是否满足条件。 int s=0,num=1; // printf("check %d\n",x); for(int i=1;i<=n;i++){ s+=a[i]; // printf("i=%d s=%d\n",i,s); if(s>=x){ num++; s=a[i]; } } // cout<<num<<endl; if(num>m)return true;//不存在一种分法,使得若所有序列都小于等于x时,num满足条件; else return false; } int work(int l,int r){ int mid; int ans=0; while(l<=r){ mid=((l+r)>>1); if(check(mid)){ l=mid+1; ans=mid; }else{ r=mid-1; } } return ans; } int main(){ // freopen("tes.in","r",stdin); cin>>n>>m; int sum=0; int M=0; for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; M=max(M,a[i]); } cout<<work(M,sum); // system("ping 127.0.0.1");//dev4.9没有暂停。freopen后pause不能用。。 } ```
by zhshh @ 2017-10-30 19:16:42


@GadTD大佬 对于@xeonz1大佬给的数据,5 5 1 3 7 95630 2 只要check中的x小于95630 tot当加上95630时就会大于x 这时你又使i--,k--,让i一直卡在95630的位置,直到k==0
by salix_leaf @ 2018-07-18 16:11:16


@[GadTD](/space/show?uid=34259) 大佬 对于@[xeonz1](/space/show?uid=42139) 大佬给的数据,5 5 1 3 7 95630 2 只要check中的x小于95630 tot当加上95630时就会大于x 这时你又使i--,k--,让i一直卡在95630的位置,直到k==0
by salix_leaf @ 2018-07-18 16:11:55


谢谢大佬提示
by suco @ 2019-07-27 22:27:51


|