Codeforces - A Game with Traps

D. A Game with Traps

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are playing a computer game, where you lead a party of m soldiers. Each soldier is characterised by his agility ai.

The level you are trying to get through can be represented as a straight line segment from point 0 (where you and your squad is initially located) to point n+1 (where the boss is located).

The level is filled with k traps. Each trap is represented by three numbers li, ri and di. li is the location of the trap, and di is the danger level of the trap: whenever a soldier with agility lower than di steps on a trap (that is, moves to the point li), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point ri, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don’t affect you, only your soldiers.

You have t seconds to complete the level — that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won’t be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:

if your location is x, you may move to x+1 or x−1. This action consumes one second;
if your location is x and the location of your squad is x, you may move to x+1 or to x−1 with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with di greater than agility of some soldier from the squad). This action consumes one second;
if your location is x and there is a trap i with ri=x, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.

You have to choose the maximum number of soldiers such that they all can be brought from the point 0 to the point n+1 (where the boss waits) in no more than t seconds.

Input
The first line contains four integers m, n, k and t (1≤m,n,k,t≤2⋅105, n<t) — the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.

The second line contains m integers a1, a2, …, am (1≤ai≤2⋅105), where ai is the agility of the i-th soldier.

Then k lines follow, containing the descriptions of traps. Each line contains three numbers li, ri and di (1≤li≤ri≤n, 1≤di≤2⋅105) — the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.

Output
Print one integer — the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than t seconds.

Example
inputCopy

5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
outputCopy
3
Note
In the first example you may take soldiers with agility 3, 4 and 5 with you. The course of action is as follows:

go to 2 without your squad;
disarm the trap 2;
go to 3 without your squad;
disartm the trap 3;
go to 0 without your squad;
go to 7 with your squad.
The whole plan can be executed in 13 seconds.


贪心+二分。

对于每一个炸弹,都分为炸弹位置,和拆弹位置。

对于当前前面有炸弹时,我们要考虑拆这个炸弹,拆完之后是继续往后拆,还是直接回去。

这个我们画图就能知道,如果两个炸弹起点和终点位置有交叉,那么就继续拆,否则直接回去。

判断被覆盖就能知道了,然后覆盖我们用前缀和,差分处理一下即可。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+10;
int m,n,k,tt,a[N],vis[N],L,R,cnt[N];
struct node{int l,r,d;}t[N];
int cmp(int a,int b){return a>b;}
inline int check(int mid){
	int low=a[mid],tot=0; memset(cnt,0,sizeof cnt);
	for(int i=1;i<=k;i++)	if(t[i].d>low){cnt[t[i].l]++,cnt[t[i].r+1]--;}
	for(int i=1;i<=n+1;i++)	cnt[i]+=cnt[i-1];
	for(int i=0;i<=n+1;i++)	tot+=(cnt[i]>=1);
	return tot*2+n+1<=tt;
}
signed main(){
	cin>>m>>n>>k>>tt; L=0,R=m;
	for(int i=1;i<=m;i++)	scanf("%d",&a[i]);
	for(int i=1;i<=k;i++)	scanf("%d %d %d",&t[i].l,&t[i].r,&t[i].d);
	sort(a+1,a+1+m,cmp);
	while(L<R){
		int mid=L+R+1>>1;
		if(check(mid))	L=mid;
		else	R=mid-1; 
	}
	cout<<L<<endl;
	return 0;
}
全部评论

相关推荐

夏日狂想曲:连体婴是这样的,不过国内还有上四休三的公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 你的mentor是什么样的人? #
4588次浏览 33人参与
# 你觉得mentor喜欢什么样的实习生 #
10776次浏览 297人参与
# 平安产险科技校招 #
2440次浏览 0人参与
# 帮我看看,领导说这话什么意思? #
6729次浏览 28人参与
# 26届秋招公司红黑榜 #
13421次浏览 44人参与
# 怎么给家人解释你的工作? #
1748次浏览 17人参与
# 智慧芽求职进展汇总 #
26137次浏览 110人参与
# 没有家庭托举的我是怎么找工作的 #
12806次浏览 161人参与
# 求职低谷期你是怎么度过的 #
5470次浏览 97人参与
# 实习必须要去大厂吗? #
146898次浏览 1542人参与
# 从哪些方向判断这个offer值不值得去? #
6825次浏览 95人参与
# 同bg的你秋招战况如何? #
158912次浏览 927人参与
# 度小满求职进展汇总 #
10248次浏览 53人参与
# 校招泡的最久的公司是哪家? #
4915次浏览 23人参与
# 面试紧张时你会有什么表现? #
1811次浏览 21人参与
# 你有哪些缓解焦虑的方法? #
37215次浏览 835人参与
# 你喜欢工作还是上学 #
77633次浏览 860人参与
# 入职第一天,你准备什么时候下班 #
85528次浏览 467人参与
# 秋招想进国企该如何准备 #
97761次浏览 487人参与
# 简历无回复,你会继续海投还是优化再投? #
103636次浏览 819人参与
# 机械人的工作环境真的很差吗 #
25100次浏览 119人参与
# 独居后,你的生活是更好了还是更差了? #
28161次浏览 263人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务