HDOJ 5595 GTW likes math(简单数学,模拟)



GTW likes math

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 125    Accepted Submission(s): 76


Problem Description
After attending the class given by Jin Longyu, who is a specially-graded teacher of Mathematics, GTW started to solve problems in a book titled “From Independent Recruitment to Olympiad”. Nevertheless, there are too many problems in the book yet GTW had a sheer number of things to do, such as dawdling away his time with his young girl. Thus, he asked you to solve these problems.

In each problem, you will be given a function whose form is like
f(x)=ax2+bx+c . Your assignment is to find the maximum value and the minimum value in the integer domain [l,r] .
 

Input
The first line of the input file is an integer T , indicating the number of test cases. ( T1000 )

In the following
T lines, each line indicates a test case, containing 5 integers, a,b,c,l,r . ( |a|,|b|,|c|100,|l||r|100 ), whose meanings are given above.
 

Output
In each line of the output file, there should be exactly two integers, max and min , indicating the maximum value and the minimum value of the given function in the integer domain [l,r] , respectively, of the test case respectively.
 

Sample Input
   
   
   
   
1 1 1 1 1 3
 

Sample Output
   
   
   
   
13 3
Hint
$f_1=3,f_2=7,f_3=13,max=13,min=3$
 

题意:有方程a*x*x+b*x+c=y,现在给出a,b,c 。  求x在区间[L,R]中取整数时,y的最大值与最小值。

很简单,就是模拟下求一元二次函数的区间极值问题。不过注意x只能去这个区间中的整数。当最高点或最低点在区间内时,注意左右两边整数谁能取到最大值。

代码如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int t,a,b,c,left,right,min,max,cnt1,num,cnt2;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d%d",&a,&b,&c,&left,&right);
		if(a==0)//当a为0时,为一元一次函数 
		{
			min=left*b+c;
			max=right*b+c;
			if(min>max)
			{
				int n=min;
				min=max;
				max=n;
			}
		}
		else
		{
			double m=(-b*1.0)/(2*a);
			if(m<=left||m>=right)//极点在区间外 
			{
				min=left*left*a+left*b+c;
				max=right*right*a+right*b+c;
				if(min>max)
				{
					int n=min;
					min=max;
					max=n;
				}
			}
			else if(m>left&&m<right)//极点在区间内 
			{
				if(a>0)//开口向上,存在稳定的最小值 
				{
					if(m>0)
					{
						cnt1=m/1;
						cnt2=cnt1+1;
						min=cnt1*cnt1*a+cnt1*b+c;
						num=cnt2*cnt2*a+cnt2*b+c;
						if(num<min)
							min=num;
					}
					else
					{
						cnt1=m/1;
						cnt2=cnt1-1;//注意当极点横坐标为负值时需要往左判一个数 
						min=cnt1*cnt1*a+cnt1*b+c;
						num=cnt2*cnt2*a+cnt2*b+c;
						if(num<min)
							min=num;
					}
					if(abs(m-left)>abs(m-right))
						max=left*left*a+left*b+c;
					else
						max=right*right*a+right*b+c;
				}
				else//开口向下,有稳定最大值 
				{
					if(m>0)
					{
						cnt1=m/1;
						cnt2=cnt1+1;
						max=cnt1*cnt1*a+cnt1*b+c;
						num=cnt2*cnt2*a+cnt2*b+c;
						if(num>max)
							max=num;
					}
					else
					{
						cnt1=m/1;
						cnt2=cnt1-1;
						max=cnt1*cnt1*a+cnt1*b+c;
						num=cnt2*cnt2*a+cnt2*b+c;
						if(max<num)
							max=num;
					}
					if(abs(m-left)>abs(m-right))
						min=left*left*a+left*b+c;
					else
						min=right*right*a+right*b+c;
				}
			}
		}
		printf("%d %d\n",max,min);
	}
	return 0;
} 

你可能感兴趣的:(HDOJ 5595 GTW likes math(简单数学,模拟))