5.24课堂总结
T3 数列分段1
原题:https://www.luogu.com.cn/problem/P1181
这道题不需要用二分来做,直接用暴力来做。
核心的程序在这里:
if(x+sum<=m) sum+=x;
else{
ans++;
sum=x;
}
这里用来判断需不需另外独立一段。
整体代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,ans,x;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>x;
if(x+sum<=m) sum+=x;
else{
ans++;
sum=x;
}
}
cout<<ans+1;
return 0;
}
T4 数列分段2
原题:https://www.luogu.com.cn/problem/P1182
这道题在上一道题的基础上,需要把循环改成二分答案,上面的核心程序就是这道题的 check() 函数。然后再输入的时候顺便找一下二分答案的
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a[10000001],l,r;
bool check(int x){
int sum=0,ans=0;
for(int i=1;i<=n;i++){
if(sum+a[i]<=x) sum+=a[i];
else{
ans++;
sum=a[i];
}
}
if(sum!=0) return ans+1<=m;
else return ans<=m;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
l=max(l,a[i]);
r+=a[i];
}
l=l-1,r=r+1;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
cout<<r;
return 0;
}
作业
T1 切绳子
这道题二分答案,对于绳子的长度进行二分答案。
check() 很好理解,就是把绳子的长度除以枚举的
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++){
sum+=a[i]/x;
}
return sum>=m;
}
但是这道题的一个重点就是 double 类型的变量很难处理,我们干脆再输入的时候把每个数乘
#include<bits/stdc++.h>
#define int long long
using namespace std;
double a[10000001],n,m,maxn=-1e18;
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++){
sum+=a[i]/x;
}
return sum>=m;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]*=100;
maxn=max(maxn,a[i]);
}
double l=0,r=maxn+1;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)){
l=mid;
}
else r=mid;
}
cout<<fixed<<setprecision(2)<<l/100;
}
T2 切割绳子
这道题和上一道题差不多,只是不需要处理 double 类型的事了。
最后要加一个 Failed 的判断。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[10000001],n,m,maxn=-1e18;
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++){
sum+=a[i]/x;
}
return sum>=m;
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
maxn=max(maxn,a[i]);
}
cin>>m;
int l=0,r=maxn+1;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)){
l=mid;
}
else r=mid;
}
if(l==0){
cout<<"Failed";
}else cout<<l;
}
T3 木材
首先提醒,这道题要开 unsigned long long,要不然会 GG
这道题二分月数,check() 函数的意思是判断这个树原本的高度,加上每月长的乘时间能不能满足要求。
#include<bits/stdc++.h>
#define int unsigned long long
#define I return
#define AK 0
#define IOI ;
using namespace std;
int n,s,m,a[1000001],h[1000001];
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++){
if(h[i]+a[i]*x>=m) sum+=h[i]+a[i]*x;
}
return sum>=s;
}
signed main(){
cin>>n>>s>>m;
int sum=0;
for(int i=1;i<=n;i++){
cin>>h[i];
if(h[i]>=m) sum+=h[i];
}
for(int i=1;i<=n;i++){
cin>>a[i];
}
if(sum>=s){
cout<<0;
I AK IOI
}
int l=0,r=4e18;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)==1){
r=mid;
}else{
l=mid;
}
}
cout<<r;
I AK IOI
}
T4 分巧克力
这道题的 check() 是用来计算巧克力分的份数够不够。
主函数就很正常,很简单的题。
#include<bits/stdc++.h>
#define int long long
#define I return
#define AK 0
#define IOI ;
using namespace std;
int n,k,w[1000001],h[1000001],maxn=-1e18;
bool check(int x){
int sum=0;
for(int i=1;i<=n;i++){
sum+=(w[i]/x)*(h[i]/x);
}
return sum>=k;
}
signed main(){
cin>>n>>k;
int sum=0;
for(int i=1;i<=n;i++){
cin>>h[i]>>w[i];
maxn=max(maxn,h[i]);
maxn=max(maxn,w[i]);
}
int l=0,r=maxn+1;
while(l+1<r){
int mid=(l+r)/2;
if(check(mid)==1){
l=mid;
}else{
r=mid;
}
}
cout<<l;
I AK IOI
}