HDU5595(水题+数学)

Posted 普通网友

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5595(水题+数学)相关的知识,希望对你有一定的参考价值。

***********************************************声明******************************************************

      原创作品,出自 “晓风残月xj” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。

      由于各种原因,可能存在诸多不足,欢迎斧正!

*********************************************************************************************************

GTW likes math

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


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$
   
    
  
 
Source BestCoder Round #66 (div.2)

题意:

     求给定区间[l,r]的整数范围内方程  f(x)=a*x*x+b*x+c的最大值和最小值,本题要求1s内解决,直接暴力枚举时间复杂度为O(l*(r-l),而整数i的范围为|i|<=100,一般不会TE,提交一下成功了,才发现是不折不扣的水题。直接贴上代码

#include<cstdio>
#define MAX 100*100*100*2

int a,b,c,l,r;

int fun(int x)
	return a*x*x+b*x+c;


int main()  
  
    int l,r,t,i,maxsum,minsum;  
    scanf("%d",&t);  
    while(t--)  
      
        minsum=MAX;  
        maxsum=-MAX;  
        scanf("%d%d%d%d%d",&a,&b,&c,&l,&r);  
        for(i=l;i<=r;i++)  
          
          int tmp=fun(i);
          if(minsum>tmp)
		   	minsum=tmp;
		  if(maxsum<tmp)
			maxsum=tmp; 
          
        printf("%d %d\\n",maxsum,minsum);  
      
    return 0;  
  


当然,本题也有时间复杂度为O(1)的算法,根据数学公式规律计算,判断二次方程(当然,a=0是不是二次方程)最值点是否在[l,r]内,然后比较最多3个点最少2个点的最大值和最小值。注意取整的处理,这是本题的一个坑:无论正负数都取最接近的整数,C/C++的浮点转整型正好是这样处理的。贴上代码:

#include<cstdio>
#define MAX 100*100*100*2

int a,b,c,l,r;

int fun(int x)
	return a*x*x+b*x+c;


int main()

int t;
	scanf("%d",&t);
	while(t--)
		scanf("%d%d%d%d%d",&a,&b,&c,&l,&r);
		int lsum=fun(l);
		int rsum=fun(r);
		
		bool flag=false;
		int msum=0;
		if(a!=0)
			int tmp;
			if(-a*b<0)
				tmp=-1.0*b/(2*a)-0.5;
			else
				tmp=-1.0*b/(2*a)+0.5;

			if((r-tmp)*(l-tmp)<=0)
			
				flag=true;
				msum=fun(tmp);
			
		
		
		int minsum=lsum,maxsum=lsum;
		if(minsum>rsum)
			minsum=rsum;
		if(maxsum<rsum)
			maxsum=rsum;
		if(flag)
			if(minsum>msum)
				minsum=msum;
			if(maxsum<msum)
				maxsum=msum;
		
		
		printf("%d %d\\n",maxsum,minsum);
	
	return 0;











f(x)=ax2+bx+c f(x)=ax2+bx+c




以上是关于HDU5595(水题+数学)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1840 Equations (简单数学 + 水题)(Java版)

HDU刷题册

HDU - 2058 The sum problem(简单数学题)

hdu 5595 GTW likes math(暴力枚举查询)

hdu-1070(水题)

hdu2629 水题