hdu 2899 凸性 二分 / 三分

Strange fuction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7230    Accepted Submission(s): 4996

Problem Description

Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)

Output

Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

Sample Input

2
100
200

Sample Output

-74.4291
-178.8534

二分先决条件是满足单调性,这里显然不满足不能直接二分,这道题目求导就可以解决,求出极值点,如果求导不方便的题目就得用三分搜索了。

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #define exp 1e-8
 5
 6 using namespace std;
 7
 8 double y;
 9
10 double suan(double x)
11 {
12     return 42*pow(x,6)+48*pow(x,5)+21*x*x+10*x-y;
13 }
14 double binary(double low,double high)
15 {
16     while(high-low>exp)
17     {
18         double mid = (low+high)/2;
19         if(suan(mid)>0)
20             high=mid-exp;
21         else
22             low=mid+exp;
23     }
24     return low;
25 }
26 int main()
27 {
28     int t;
29     scanf("%d",&t);
30     while(t--)
31     {
32         scanf("%lf",&y);
33         double ans = binary(0,100);
34         printf("%.4f\n",6*pow(ans,7)+8*pow(ans,6)+7*pow(ans,3)+5*ans*ans-y*ans);
35     }
36     return 0;
37 }

三分查找

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #define exp 1e-8
 5
 6 using namespace std;
 7
 8 double y;
 9
10 double suan(double x)
11 {
12     return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;
13 }
14 double binary(double low,double high)
15 {
16     double mid,mmid;
17     while(high-low>exp)
18     {
19         mid = (low+high)/2;
20         mmid = (high+mid)/2;
21         if(suan(mid)>suan(mmid))
22             low=mid+exp;
23         else
24             high=mmid-exp;
25     }
26     return (mid+mmid)/2;
27 }
28 int main()
29 {
30     int t;
31     scanf("%d",&t);
32     while(t--)
33     {
34         scanf("%lf",&y);
35         double ans = binary(0,100);
36         printf("%.4f\n",suan(ans));
37     }
38     return 0;
39 }
时间: 2024-07-31 14:26:14

hdu 2899 凸性 二分 / 三分的相关文章

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

HDU 2199 (二分&amp;三分 _A题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 ----------------------------------------------------------------------------------- 题意:8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,求解 思路:判断下导数,二分法求解. 代码: #include<cstdio> #include<cstring> #include&

Strange fuction HDU 2899 【二分】

Problem Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<=T<=100) which means the numb

hdu 2899 Strange fuction (二分)

题目链接:http://acm.hdu.edu.cn/showproblem.pihp?pid=2899 题目大意:找出满足F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)的x值.注意精确度的问题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 double y; 6 7 doub

Toxophily-数论以及二分三分

G - Toxophily Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2298 Description The recreation center of WHU ACM Team has indoor billiards, Ping Pang, chess and bridge, toxophily, deluxe ballroom

hdu 2899 Strange fuction——模拟退火

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899 还可三分.不过只写了模拟退火. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<ctime> #include<cmath> #include<cstdlib> #define db double using

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

HDU 3400 Line belt 三分

笨蛋的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述        笨蛋之所以称为笨蛋,是因为他有点路痴.他一旦不高兴,就必然一个人漫无目的的出去走走.今天下雨了,他又不高兴了,怎么办?那就出去走呗,这不又丢了,这次幸好记下出来时的方向,并且在一张纸上密密麻麻的记下了他拐的弯(他很聪明吧,拐的弯都是90度的弯),0代表左拐,1代表右拐,那么多0.1,他实在看不下去了,正好遇见善良加聪明的你,你能告诉他,他现在面向哪吗? 输入 多组测试数据 第一行 输入:

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=