2017华东师范大学网赛-丽娃河的狼人传说

丽娃河的狼人传说

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

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

Source

2017 华东师范大学网赛
解题思路:按照r从小到大排序,用树状数组维护前缀和


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

struct node
{
	int l, r,k;
}x[1005];
int n, m, k,sum[1005],visit[1005];

bool cmp(node a, node b)
{
	if (a.r != b.r) return a.r < b.r;
	else return a.l < b.l;
}

int lowbit(int x)
{
	return x&-x;
}

void add(int x, int val)
{
	while (x <= n)
	{
		sum[x] += val;
		x += lowbit(x);
	}
}

int getsum(int x)
{
	int ans = 0;
	while(x)
	{ 
		ans += sum[x];
		x -= lowbit(x);
	}
	return ans;
}
int main()
{
	int t,cas=0;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m, &k);
		memset(sum, 0, sizeof sum);
		memset(visit, 0, sizeof visit);
		printf("Case %d: ", ++cas);
		int a,flag=1;
		for(int i=1;i<=k;i++)
		{ 
			scanf("%d", &a);
			visit[a] = 1;
			add(a, 1);
		}
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &x[i].l, &x[i].r,&x[i].k);
			if (x[i].k > x[i].r - x[i].l + 1) flag = 0;
		}
		if (!flag) { printf("-1\n"); continue; }
		sort(x + 1, x + 1 + m, cmp);
		int ans = 0;
		for (int i = 1; i <= m; i++)
		{
			int sum1 = getsum(x[i].r) - getsum(x[i].l - 1);
			if ( sum1>= x[i].k) continue;
			int cnt = x[i].k - sum1,pos=x[i].r;
			while (cnt)
			{
				if (visit[pos]) { pos--; continue; }
				visit[pos] = 1;
				add(pos, 1);
				pos--;
				cnt--;
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值