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

D题

原题链接

题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天)

思路:因为怪兽是不可排序的,我就把英雄按类分装,运用贪心思想来做,每一个攻击次数下都有一个或多个英雄,我们都选择攻击力最高的英雄来代表,然后就暴力枚举所有怪兽,这里天数在同一天的条件有两个:1.在当前攻击次数下最大得攻击力比从一个怪兽开始到现在最大得要大于等于2.不要超过n

代码:

 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

这道题目,感觉挺好理解但是就是无从下手,贪心的思想~~~~

猜你喜欢

转载自www.cnblogs.com/wangzhe52xia/p/11861122.html