Educational Codeforces Round 76 (Rated for Div. 2) D

D title

Original title link

Meaning of the questions: n is to give you a monster has an attribute (attack), m a hero, every hero has two attributes (attack, respectively, and the number of attacks), when arranged by the best of circumstances, a minimum of the number of days (every play is to select a hero one day)

Idea: because the monster is not sorted, I'll hero by class packaging, using greedy thinking to do, have one or more heroes, we have chosen to attack the hero of the next highest number of attacks each to represent, then violence enumerate all the monsters, the number of days here on the same day of two conditions: 1. in the current maximum number of attacks than have to attack from a monster to begin now to get maximum greater than or equal 2. Do not exceed n

 

Code:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long ll;
 7 const int N = 2e5 + 10;
 8 int a[N],b[N];
 9 int main()
10 {
11     int t;
12    scanf("%d",&t);
13     while(t--)
14     {
15         int n;
16         scanf("%d",&n);
17         for(int i=1;i<=n;i++)b[i]=0;
18         int ma_x =0;
19         for(int i=1;i<=n;i++)
20         {
21             scanf("%d",&a[i]);
22         }
23         int m;
24         scanf("%d",&m);
25         for(int i=1;i<=m;i++)
26         {
27             int p,s;
28             scanf("%d%d",&p,&s);
29            // mi_n = max(mi_n,p);
30             b[s]=max(b[s],p);
31         }
32         b[n+1]=0;
33         for(int i=n-1;i>=0;i--)
34             b[i]=max(b[i],b[i+1]);
35             //cout <<"gehgb"<<endl;
36        int pos = 1;
37        int ans = 0;
38        while(pos<=n)
39        {
40            int cnt = pos;
41            int maxx = a[cnt];
42            while(cnt<=n&&b[cnt-pos+1]>=maxx)
43            {
44                cnt++;
45                //cout<<a[cnt] <<"egrew"<<b[cnt-pos+1]<<endl;
46                maxx = max(maxx,a[cnt]);
47            }
48            if(cnt == pos)
49            {
50 
51                ans=-1;
52                break;
53            }
54            ans++;
55            pos =cnt;
56     }
57    printf("%d\n",ans);
58     }
59     return 0;
60 }
View Code

This question, I feel very good understanding but is unable to start, the idea of ​​greedy ~ ~ ~ ~

Guess you like

Origin www.cnblogs.com/wangzhe52xia/p/11861122.html