#include<iostream>
#include<cstdio>
#include<set>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,sum;
int a[510];
int cal(int mid)
{
int i,j,s;
int cnt=0;
for(i=0;i<m;)
{
s=a[i];
if(s>mid)return 0;
while(s<=mid&&i<m)
s+=a[i+1],i++;
cnt++;
}
return cnt<=k;
}
int main()
{
int i,t;
//freopen("D:\\p.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d%d",&m,&k);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
int left=1,right=sum,mid,res=sum;
while(left<=right)
{
mid=left+(right-left)/2;
if(cal(mid))//最有的在左边或者是mid
{
if(mid<res)
res=mid;
//right=mid;mid已经被保存下来了,从前一个位置搜索
right=mid-1;
}
else left=mid+1;//mid不行,最优的只能值mid以后
}
printf("%d\n",res);
}
return 0;
}
xmuoj 1429果汁
#include <cstdio>
#include<iostream>
using namespace std;
int n,x;
int cal(int v)
{
int res=v,b=v,d;
while(b>=x)
{
d=b/x;
res+=d;
b%=x;
b+=d;
}
return res;
}
int solve()
{
int left=1,right=n,mid;
if(n==1||x==1)
return 1;
while(left+1<right) //左开右闭区间(1,n],二分出来的结果不可能是1,如果是1的话已经被return了
{
mid=left+(right-left)/2;
int v=cal(mid);
if(v==n)return mid;
else if(v<n)
left=mid;
else
right=mid;
}
return right; //至少是一个上确界
}
int main()
{
while(~scanf("%d%d",&n,&x))
printf("%d\n",solve());
return 0;
}
#include<iostream>
#include<cstdio>
#include<set>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL __int64
using namespace std;
char ss[110];
LL nb,ns,nc,pb,ps,pc,r,b=0,s=0,c=0,minn,maxn;
int judge(LL mid)
{
LL tot=0,Nb=mid*b,Ns=mid*s,Nc=mid*c;
if(b!=0&&Nb>=nb)tot+=(Nb-nb)*pb;//必须保证Nb>=nb
if(s!=0&&Ns>=ns)tot+=(Ns-ns)*ps;
if(c!=0&&Nc>=nc)tot+=(Nc-nc)*pc;
return tot<=r;
}
int main()
{
int i,L;
//freopen("D:\\p.txt","r",stdin);
gets(ss);
L=strlen(ss);
scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&nb,&ns,&nc,&pb,&ps,&pc,&r);
//printf("%I64d %I64d %I64d %I64d %I64d %I64d %I64d\n",nb,ns,nc,pb,ps,pc,r);
for(i=0;i<L;i++)
{
if(ss[i]=='B')b++;
if(ss[i]=='S')s++;
if(ss[i]=='C')c++;
}
minn=1e18;
maxn=0;
if(b!=0&&nb/b<minn)minn=nb/b;
if(s!=0&&ns/s<minn)minn=ns/s;
if(c!=0&&nc/c<minn)minn=nc/c;
if(b!=0&&(nb+r/pb)/b>maxn)maxn=(nb+r/pb)/b;
if(s!=0&&(ns+r/ps)/s>maxn)maxn=(ns+r/ps)/s;
if(c!=0&&(nc+r/pc)/c>maxn)maxn=(nc+r/pc)/c;
LL res=minn;
while(minn<=maxn)//左闭右闭区间
{
LL mid=minn+(maxn-minn)/2;
if(judge(mid))
{
res=mid;
minn=mid+1;//mid可以,保存mid,尝试着找到更大的res
}
else maxn=mid-1;//mid不可以,常识着找到更小的
}
printf("%I64d\n",res);
return 0;
}