丽娃河的狼人传说
Time limit per test: 1.0 seconds
Time limit all tests: 1.0 seconds
Memory limit: 256 megabytes
丽娃河是华师大著名的风景线。但由于学校财政紧缺,丽娃河边的路灯年久失修,一到晚上就会出现走在河边要打着手电的情况,不仅非常不方便,而且影响安全:已经发生了大大小小的事故多起。
方便起见,丽娃河可以看成是从 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
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
Case 1: 1 Case 2: 2 Case 3: 1
Note
因为今天不是满月,所以狼人没有出现。
Source
2017 华东师范大学网赛主要思想是,把区间按照右端点排序,然后从右开始安装路灯,因为是按照右区间端点排序的,所以当前安装的路灯一定会对后续区间产生增益作用;
还是太弱,思维跟不上
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1007;
const int inf = 0x3f3f3f3f;
const int mod = 10007;
int a[N];
struct node
{
int l, r, k;
}p[2000];
int cmp(node x,node y)
{
return x.r<y.r;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
memset(a,0,sizeof(a));
for(int i=0;i<k;i++)
{
int x;
scanf("%d", &x);
a[x]=1;
}
int flag=0, cnt=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d", &p[i].l, &p[i].r, &p[i].k);
if(p[i].k>(p[i].r-p[i].l+1)) flag=-1;
}
printf("Case %d: ",ncase++);
if(flag==-1)
{
printf("-1\n");
continue;
}
sort(p,p+m,cmp);
for(int i=0;i<m;i++)
{
int l, r, t1;
l=p[i].l, r=p[i].r, t1=p[i].k;
int sum=0;
for(int j=r;j>=l;j--) sum+=a[j];
if(t1>sum)
{
cnt+=(t1-sum);
int num=0;
for(int j=r;j>=l;j--)
{
if(num==(t1-sum)) break;
if(a[j]==0)
{
a[j]=1;
num++;
}
}
}
}
printf("%d\n",cnt);
}
return 0;
}