【HDU - 3714 】Error Curves (三分)

题干:

Josephina is a clever girl and addicted to Machine Learning recently. She 
pays much attention to a method called Linear Discriminant Analysis, which 
has many interesting properties. 
In order to test the algorithm's efficiency, she collects many datasets. 
What's more, each data is divided into two parts: training data and test 
data. She gets the parameters of the model on training data and test the 
model on test data. To her surprise, she finds each dataset's test error curve is just a parabolic curve. A parabolic curve corresponds to a quadratic function. In mathematics, a quadratic function is a polynomial function of the form f(x) = ax2 + bx + c. The quadratic will degrade to linear function if a = 0. 



It's very easy to calculate the minimal error if there is only one test error curve. However, there are several datasets, which means Josephina will obtain many parabolic curves. Josephina wants to get the tuned parameters that make the best performance on all datasets. So she should take all error curves into account, i.e., she has to deal with many quadric functions and make a new error definition to represent the total error. Now, she focuses on the following new function's minimum which related to multiple quadric functions. The new function F(x) is defined as follows: F(x) = max(Si(x)), i = 1...n. The domain of x is [0, 1000]. Si(x) is a quadric function. Josephina wonders the minimum of F(x). Unfortunately, it's too hard for her to solve this problem. As a super programmer, can you help her?

Input

The input contains multiple test cases. The first line is the number of cases T (T < 100). Each case begins with a number n (n ≤ 10000). Following n lines, each line contains three integers a (0 ≤ a ≤ 100), b (|b| ≤ 5000), c (|c| ≤ 5000), which mean the corresponding coefficients of a quadratic function.

Output

For each test case, output the answer in a line. Round to 4 digits after the decimal point.

Sample Input

2
1
2 0 0
2
2 0 0
2 -4 2

Sample Output

0.0000
0.5000

解题报告:

    由于题中给出的a>=0, 所以a有可能为零,(但是这个题好像并没有在这里设坑)此时曲线为直线,否则曲线为开口向上的抛物线,故为下凸函数,所以F(x)也为下凸函数。故可用三分法求F(x)的极值。先算出F(x)的具体值,然后就可直接三分了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;const int MAX = 10000 + 10;
int n, a[MAX], b[MAX], c[MAX];double cal(double x) {                //求F(x)double ans = a[0]*x*x + b[0]*x + c[0];for(int i=1; i<n; i++) {ans = max(ans, a[i]*x*x+b[i]*x+c[i]);}return ans;
}
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d", &n);for(int i=0; i<n; i++) {scanf("%d%d%d", &a[i], &b[i], &c[i]);}double l = 0, r = 1000;           //三分求极值for(int i=0; i<100; i++) {double mid = l + (r-l)/3;double midd = r - (r-l)/3;if(cal(mid) < cal(midd))  r = midd;else  l = mid;}printf("%.4f\n",cal(l));}return 0;
}

总结:

   这里还有一个黑科技!不用定义eps来卡精度!直接卡时间!还有二分次数可以多一点,二分区间为10^6,才二分二十多次,可以二分100次。for循环!不用while(l<r)!! 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441950.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java中JLabel添加监听事件_[求助]关于JLabel添加监听器的问题。请各位帮忙!!

[求助]关于JLabel添加监听器的问题。请各位帮忙&#xff01;&#xff01;如图&#xff0c;我想在左边的JLabel上添加事件监听器&#xff0c;然后再去右边的JPane上进行绘制图形&#xff0c;请问这个事件监听器改怎么加&#xff0c;好象不能加ActionListener&#xff0c;要加什么…

指数循环节证明

还有关键的一步忘写了phi(m)>r的注意因为ma^r*m‘’所以phi(m)>phi(a^r)>r,所以就相当于phi(m)为循环节&#xff0c;不过如果指数小于phi(m)只能直接算了。。 注意这里的m与a^r是互质的上面忘写了。。 转自https://blog.csdn.net/guoshiyuan484/article/details/787…

