Created
February 5, 2015 02:01
-
-
Save ababup1192/436acb3f412ae7747596 to your computer and use it in GitHub Desktop.
Getting Started - Maximum Profit http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Main{ | |
| public static void main(String[] args){ | |
| Scanner scan = new Scanner(System.in); | |
| int n = scan.nextInt(); | |
| // 初期値を仮で最小値に | |
| int minv = scan.nextInt(); | |
| // 更新できるように最大値に十分小さい値を | |
| int maxv = Integer.MIN_VALUE; | |
| for(int i=1;i<n;i++){ | |
| int in = scan.nextInt(); | |
| // 最大値の更新 | |
| maxv = Math.max(maxv, in-minv); | |
| // 最小値の更新 | |
| minv = Math.min(minv, in); | |
| } | |
| System.out.println(maxv); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.io._ | |
| object Main extends App { | |
| val n = StdIn.readInt | |
| // 入力を流し込んで(最大,最小)のタプルを書き換えて(畳み込んで)いく。 | |
| val (max,min) = Iterator.continually(readLine()).takeWhile(_ != null) | |
| .foldLeft((Int.MinValue, Int.MaxValue)){ | |
| case ((preMax,preMin),in) => | |
| val inNum = in.toInt | |
| val max = Math.max(preMax, inNum - preMin) | |
| val min = Math.min(preMin, inNum) | |
| (max, min) | |
| } | |
| // 最終結果のmaxを出力。 | |
| println(max) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment