贪心,流速y1,y2都设置最大,热了y2–,冷了y1–,符合>=t0且更接近t0就更新
double t1,t2,x1,x2,t0;
cin>>t1>>t2>>x1>>x2>>t0;
double y1=x1,y2=x2;
double ans1,ans2;
double MAX=INF;
while(y1>=0&&y2>=0){
double cal=(y1*t1+y2*t2)/(y1+y2);
if(cal>=t0){
if(cal<MAX){
MAX=cal;ans1=y1;ans2=y2;
}
y2--;
}else y1--;
}
cout<<ans1<<" "<<ans2<<endl;
贪心:正向遍历,体重更大就+1,相等就=,少就不发(初始值为0)
反向遍历,体重更大选择max(后者+1,本身瓜子数)
初始值为0,最后sum需要+n
int n;cin>>n;
fer(i,0,n)cin>>a[i];
fer(i,1,n){
if(a[i]>a[i-1]){
num[i]=num[i-1]+1;
}else if(a[i]==a[i-1])num[i]=num[i-1];
}
for(int i=n-1;i>=1;i--){
if(a[i-1]>a[i]){
num[i-1]=max(num[i]+1,num[i-1]);
}
}
int sum=0;
fer(i,0,n)sum+=num[i];
cout<<sum+n<<endl;
int sum=0;
int m,n;cin>>m>>n;
fer(i,0,n){
cin>>a[i];
sum+=a[i];
}
sum-=m;
int res=0;
sort(a,a+n);
fer(i,0,n){
int t=min(a[i],sum/(n-i));
res+=t*t;
sum-=t;
}
cout<<res<<endl;
将可吃到的奶酪数量二分
check函数实现:排序数组后,遍历是否可以在t内吃掉x个奶酪
三种情况:左端点<0,右端点<0,判断-a[i]int a[N];
int t,n;
bool check(int x){
int j;
fer(i,0,n-x+1){
j=i+x-1;
if(a[i]<=0&&a[j]<=0&&-a[i]<=t)return 1;
if(a[i]>=0&&a[j]>=0&&a[j]<=t)return 1;
if(a[i]<=0&&a[j]>=0&&min(-a[i],a[j])+a[j]-a[i]<=t)return 1;
}
return 0;
}
signed main(){
cin>>t>>n;
fer(i,0,n)cin>>a[i];
sort(a,a+n);
int l=0,r=n,mid,ans;
while(l<=r){
mid=l+(r-l)/2;
if(check(mid)){
ans=mid;
l=mid+1;
}else r=mid-1;
}
cout<<ans<<endl;
return 0;
}
礼物
二分,check函数满足三个条件:
v-v/(x*y)>=cnt1+cnt2 最小公倍数的倍数不能给a也不能给b
v-v/x>=cnt1 x的倍数不能给a
v-v/y>=cnt2 y的倍数不能给bint N=2e9+1;
int cnt1,cnt2,x,y;
bool check(int v){
if(v-v/(x*y)<cnt1+cnt2)return 0;
if(v-v/x<cnt1)return 0;
if(v-v/y<cnt2)return 0;
return 1;
}
signed main(){
cin>>cnt1>>cnt2>>x>>y;
int l=0,r=N,mid,ans;
while(l<=r){
mid=l+(r-l)/2;
if(check(mid)){
ans=mid;
r=mid-1;
}else l=mid+1;
}
cout<<ans<<endl;
return 0;
}