洛谷P2735 电网 Electric Fences

题目描述 https://www.luogu.org/problemnew/show/P2735
先不管p,先处理(n,m)、(n,0)和坐标原点构成的直角三角形中的合理点数,
然后再判断n和p的大小关系,在算有一个直角三角形中合理点数,用相似三角形拉解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> 
using namespace std;
int n,m,p;
int work1()
{
	int y=1,ans=0;
	for(int i=1;i<=n;i++)
	{
		double x=i*m*1.0/n;
		while(y<x&&y<m) y++;
		ans+=y-1;
	}
	return ans;
}
int work2()
{
	int y=1,ans=0;
	for(int i=p-1;i>n;i--)
	{
		double x=(p-i)*m*1.0/(p-n);
		while(y<x&&y<m) y++;
		ans+=y-1;
	}
	return ans;
}
const double eps=1e-5;
int work3()
{
	int y=1,ans=0;
	for(int i=p+1;i<=n;i++)
	{
		double x=(i-p)*m/(n-p);
		while((y<x||abs(y-x)<=eps)&&y<m) y++;
		ans+=y-1;
	}
	return ans;
}
int main() 
{ 
   scanf("%d%d%d",&n,&m,&p);
   int ans=work1();
   if(n==p) cout<<ans-m+1;
   else if(n<p) cout<<ans+work2();
   else if(n>p) cout<<ans-work3();
 	
  return 0;	
}

猜你喜欢

转载自blog.csdn.net/qq_42920122/article/details/88951067