Searchlights (CodeForces - 1408D)

Searchlights

题目

There are nn robbers at coordinates (a1,b1),(a2,b2), …,(an,bn) and mm searchlight at coordinates (c1,d1), (c2,d2), …, (cm,dm).

In one move you can move each robber to the right (increase ai of each robber by one) or move each robber up (increase bi of each robber by one). Note that you should either increase all ai or all bi, you can’t increase ai for some points and bi for some other points.

Searchlight j can see a robber ii if ai≤cj and bi≤dj.

A configuration of robbers is safe if no searchlight can see a robber (i.e. if there is no pair i,j such that searchlight j can see a robber i).

What is the minimum number of moves you need to perform to reach a safe configuration?

题意

给出n个强盗坐标 ( a i , b i ),m个探照灯坐标 ( c j , d j ),每一次可以将所有 a i + 1 或者 b i + 1 ,对于 1 ≤ i ≤ n , 1 ≤ j ≤ m,均满足 a i ≤ c j & & b i ≤ d j,问最少需要操作几次

思路

(听说有O(n)的解法 但是我不会orz)
对于任意一组强盗x和探照灯y,如果x能被看到,那么可以记录下来该强盗向右走多少步能走出探照灯和向上走多少步能走出探照灯,记录完毕后将其按照向右走的步数从小到大排序。如果向右走i步可以出去,那么该项前面的海盗也都走出去了,该项后面的海盗则只能从上面走出去,另开一个mx数组记录从上面走出去的步数 然后on遍历求最值

Code

#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
#define MOD 1000000007
#define PI 3.1415926535898
#define INF 1000055
#define MAXN 2050
const double EPS = 1e-8;
long long read()
{
	LL x = 0, w = 1;
	char ch = 0;
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
		{
			w = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return w * x;
}
int n, m,cnt,ans=0x3f3f3f3f, mx[4000005];
struct node
{
	int x, y;
}a[4000005];
node xx[MAXN], yy[MAXN];
bool cmp(node xxx, node yyy)
{
	return xxx.x < yyy.x;
}
int main()
{
	n = read();
	m = read();
	for (register int i = 1; i <= n; i++)
	{
		xx[i].x = read();
		xx[i].y = read();
	}
	for (register int i = 1; i <= m; i++)
	{
		yy[i].x = read();
		yy[i].y = read();
		for (register int j = 1; j <= n; j++)
		{
			if (xx[j].x <= yy[i].x && xx[j].y <= yy[i].y)
			{
				a[++cnt].x = abs(xx[j].x - yy[i].x)+1;
				a[cnt].y = abs(xx[j].y - yy[i].y)+1;
			}
		}
	}
	sort(a + 1, a + cnt + 1, cmp);
	for (register int i = cnt; i >= 0; i--)
	{
		if (i == cnt)
		{
			mx[i] = a[i].y;
		}
		else
		{
			mx[i] = max(mx[i + 1], a[i].y);
		}
	}
//	mx[0] = mx[1];
	for (register int i = 0; i <= cnt; i++)
	{
		ans = min(a[i].x + mx[i+1], ans);//注意这里是mx+1
	}
	cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值