java语言中的类可以_java 语言中的类

类一、类类是具有相同性质的一类事物的总称, 它是一个抽象的概念。它封装了一类对象的状态和方法, 是创建对象的模板。类的实现包括两部分: 类声明和类体类的声明类声明的基本格式为:[ 访问权限修饰符]c l a s s类名[extends超类][ implments实现的接口列表]{}说 明:① 访问权限…

【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

题干&#xff1a; Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two…

Java迭代器修改链表_Java恼人的迭代器不会返回链表中的元素

给出以下代码&#xff1a;public void insertIntoQueue(float length,int xElement,int yElement,int whichElement){Dot dot new Dot(xElement,yElement);GeometricElement element null;// some codeint robotX,robotY;boolean flag false;for (Iterator i robotList.ite…

(精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj…

java输入正确的信息_判断用户输入的信息是否正确

package com.Embed.util;import java.sql.Connection;import java.sql.DriverManager;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Common {// 判断用户输入的时间格式是否正确publ…

【UVA - 10815】 Andy's First Dictionary(STL+字符处理)

题干&#xff1a; Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. F…

java里dir是什么意思_关于文件系统:为什么user.dir系统属性在Java中工作?

我读过的几乎每篇文章都告诉我&#xff0c;你不能用Java创建chdir。 这个问题的公认答案说你不能用Java做到这一点。但是&#xff0c;这里有一些我尝试过的东西&#xff1a;geocodebox:~$ java -versionjava version"1.6.0_14"Java(TM) SE Runtime Environment (buil…

【Uva - 10935】 Throwing cards away I (既然是I,看来还有Ⅱ、Ⅲ、Ⅳ?)(站队问题队列问题)

题干&#xff1a; Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that …

腾讯 tars java_腾讯TARS开源团队郑苏波:腾讯微服务开发框架的源码剖析

郑苏波&#xff1a;大家下午好&#xff01;我是腾讯微服务的郑苏波。先做一个框架的介绍&#xff0c;Tars是一个支持多语言内嵌服务治理功能的框槛&#xff0c;能跟DevOps比较好的协同开发。这个框架四大特点&#xff1a;一是对用户透明实现&#xff0c;让业务可以聚焦自己的逻…

【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

题干&#xff1a; An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar …

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

题干&#xff1a; Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specia…

使用java开发应用程序_使用Java中的插件支持开发应用程序

我一直在研究如何开发可以加载插件的应用程序.到目前为止,我已经看到这可以通过定义一个接口来实现,并让插件实现它.但是,我当前的问题是如何在Jars中打包时加载插件.有没有“最好的”方法呢&#xff1f;我正在考虑的当前逻辑是让每个插件和他们的Jar内部寻找实现接口的类.但我…

【POJ - 1995】Raising Modulo Numbers(裸的快速幂)

题干&#xff1a; People are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that t…

软件设计师下午题java_2018上半年软件设计师下午真题(三)

● 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图6-1所示为其类图。【Java代码】import java.util.*&#xff1b;class Product {priv…

【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)

题干&#xff1a; Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhil…

java细粒度锁_Java细粒度锁实现的3种方式

最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性&#xff0c;并且要求加锁后性能不能受到太大的影响。初步的想法是通过数据的时间戳&#xff0c;id等关键字来加锁&#xff0c;从而保证不同类型数据处理的并发性。而java自身api提供的锁粒度太大&#xff0c…

【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

题干&#xff1a; 年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了&#xff0c;于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币&#xff0c;便请求酋长降低要求。酋长说&#xff1a;"嗯&#xff0c;如果…

php e notice,PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

举例说明&#xff1a;在Windows环境下&#xff1a;原本在php4.3.0中运行正常的程序&#xff0c;在4.3.1中为何多处报错&#xff0c;大体提示为&#xff1a;Notice:Undefined varialbe:变量名称. 例如有如下的代码&#xff1a; 复制代码 代码如下:if (!$tmp_i) { $tmp_i10; }在4…