首页 > 其他分享 >Jumping Through Segments

Jumping Through Segments

时间:2024-04-29 19:55:37浏览次数:26  
标签:right Segments mid xdlst Jumping Through include ll left

题目:

链接:
https://www.luogu.com.cn/problem/CF1907D
大致思路:
二分模拟
关键点:
①确定二分区间:最小值为第一次跳的左边界,最大值为连续两个线段的最远值(注意,应该有四种情况:左1减右1,左2减右1,左1减右2,左2减右2,取绝对值);
②确定如何模拟:递推:假定跳跃长度≤k(mid),那么下一个最远就是ra+mid,最近就是la-mid,然后看有没有交集:如果没有就返回false,改变左值为mid+1;如果有就更新la和ra为能跳到的边界(就是交集)。
其中:初值:la= xdlst[0].left,ra = min(xdlst[0].right, mid)就是说第一步一定能有左边界,然后右边界和mid的最小值。
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;

using namespace std;
const int N = 2e5 + 10;
int n;
struct xd
{
	ll left, right;
}xdlst[N];
bool jd(ll mid)
{
	ll la = xdlst[0].left, ra = min(xdlst[0].right, mid);
	int id = 1;
	while (id < n)
	{
		ll lnext = max(la - mid, (ll)0), rnext = ra + mid;
		if (lnext > xdlst[id].right or rnext < xdlst[id].left)return false;
		else
		{
			la = max(xdlst[id].left, lnext), ra = min(xdlst[id].right, rnext);
		}
		id++;
	}
	return true;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		cin >> n;
		ll maxn = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> xdlst[i].left >> xdlst[i].right;
			if (i)maxn = max(abs(xdlst[i].left - xdlst[i - 1].right), max(abs(xdlst[i].right - xdlst[i - 1].right),
				max(abs(xdlst[i].left - xdlst[i - 1].left),max(maxn,abs( xdlst[i].right - xdlst[i - 1].left)))));
		}

		ll minn = xdlst[0].left;
		while (minn < maxn)
		{
			ll mid = minn + (maxn - minn) / 2;
			if (jd(mid))//if mid low:cannot,return true;
				maxn = mid;
			else minn = 1 + mid;
		}
		cout << minn << endl;

	}


	return 0;
}

标签:right,Segments,mid,xdlst,Jumping,Through,include,ll,left
From: https://www.cnblogs.com/zzzsacmblog/p/18166549

相关文章

  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib
    ERROR2002(HY000):Can'tconnecttolocalMySQLserverthroughsocket'/var/lib/mysql/mysql.sock'(2)=====================================================步骤:以下可用。(1)关闭mysql:servicemysqldstop(2)查看mysql.sock的位置(base)[root@VM-0-2-ce......
  • SpringBoot+Redis启动报错Unsatisfied dependency expressed through method 'stringR
    SpringBoot+Redis启动报错Applicationrunfailedorg.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'redisTool':Unsatisfieddependencyexpressedthroughfield'stringRedisTemplate';nestedexcep......
  • As a reader --> NetDiffusion: Network Data Augmentation Through Protocol-Constra
    ......
  • 题解 ARC175C【Jumping Through Intervals】
    先不考虑构造字典序最小的方案,只考虑求出最小的\(\sum\limits_{i=1}^{N-1}|A_{i+1}-A_i|\)。设定义域为\([L_i,R_i]\)的函数\(F_i(x)\)表示考虑后缀\([i,N]\),令\(A_i=x\)时上式最小的值。初值为\(F_N(x)=0,(x\in[L_N,R_N])\)。显然有转移方程:\[F_i(x)=\min\limits_{y......
  • CF932F Escape Through Leaf【DP,李超线段树】
    有一颗\(n\)个节点的树,根节点为\(1\)。每个节点有两个权值,第\(i\)个节点的权值为\(a_i,b_i\)。你可以从一个节点跳到它的子树内任意一个节点上。从节点\(x\)跳到节点\(y\)一次的花费为\(a_x\timesb_y\)。跳跃多次走过一条路径的总费用为每次跳跃的费用之和。求每个节......
  • [ABC314Ex] Disk and Segments题解(退火实现)
    一到比较水的退火题(虽然也调了3h)题意在平面直角坐标系中,有\(n\)条线段,第\(i\)条的端点是\((a_i,b_i)\)和$(c_i,d_i)$,任意线段不共点。(这里笔者为了方便会默认\(a_i<c_i\))你要在平面上画一个圆,使得任意一条线段都和圆周或圆内部有至少一个公共点,求满足条件的圆的最小......
  • A trip through the Graphics Pipeline 2011: Index
    原文地址https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/Welcome.ThisistheindexpageforaseriesofblogpostsI’mcurrentlywritingabouttheD3D/OpenGLgraphicspipelinesasactuallyimplementedbyGPUs.Alot......
  • CF1196B Odd Sum Segments题解
    【问题分析】本题考了奇数。由此想到以下定律:奇数+偶数=奇数;奇数+奇数=偶数;偶数+偶数=偶数;所以偶数可以忽略不计,只有奇数可以对结果产生影响,所以我们只要注意奇数即可。经过思考可得奇数的个数至少为$k$个且比$k$多的个数为偶数,此时多出的奇数可组成偶数,对结果不产生......
  • CF1677E Tokitsukaze and Beautiful Subsegments
    (题目传送门)你就算再怎么套路我也做不出来……看到\(\maxa_k\),根据套路想到用单调栈处理出\(a_i\)左边第一个比它大的位置\(pre_i\),右边第一个比它大的位置\(nxt_i\)。枚举最大值\(a_i\)考虑它的贡献,显然若存在\(j,k\)满足\(nxt_i<j,k<pre_i\)且\(a_j\timesa_k=a......
  • Eloquent 模型使用详解 Has One Through 远程一对一
    远程一对一也好,经过型,穿过型一对一也好,都能表示这种模型的关联方式:一种非直接的关系定义这里使用官方的例子:......