hdu 5112 A Curious Matt (java,快速输入)
来源:互联网 发布:linux新手基础知识总结 编辑:程序博客网 时间:2024/06/04 00:55
A Curious Matt
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 2397 Accepted Submission(s): 1355
One day, Matt's best friend Ted is wandering on the non-negative half of the number line. Matt finds it interesting to know the maximal speed Ted may reach. In order to do so, Matt takes records of Ted’s position. Now Matt has a great deal of records. Please help him to find out the maximal speed Ted may reach, assuming Ted moves with a constant speed between two consecutive records.
For each test case, the first line contains an integer N (2 ≤ N ≤ 10000),indicating the number of records.
Each of the following N lines contains two integers ti and xi (0 ≤ ti, xi ≤ 106), indicating the time when this record is taken and Ted’s corresponding position. Note that records may be unsorted by time. It’s guaranteed that all ti would be distinct.
232 21 13 430 31 52 0
Case #1: 2.00Case #2: 5.00HintIn the first sample, Ted moves from 2 to 4 in 1 time unit. The speed 2/1 is maximal.In the second sample, Ted moves from 5 to 0 in 1 time unit. The speed 5/1 is maximal.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5112
解题思路:给定n个时间点的坐标和n个时间点对应的横坐标,求整段时间里的最大速度。这题解题思路很简单,就对时间按从小到大排一下顺序,然后计算相邻时间点里速度,取最大速度即可。但这题用java实现有几点要注意:
1、C++里建结构体直接用struct就行,Java是定义成一个类的形式,在主函数里申请空间时用多少申请多少,比如<类名>[] <对象名> = new<类名>[n] ;不能多申请,和C++的排序函数不同,C++传入排序长度,Java不传,多余的空间会跟着排序。输入结构体里的变量时要边申请空间边输入。
2、重写Comparator要写在主函数里面,格式为:
Comparator <类名> cmp = new Comparator<类名>(){
@Override
public int compare(类名 o1, 类名 o2){
}
排序函数 :Arrays.sort(<数组名>,cmp);
3、 确定小数点后几位,比如保留ans的小数点后两位:new DecimalFormat("0.00").format(ans)
4、io快速读入,这题不用这个方法大概是2s+,会超时,用快速读入后就在1s内了。原理是:Scanner读入是字符读入,要换成字符流读入会更快。但是io读入要重写方法,有些复杂,多理解记忆一下。 定义myScanner类:重写读入string、int 、double等方法(详见代码)。
代码如下:
import java.util.*;import java.text.*;import java.io.*;public class Main {static class Recordes{double t,x; }public static int T, n;static class myScanner { //写输入类BufferedReader br;StringTokenizer st;public myScanner(InputStream in) {br = new BufferedReader(new InputStreamReader(in));st = new StringTokenizer("");}public String nextLine() {try { //此处要捕获异常return br.readLine();} catch (IOException e){return null;}}public boolean hasNext() {while(!st.hasMoreTokens()) {String s = nextLine();if (s == null) {return false;}st = new StringTokenizer(s);}return true;}public String next() {hasNext();return st.nextToken();}public int nextInt() {return Integer.parseInt(next());}public double nextDouble() {return Double.parseDouble(next());}} public static void main(String[] args) { myScanner sc = new myScanner(System.in); PrintWriter pw = new PrintWriter(System.out); T = sc.nextInt(); for(int ca = 1;ca<=T;ca++){ n = sc.nextInt(); Recordes[] re = new Recordes[n]; for(int i=0;i<n;i++){ re[i] = new Recordes(); re[i].t = sc.nextDouble(); re[i].x = sc.nextDouble(); } Comparator <Recordes> cmp = new Comparator<Recordes>(){ @Override public int compare(Recordes o1, Recordes o2) { // TODO Auto-generated method stub if( o1.t > o2.t ){ return 1; }else if( o1.t < o2.t ){ return -1; }else{ if(o1.x > o2.x){ return 1; }else if(o1.x < o2.x){ return -1; }else{ return 0; } } } }; Arrays.sort(re,cmp); double ans = 0.0; for(int i=1;i<n;i++){ double xx = Math.abs(re[i].x-re[i-1].x) ; double tt = re[i].t - re[i-1].t; if(tt==0){ continue; } double cur = xx/tt; ans=Math.max(ans, cur); } pw.println("Case #"+ca+": "+new DecimalFormat("0.00").format(ans)); pw.flush(); } } }
- hdu 5112 A Curious Matt (java,快速输入)
- hdu 5112 A Curious Matt(排序)
- HDU 5112 A Curious Matt(水~)
- HDU 5112 A Curious Matt
- hdu 5112 A Curious Matt
- hdu 5112 A Curious Matt
- HDU 5112 A Curious Matt
- hdu 5112 A Curious Matt
- HDU 5112 A Curious Matt (Java comparator + 快速IO + 控制浮点精度)
- 【瞎搞】 HDU 5112 A Curious Matt 水
- 文章标题 HDU 5112- A Curious Matt
- HDU 5112 A Curious Matt (水题)
- A Curious Matt 5112
- hdoj 5112 A Curious Matt(水题)
- hdoj-5112-A Curious Matt
- HDU 5112 A Curious Matt (14北京 排序 水题)
- HDU 5112 2014ICPC北京站现场赛 A Curious Matt
- hdu 5112 A Curious Matt (结构体+cmp函数)
- 问题解决:Error -27776: Server "XXX" shut connection during attempt to negotiate SSL session
- 带头视图的瀑布流
- Tango 开发指南(9) — 区域学习 API (Java 描述)
- 跨域问题以及Nginx配置文件
- Android Lottie动画框架简单例子
- hdu 5112 A Curious Matt (java,快速输入)
- poj1664 放苹果
- 算法-动态规划-背包问题
- 设备驱动程序框架分析——(以点灯为例)
- PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件
- Win64 驱动签名
- 蓝桥杯 —— 打印十字图
- iOS--LaunchImage启动页设置及问题解决
- C++ 宏定义和 typedef 的区别