跳到正文

题解

题解:P11176 [ROIR 2018 Day1] 提高成绩

malinhao45约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年10月8日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P11176

版本

  1. v2当前
  2. v1

本题解专门讲解如何模拟,如果想看数学推导,请至其他题解

题意:此学生获得了 aa 个 22 分, bb 个 33 分, cc 个 44 分,问还要增加多少个 55 分才能是平均分不小于 44 。(平均分四舍五入); 做法:把题意理解好了之后就可以立刻的出来思路:直接模拟。是谁把提议理解错后喜提 00 我不说。 只要是语法过关了的同学我觉得都能写出来。

C++
#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了,所以我们考虑降低时间复杂度。那用什么呢?我脑子里立刻有了一个想法:二分! 本题的二分答案也是十分好写的,代码如下:

C++
#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;
}
}
原文