5.23总结

· · 闲话

5.23总结

T1 U564349

一道简单的二分题

写起来和二分答案模版差不多,只要根据题意改一下 check 函数就行了

code

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int N=2e7+10;
ll n,s,k,a[N],b[N];
ll c(ll mid){
    ll sum=0;
    for(ll i=1;i<=n;i++){
        if(a[i]+mid*b[i]>=k){
            sum+=a[i]+mid*b[i];
        } 
    }
    return sum>=s;
}
int main(){
    cin>>n>>s>>k;
    for(ll i=1;i<=n;i++)cin>>a[i];
    for(ll i=1;i<=n;i++)cin>>b[i];
    ll l=-1,r=1e18;
    while(l+1<r){
        ll mid=(l+r)/2;
        if(c(mid))r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}

T2 U564350 布料最优裁剪

思路:二分答案模版+亿点点头脑风暴

code

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+10;
ll n,m,a[N],b[N],s[N],l,r=1e5+1,mid;
int f(int x){
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans+=(a[i]/x)*(b[i]/x);
    }
    return ans>=m;
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++cin>>a[i]>>b[i];
    while(l+1<r){
        mid=(l+r)/2;
        if(f(mid))l=mid;
        else r=mid;
    }
    cout<<l;
    return 0;
}

T3 T611157 数列分段1

\color{red}这题根本不要二分 :(

暴力就行

code

#include <bits/stdc++.h>
typedef unsigned long long ll;
using namespace std;
const int N=1e5+10;
ll n,m,a[N],sum,ans=1;
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++){
        sum+=a[i];
        if(sum>m){
            ans++;
            sum=a[i];
        }
    }
    cout<<ans;
    return 0;
}

T4 U564351 数列分段2

啊啊啊我的思路去哪了我不知道

code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=2e5+10;
ll n,m,k,a[N],ans,maxx=-1;
bool c(ll x){
    ll ans=1,sum=0;
    for(int i=1;i<=n;i++){
        sum+=a[i];
        if(sum>x){
            ans++;
            sum=a[i];
        }
    }
    return ans<=m;
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        maxx=max(maxx,a[i]);
        ans+=a[i];
    }
    ll l=maxx-1,r=ans+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(c(mid))r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}

\color{gold}The-end.