SDUT 2021 Spring Individual Contest(for 20) - 11 补题

Question B Bonuses on a Line
: Four situations: only go to the left, go only to the right, go first to the right and then to the left, go to the left and then to the right. Enumerate the maximum value of each case, and also determine whether there is a value at 0, if there is a maximum value +1, if not, output the maximum value

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
ll pos[N];
ll neg[N];
ll p1=0;
ll p2=0;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,t;
    cin>>n>>t;
    ll flag=0;
    for(int i=1;i<=n;i++)
    {
    
    
        ll x;
        cin>>x;
        if(x<0)
        neg[++p1]=-x;
        else if(x>0)
        pos[++p2]=x;
        else
        flag=1;
    }
    sort(neg+1,neg+1+p1);
    ll ans=0;
    ll cur=0;
    for(int i=1;i<=p1;i++)
    {
    
    
        if(neg[i]>t)
        break;
        cur++;
    }
    ans=max(ans,cur);
    cur=0;
    for(int i=1;i<=p2;i++)
    {
    
    
        if(pos[i]>t)
        break;
        cur++;
    }
    ans=max(ans,cur);
    for(int i=1;i<=p1;i++)
    {
    
    
        ll left=t-neg[i]*2;
        if(left<0)
        break;
        ll idx=upper_bound(pos+1,pos+1+p2,left)-pos;
        idx--;
        cur=idx+i;
        ans=max(cur,ans);
    }
    for(int i=1;i<=p2;i++)
    {
    
    
        ll left=t-pos[i]*2;
        if(left<0)
        break;
        ll idx=upper_bound(neg+1,neg+1+p1,left)-neg;
        idx--;
        cur=idx+i;
        ans=max(cur,ans);
    }
    cout<<ans+flag<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51768569/article/details/115358927