ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[HDU6343]Graph Theory Homework

2021-06-25 14:33:40  阅读:156  来源: 互联网

标签:lfloor ch HDU6343 Theory int Graph sqrt rfloor leqslant


Description
There is a complete graph containing \(n\) vertices, the weight of the \(i\)-th vertex is \(w_i\).
The length of edge between vertex \(i\) and \(j\) \((i≠j)\) is \(\lfloor \sqrt{|w_i-w_j|}\rfloor\).
Calculate the length of the shortest path from 1 to \(n\).

Input
The first line of the input contains an integer \(T (1\leqslant T\leqslant 10)\) denoting the number of test cases.
Each test case starts with an integer \(n (1\leqslant n\leqslant 10^5)\) denoting the number of vertices in the graph.
The second line contains n integers, the \(i\)-th integer denotes \(w_i (1\leqslant w_i\leqslant 10^5)\).

Output
For each test case, print an integer denoting the length of the shortest path from 1 to \(n\).

Sample Input

1
3
1 3 5

Sample Output

2

根据\(\lfloor\sqrt{a}\rfloor+\lfloor\sqrt{b}\rfloor\geqslant\lfloor\sqrt{a+b}\rfloor,(a,b\in\Z)\)可得,对于任意两点\(i,j\)而言,必然有\(\lfloor\sqrt{|w_i-w_k|}\rfloor+\lfloor\sqrt{|w_k-w_j|}\rfloor\geqslant\lfloor\sqrt{|w_i-w_k|+|w_k-w_j|}\rfloor\geqslant\lfloor\sqrt{w_i-w_j}\rfloor\),故我们直接计算\(1\sim n\)即可

现对\(\lfloor\sqrt{a}\rfloor+\lfloor\sqrt{b}\rfloor\geqslant\lfloor\sqrt{a+b}\rfloor\)进行证明

令\(m^2\leqslant a\leqslant (m+1)^2\)和\(n^2\leqslant b\leqslant (n+1)^2\),则有\(\lfloor\sqrt{a}\rfloor=m,\lfloor\sqrt{b}\rfloor=n\)

若\(m=0\)或\(n=0\),则\(a=0\)或\(b=0\),此时命题显然成立

欲证 \(m+n\geqslant\lfloor\sqrt{a+b}\rfloor\)

则只需证 \(m+n+1\geqslant \sqrt{a+b}\)

则只需证 \(m+n+1\geqslant\sqrt{(m+1)^2+(n+1)^2}\)

则只需证 \(m^2+n^2+1+2m+2n+2mn\geqslant m^2+1+2m+n^2+1+2n\)

则只需证 \(2nm>1\),这在\(n,m>0\)时是显然成立的,故得证

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
int A[N+10];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read(0);
	while (T--){
		int n=read(0);
		for (int i=1;i<=n;i++)	A[i]=read(0);
		printf("%d\n",(int)trunc(sqrt(abs(A[n]-A[1]))));
	}
	return 0;
}

标签:lfloor,ch,HDU6343,Theory,int,Graph,sqrt,rfloor,leqslant
来源: https://www.cnblogs.com/Wolfycz/p/14930740.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有