2017年华东师范大学网络赛 F

来源:互联网 发布:vip视频解析端口原理 编辑:程序博客网 时间:2024/04/30 05:10

F. 丽娃河的狼人传说
Time limit per test: 1.0 seconds
Time limit all tests: 1.0 seconds
Memory limit: 256 megabytes
Accept / Submit: 224 / 1759

丽娃河是华师大著名的风景线。但由于学校财政紧缺,丽娃河边的路灯年久失修,一到晚上就会出现走在河边要打着手电的情况,不仅非常不方便,而且影响安全:已经发生了大大小小的事故多起。
这里写图片描述
方便起见,丽娃河可以看成是从 1 到 n 的一条数轴。为了美观,路灯只能安装在整数点上,每个整数点只能安装一盏路灯。经专业勘测,有 m 个区间特别容易发生事故,所以至少要安装一定数量的路灯,

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

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

对于每组数据:

第一行三个整数 n,m,k (1≤n≤103,1≤m≤103,1≤k≤n)。

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

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

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
分析: 先按右区间对区间排序 , 贪心就好 , 当区间需要安装路灯时尽量按在区间右边 , 比赛时成二分+贪心了 , 虽然也能过 , 但是加个二分时间翻了好几番。
另外这道题和poj 2586 很像。

#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <stdlib.h>#define sc scanf#define pr printfusing namespace std;struct Note{    int l,r,num;}note[1100];int arr[1100];int is_ok();bool cmp(Note &a , Note &b){    //if(a.l != b.r) return a.l < b.l;  //加上这句话就错 , 不知道为啥。。    return a.r < b.r;}int n , m , k;int main(){    int t,i,j,  cae = 0;    sc("%d",&t);    while(t--)    {        memset(arr,0,sizeof(arr));        sc("%d%d%d",&n,&m,&k);        int a,b,c;        while(k--)        {            sc("%d",&a);            arr[a]++;        }        bool flag = 0;        for(i=1; i<=m; i++)        {            sc("%d%d%d",&note[i].l,&note[i].r,&note[i].num);            if(note[i].r - note[i].l + 1 < note[i].num) flag = 1;        }        if(flag)        {            pr("Case %d: -1\n",++cae); continue;        }        sort(note+1,note+1+m,cmp);        pr("Case %d: %d\n",++cae,is_ok());    }    return 0;}int is_ok(){    int i , j , sum = 0 , ans = 0;    int arr2[1100];    for(i=1; i<=n; i++) arr2[i] = arr[i];    for(i=1; i<=m; i++)    {        sum = 0;        for(j=note[i].l; j<=note[i].r; j++)            if(arr2[j]) sum++;        if(sum < note[i].num)        {            int aa = note[i].num - sum;            for(j=note[i].r; j>=note[i].l; j--)            {                if(arr2[j] == 0)                {                    aa-- , ans++;                    arr2[j] = 1;                    if(aa == 0) break;                }            }        }    }    return ans;}
0 0