Codeforces - 1463C. Busy Robot (思维)

Busy Robot

题意

机器人初始在一数轴上的原点 某时刻接受命令 并朝命令方向前进 每秒前进1距离 在未到达当前命令的终点时 忽略当前时刻的其他命令 若机器人在 [ t i , t i + 1 ] [t_i,t_{i+1}] [ti,ti+1] 时间内移动到 x i x_i xi 位置 视为完成一次指令 (被忽略的命令可能会成功执行)

给出一系列的指令问机器人能完成多少次

思路

记录每次指令的起点 s t st st 和终点 e d ed ed 下一次停止的时间 n e t net net 上一次接收到可执行命令的时刻 l a s las las

遍历每个指令 如果当前指令的时刻大于等于 n e t net net 说明可以执行当前指令 更新 n e t 、 s t 、 e d 、 l a s net、st、ed、las netstedlas

否则判断机器人是否能在 [ t i , t i + 1 ] [t_i,t_{i+1}] [ti,ti+1] 时间内移动到 x i x_i xi 位置 具体判断依据为 在 t i t _i ti 时刻 到 t i + 1 t_{i+1} ti+1 时刻 机器人是否经过 x i x_i xi

代码

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long LL;
typedef pair<int, int>PII;
const int N = 100100;

int n;
LL tim[N];
LL pos[N];

void solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> tim[i] >> pos[i];
	}
	tim[n] = 1e10;

	LL net = 0; //下一次停止的时间
	LL st = 0, ed = 0; //一次指令的起点和终点
	LL las = 0; //上一次接收到命令的时间
	LL res = 0;

	for (int i = 0; i < n; ++i) {
		if (tim[i] >= net) {
			net = tim[i] + abs(pos[i] - ed);
			st = ed, ed = pos[i];
			las = i;
			if (net <= tim[i + 1])
				res++;
		}
		else {
			if (st >= ed) {
				int y = st - (tim[i] - tim[las]);
				int x = max(ed, st - (tim[i + 1] - tim[las]));
				if (pos[i] >= x && pos[i] <= y)res++;
			}
			else {
				int x = st + (tim[i] - tim[las]);
				int y = min(ed, st + tim[i + 1] - tim[las]);
				if (pos[i] >= x && pos[i] <= y)res++;
			}
		}
	}
	cout << res << endl;
}
int main() {
	int t; cin >> t;
	while (t--)
		solve();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzqwtc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值