本题解专门讲解如何模拟,如果想看数学推导,请至其他题解
题意:此学生获得了 个 分, 个 分, 个 分,问还要增加多少个 分才能是平均分不小于 。(平均分四舍五入);
做法:把题意理解好了之后就可以立刻的出来思路:直接模拟。是谁把提议理解错后喜提 我不说。
只要是语法过关了的同学我觉得都能写出来。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,c;
signed main(){
cin>>a>>b>>c;
int res=a*2+b*3+c*4;
if(round(res*1.0/(a+b+c+0.0))>=4){
cout<<0;
}else{
int i=0;
while(round(res*1.0/(a+b+c+i+0.0))<4){
i++;
res+=5;
}
cout<<i<<endl;
}
}但是喜提70.为什么呢?我们观察到最后一个subtaskTLE了,所以我们考虑降低时间复杂度。那用什么呢?我脑子里立刻有了一个想法:二分!
本题的二分答案也是十分好写的,代码如下:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,c;
long double res;
bool check(int mid){
if(round((res+mid*5)*1.0/(1.0*(a+b+c+0.0+mid)))>=4){
return true;
}else{
return false;
}
}
signed main(){
cin>>a>>b>>c;
res=a*2+b*3+c*4;
if(round(res*1.0/(a+b+c+0.0))>=4){
cout<<0;
}else{
int l=0;
int r=1e18;
while(l<r){
int mid=(l+r)/2;
if(check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout<<r;
}
}