zoukankan      html  css  js  c++  java
  • ECNU 3263

    一定要纪念一下第一道在比赛中自己做出来的贪心。

    题目链接:http://acm.ecnu.edu.cn/problem/3263/

    Time limit per test: 1.0 seconds

    Time limit all tests: 1.0 seconds

    Memory limit: 256 megabytes

    丽娃河是华师大著名的风景线。但由于学校财政紧缺,丽娃河边的路灯年久失修,一到晚上就会出现走在河边要打着手电的情况,不仅非常不方便,而且影响安全:已经发生了大大小小的事故多起。

    方便起见,丽娃河可以看成是从 1 到 n 的一条数轴。为了美观,路灯只能安装在整数点上,每个整数点只能安装一盏路灯。经专业勘测,有 m 个区间特别容易发生事故,所以至少要安装一定数量的路灯,

    请问至少还要安装多少路灯。

    Input

    第一行一个整数 T (1T300),表示测试数据组数。

    对于每组数据:

    • 第一行三个整数 n,m,k (1n103,1m103,1kn)

    • 第二行 k 个不同的整数用空格隔开,表示这些位置一开始就有路灯。

    • 接下来 m 行表示约束条件。第 i 行三个整数 li,ri,ti 表示:第 i 个区间 [li,ri] 至少要安装 ti 盏路灯 (1lirin,1tin)

    Output

    对于每组数据,输出 Case x: y。其中 x 表示测试数据编号(从 1 开始),y 表示至少要安装的路灯数目。如果无解,y 为 1

    Examples

    input
    3
    5 1 3
    1 3 5
    2 3 2
    5 2 3
    1 3 5
    2 3 2
    3 5 3
    5 2 3
    1 3 5
    2 3 2
    4 5 1
    output
    Case 1: 1
    Case 2: 2
    Case 3: 1

    Note

    因为今天不是满月,所以狼人没有出现。

    解题思路:

    考虑这些线段,我们将他们按照右端点从小到大排序,然后遍历线段,每次装路灯前,先查查这一段已经有多少路灯;

    然后,我们把剩下还要装几个路灯,在线段里从右往左,把空的整点一个个填满,这样一条线段的路灯就装完成了。

    这样遍历完,我们就可以得到答案。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define MAX 1000+5
     5 using namespace std;
     6 int n,m,k;
     7 bool light[MAX];
     8 struct Seg{
     9     int l,r,t;
    10 }seg[MAX];
    11 bool cmp(Seg a,Seg b)
    12 {
    13     if(a.r==b.r) return a.l<b.l;
    14     else return a.r<b.r;
    15 }
    16 int main()
    17 {
    18     int t;
    19     scanf("%d",&t);
    20     for(int kase=1;kase<=t;kase++)
    21     {
    22         scanf("%d%d%d",&n,&m,&k);
    23         for(int i=1;i<=n;i++) light[i]=0;//init
    24         for(int i=1;i<=k;i++)
    25         {
    26             int tmp;
    27             scanf("%d",&tmp);
    28             light[tmp]=1;
    29         }
    30         for(int i=1;i<=m;i++) scanf("%d%d%d",&seg[i].l,&seg[i].r,&seg[i].t);
    31         sort(seg+1,seg+m+1,cmp);
    32         int ans=0;
    33         int flag=1;
    34         for(int i=1;i<=m;i++)
    35         {
    36             if( seg[i].t > seg[i].r-seg[i].l+1 )//输出-1 
    37             {
    38                 printf("Case %d: -1
    ",kase);
    39                 flag=0;
    40                 break;
    41             }
    42             for(int p=seg[i].l;p<=seg[i].r;p++){
    43                 if(light[p]) seg[i].t--;
    44                 if(seg[i].t<=0) break;
    45             }//查找当前区间内已有路灯 
    46             if(seg[i].t>0)//加装路灯 
    47             {
    48                 for(int p=seg[i].r;p>=seg[i].l;p--)
    49                 {
    50                     if(!light[p])
    51                     {
    52                         light[p]=1;
    53                         ans++;
    54                         seg[i].t--;
    55                     }
    56                     if(seg[i].t<=0) break;
    57                 }
    58             }
    59         }
    60         if(flag) printf("Case %d: %d
    ",kase,ans);
    61     }
    62 }
  • 相关阅读:
    android.animation(6)
    android.animation(5)
    android.animation(4)
    android.animation(3)
    android.animation(2)
    android.animation(1)
    android.view.animation(2)
    php热身2:CRUD with Ajax
    PHP热身
    Android热身:通过网络获取资源并更新UI组件
  • 原文地址:https://www.cnblogs.com/dilthey/p/6847597.html
Copyright © 2011-2022 走看看