HDU 5595 GTW likes math(暴力,细节问题要注意)——BestCoder Round #66(div.2)

76 篇文章 0 订阅
35 篇文章 0 订阅

GTW likes math

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


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
f1=3,f2=7,f3=13,max=13,min=3
 

Source
 

/************************************************************************/

附上该题对应的中文题

GTW likes math

 
 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
问题描述
某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学《从自主招生到竞赛》。然而书里的题目太多了,GTW还有很多事情要忙(比如把妹),于是他把那些题目交给了你。每一道题目会给你一个函数f(x)=ax^2+bx+cf(x)=ax2+bx+c,求这个函数在整数区间[l,r][l,r]之间的最值。
输入描述
第一行一个整数T,表示数据组数。(T\leq 1000T1000)
对于每一组数据,有一行,共五个整数a,b,c,l,r。(|a|\leq 100,|b|\leq 100,|c|\leq 100,|l|\leq 100,|r|\leq 100,l\leq ra100,b100,c100,l100,r100,lr)
输出描述
对于每一组数据,共一行两个整数max,min,表示函数在整数区间[l,r][l,r]中的最大值和最小值。
输入样例
1
1 1 1 1 2
输出样例
7 3
Hint
f_1=3,f_2=7f1=3,f2=7,最大值=7,最小值=3
/****************************************************/

出题人的解题思路:

GTW likes math

由于是整数区间,直接枚举即可。时间复杂度O(T*(r-l))O(T(rl))

遍历l到r,更新Max和Min即可,需要注意的一点是Max和Min的初值,可以把l代入,将求得的值作为MinMax的初值,也可以赋正无穷与负无穷,当然所谓的无穷只是题目数据达不到的一个数
在此,我也要对被我hack的人表示抱歉,初值赋成999999,虽然也比较大,但是当a,b,c,l,r的值均为100的时候就超过该初值了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 25;
const int M = 710;
const int inf = 1000000000;
const int mod = 1000000007;
int main()
{
    int a,b,c,l,r,t,i,Max,Min,y;
    scanf("%d",&t);
    while(t--)
    {
        Max=-inf;
        Min=inf;
        scanf("%d%d%d%d%d",&a,&b,&c,&l,&r);
        for(i=l;i<=r;i++)
        {
            y=a*i*i+b*i+c;
            Max=max(Max,y);
            Min=min(Min,y);
        }
        printf("%d %d\n",Max,Min);
    }
    return 0;
}
菜鸟成长记
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值