CodeForces - 1307B Cow and Friend(数学+思维)

题目链接:点击查看

题目大意:在二维平面上,要求从点 ( 0 , 0 ) 到点 ( x , 0 ) 处,每次只能行走给出的路径长度,问最少需要走几次才能到

题目分析:读完题后首先想到的是三角形,如果想组成三角形,那么必须满足两边之和大于第三边,当然在这个题目中稍微特殊一点,两边之和也可以等于第三边,这也就说,如果给出的长度的最大值大于 x 时,答案一定是 2 ,因为利用两条最大值的长度和 x 的长度,一定能组成一个锐角的等腰三角形,当给出长度的最大值小于 x 时,最优的答案肯定是先用这个最大值沿着 x 轴一直铺过去,尽可能的将其变为上面的第一种情况就好了,那么此时的答案就是 (x / mmax) 向上取整

到这就完了嘛?如果只是实现上述的操作,看似天衣无缝,但这个题目中还隐藏着一个小坑点,就是如果给出的数据中,存在一条边与 x 相等时,那么答案是 1 。因为这个特判整整卡了我一个小时。。太自闭了

代码:
 

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
     
typedef long long LL;
    
typedef unsigned long long ull;
     
const int inf=0x3f3f3f3f;

const int N=1e5+100;

int a[N];
 
int main()
{
//#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
//#endif
//  ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		int n,m;
		bool flag=false;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",a+i);
			if(a[i]==m)
				flag=true;
		}
		if(flag)
		{
			puts("1");
			continue;
		}
		int mmax=*max_element(a+1,a+1+n);
		if(mmax<=m)
			printf("%d\n",m/mmax+bool(m%mmax));
		else
			puts("2");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
    return 0;
}
发布了646 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104375282
今日推荐