码迷,mamicode.com
首页 > 其他好文 > 详细

loj 10001 种树

时间:2018-09-08 23:31:53      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:space   sort   排序   image   技术分享   ==   algo   name   分享图片   

技术分享图片

 

技术分享图片

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

 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 }

 

loj 10001 种树

标签:space   sort   排序   image   技术分享   ==   algo   name   分享图片   

原文地址:https://www.cnblogs.com/rax-/p/9610852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!