Codeforcs(Yet Another Monster Killing Problem)

Greedy like
a mistake to begin to understand the meaning of the questions, I thought I could just find a monster hit, the title is required to play next to one of
the enumeration values for each stamina, endurance obtained the maximum value under attack hero

Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200005],u[200005];
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--)
    {
        ll x,y;
        cin>>n;
        ll gmx=-1;
        for(int i=0;i<n;i++)
        {cin>>a[i];gmx=max(gmx,a[i]);u[i]=0;}
        u[n]=0;
        cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>x>>y;
            u[y]=max(u[y],x);
        }
        for(int i=n-1;i>=1;i--)
        {u[i]=max(u[i],u[i+1]);}
        if(u[1]<gmx)
        {cout<<"-1"<<endl;
        continue;}
        ll time=0,cnt=0,df=-1,i=0;
        while(i<n)
        {
            cnt++;
            df=max(df,a[i]);
            if(u[cnt]>=df)
            {i++;}
            else
            {
                df=-1;
                cnt=0;
                time++;
            }
        }
        if(cnt!=0)
        {time++;}
        cout<<time<<endl;
    }
    return 0;
}
Published 36 original articles · won praise 4 · Views 1385

Guess you like

Origin blog.csdn.net/qq_43781431/article/details/104736568