zoukankan      html  css  js  c++  java
  • (贪心)华师大程序设计竞赛 F-丽娃河的狼人传说

    丽娃河的狼人传说

    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

    最初见到这个题,一筹莫展,想到线段树,但又不知道这样的情况如何更新值比较好。

    实际上,按照每个区间右侧的位置从小到大排序,这样对于每个区间,都在该区间尽量向右放即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <stack>
    12 #define mp make_pair
    13 typedef long long ll;
    14 typedef unsigned long long ull;
    15 const int MAX=1e3+5;
    16 const int INF=1e9+5;
    17 const double M=4e18;
    18 using namespace std;
    19 const int MOD=1e9+7;
    20 typedef pair<int,int> pii;
    21 const double eps=0.000000001;
    22 int n,m,k;
    23 int t;
    24 bool vi[MAX];
    25 int lo;
    26 struct seg
    27 {
    28     int l,r,num;
    29 };
    30 bool cmp(seg x,seg y)
    31 {
    32     if(x.r!=y.r)
    33         return x.r<y.r;
    34     else
    35         return x.l>y.l;
    36 }
    37 bool st;
    38 seg a[MAX];
    39 int cnt,ge;
    40 int main()
    41 {
    42     scanf("%d",&t);
    43     for(int i=1;i<=t;++i)
    44     {
    45         scanf("%d%d%d",&n,&m,&k);
    46         memset(vi,false,sizeof(vi));
    47         st=true;
    48         cnt=0;
    49         for(int j=1;j<=k;++j)
    50         {
    51             scanf("%d",&lo);
    52             vi[lo]=true;
    53         }
    54         for(int j=1;j<=m;++j)
    55         {
    56             scanf("%d%d%d",&a[j].l,&a[j].r,&a[j].num);
    57             if(a[j].num>a[j].r+1-a[j].l)
    58             {
    59                 st=false;
    60             }
    61         }
    62         if(!st)
    63         {
    64             printf("Case %d: -1
    ",i);continue;
    65         }
    66         sort(a+1,a+1+m,cmp);
    67         for(int j=1;j<=m;++j)
    68         {
    69             ge=0;
    70             for(int s=a[j].l;s<=a[j].r;++s)
    71             {
    72                 if(vi[s])
    73                     ++ge;
    74             }
    75             if(ge<a[j].num)
    76                 cnt+=a[j].num-ge;
    77             for(int s=a[j].r;s>=a[j].l&&ge<a[j].num;--s)
    78             {
    79                 if(!vi[s])
    80                 {
    81                     ++ge;vi[s]=true;
    82                 }
    83             }
    84         }
    85         printf("Case %d: %d
    ",i,cnt);
    86     }
    87     return 0;
    88 }
    View Code

    丽娃河的狼人传说

    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
  • 相关阅读:
    不常用的cmd命令
    js获取宽度
    Marshaling Data with Platform Invoke 概览
    Calling a DLL Function 之三 How to: Implement Callback Functions
    Marshaling Data with Platform Invoke 之四 Marshaling Arrays of Types
    Marshaling Data with Platform Invoke 之一 Platform Invoke Data Types
    Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)
    Calling a DLL Function 之二 Callback Functions
    WCF 引论
    Marshaling Data with Platform Invoke 之二 Marshaling Strings (用时查阅)
  • 原文地址:https://www.cnblogs.com/quintessence/p/6850877.html
Copyright © 2011-2022 走看看