• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
scorpiorax
博客园    首页    新随笔    联系   管理    订阅  订阅
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 }

 

posted on 2018-09-08 22:21  scorpiorax  阅读(130)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3