GTW likes math
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)=ax ^ 2 + bx + c,f(x)=ax2+bx+c. Your assignment is to find the maximum value and the
minimum value in the integer domain [l, r][l,r].
The first line of the input file is an integer TT, indicating the number of test cases. (Tleq 1000T≤1000)
In the following TT lines, each line indicates a test case, containing 5 integers, a, b, c, l, ra,b,c,l,r. (|a|, |b|, |c|leq 100, |l|leq |r|leq 100∣a∣,∣b∣,∣c∣≤100,∣l∣≤∣r∣≤100), whose meanings are given above.
In each line of the output file, there should be exactly two integers, maxmax and minmin, indicating the maximum value and the minimum value of the given function in the integer domain [l, r][l,r], respectively, of the test case respectively.
1 1 1 1 1 3
13 3
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<vector> using namespace std; const int INF=0x3f3f3f3f; #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) #define P_ printf(" ") #define T_T while(T--) #define mem(x,y) memset(x,y,sizeof(x)) const int MAXN=0x3f3f3f3f; int a,b,c,l,r; int js(int x){ return a*x*x+b*x+c; } /*void sanfen(double l,double r){ double mx,mi,m,mm; double x=l,y=r; while(r-l>=1e-6){ m=(l+r)/2.0; mm=(m+r)/2.0; if(js(m)>=js(mm))r=mm; else l=m; } mx=js(l); l=x;r=y; while(r-l>=1e-6){ m=(l+r)/2.0; mm=(m+r)/2.0; if(js(m)<=js(mm))r=mm; else l=m; } mi=js(l); printf("%.0lf %.0lf ",mx,mi); }*/ int main(){ int T; SI(T); int mi,mx; T_T{ scanf("%d%d%d%d%d",&a,&b,&c,&l,&r); mi=INF;mx=-INF; for(int i=l;i<=r;i++){ mi=min(mi,js(i)); mx=max(mx,js(i)); } printf("%d %d ",mx,mi); } return 0; } /* #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<vector> using namespace std; const int INF=0x3f3f3f3f; #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) #define P_ printf(" ") #define T_T while(T--) #define mem(x,y) memset(x,y,sizeof(x)) double a,b,c,l,r; double js(double x){ return a*x*x+b*x+c; } int main(){ int T; SI(T); T_T{ scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&l,&r); double mid=-b/(2*a); double m[3]; m[0]=js(mid); m[1]=js(l);m[2]=js(r); //printf("%lf %lf %lf ",m[0],m[1],m[2]); if(l<=mid&&mid<=r)printf("%.0lf %.0lf ",*max_element(m,m+3),*min_element(m,m+3)); else printf("%.0lf %.0lf ",max(m[1],m[2]),min(m[1],m[2])); } return 0; }*/