loj 10001 种树

*********贪心,把需要的路段终止点排序,然后在每个区间内判断是否已经满足条件,不满足的从区间右端向左端种树。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 int i,j,n,h,vis[30005] = {0},ans = 0,tot;
 7 struct node
 8 {
 9     int b;
10     int e;
11     int t;
12 }a[30005];
13 int cmp(node a,node b)
14 {
15     return a.e < b.e;
16 }
17 int main()
18 {
19     scanf("%d",&n);
20     scanf("%d",&h);
21     for(i = 1;i <= h;i++)
22     {
23         scanf("%d %d %d",&a[i].b,&a[i].e,&a[i].t);
24     }
25     sort(a + 1,a + 1 + h,cmp);
26     for(i = 1;i <= h;i++)
27     {
28         tot = 0;
29         for(j = a[i].b;j <= a[i].e;j++)
30         {
31             if(vis[j] == 1)
32             {
33                 tot++;
34             }
35             if(tot == a[i].t)
36             break;
37         }
38         if(tot < a[i].t)
39         {
40             for(j = a[i].e;j >= a[i].b;j--)
41             {
42                 if(vis[j] == 0)
43                 {
44                     vis[j] = 1;
45                     tot++;
46                     ans++;
47                 }
48                 if(tot == a[i].t)
49                 break;
50             }
51         }
52     }
53     printf("%d",ans);
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/rax-/p/9610852.html