CodeForces-1257D (the double pointer greedy +)

The meaning of problems

https://vjudge.net/problem/CodeForces-1257D

You need to operate a hero to beat m n monster, the power value of each hero pi, si monsters can defeat; each monster power value ai.

When the new day begins, you can choose a hero to Daguai. If there k monsters before being defeated, the hero will challenge the first k + 1 monster, this time there are two cases:

1. Hero force ≤ monster strength, the heroes retreat, this end of the day.

2. hero strength> monster power monster is defeated. A monster under the continued challenge. When n monsters have all been defeated, or the hero has to defeat a number of monsters = si, the end of the day.

Your task is to calculate the minimum number of days to defeat all the monsters needed.

Thinking

The simplest idea is to play as much as possible with large multi-si.

Maintenance greatest heroes each stamina si corresponding strength, because in case of equal power endurance value is certainly the biggest election better.

And then maintain the maximum suffix array above, this is actually less stamina may be (note that you can! Not necessarily) with great stamina instead, such as for stamina i, i + 1, i + 1 if the force is greater than i, then the play can replace i with i + 1, because the endurance value is greater than i.

Then solving double pointer, to the current iteration i-monster, extending to the right by j, which can hit the farthest to see the monster, to where the real time recording the maximum force monster this period, if v [j-i + 1] > = mx (v is an array of the maximum value of the suffix processed), then it can be extended, v [j-i + 1] represents a value of endurance j-i + 1 ~ n (this period of endurance greater than mx heroes can be used) the maximum value of the power of the hero.

Code

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int a[N],v[N];
int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            v[i]=0;
        }
        int m;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            int p,s;
            cin>>p>>s;
            v[s]=max(v[s],p);
        }
        for(int i=n-1;i>=1;i--)
        {
            v[i]=max(v[i],v[i+1]);
        }
        int i=1,j,ans=0,flag=0;
        while(i<=n)
        {
            if(a[i]>v[1])
            {
                flag=1;
                break;
            }
            j=i;
            int mx=a[i];
            while(j<=n&&v[j-i+1]>=mx)
            {
                j++;
                mx=max(mx,a[j]);
            }
            ans++;
            i=j;
 //           cout<<i<<" "<<ans<<endl;
        }
        if(flag)
            cout<<-1<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11887403.html