zoukankan      html  css  js  c++  java
  • PTA 7-16 拿糖果 (15分)

    Long long ago,a handsome boy whose name is HSP studied in JSU of information science and engineering. He is clever and always thinks of ways to make fun of others. Now,it is your turn.

    HSP和他的女朋友ZM来到了商店,商店有n个糖果,标号依次为1,2,3....n,对应的价值为W1,W2,W3...Wn。现在HSP先拿走一个标号为a的糖果,标号小于a的糖果就被ZM收回去了,然后HSP只能在剩下的糖果中选一个标号为b的糖果,请问Wa-Wb的最大值是多少?

    输入格式:

    多组数据输入,每一组数据第一行输入一个数字 n(2<=n<=100000),接下来n行,每行输入一个wi表示第i个糖果的价值

    (0<wi<=100000)

    输出格式:

    每组数据输出Wa-Wb的最大值

    输入样例:

    3
    3 2 1
    6
    1 1 1 1 1 1
    
     

    输出样例:

    2
    0
    
     

    JSU 2015ACM工作组

    作者: HZL
    单位: 吉首大学
    时间限制: 1000 ms
    内存限制: 64 MB
    代码长度限制: 16 KB
     
     1 import java.util.Scanner;
     2 public class Main {
     3     public static void main(String[] args)   {
     4         Scanner sc=new Scanner(System.in);
     5         int n;
     6         while(sc.hasNext()) {
     7             n=sc.nextInt();
     8             int[] a=new int[n];
     9             for(int i=0;i<n;i++) {
    10                 a[i]=sc.nextInt();
    11             }
    12             int max=-200000;
    13             for(int i=0;i<n-1;i++) {
    14                 int min=a[i+1];
    15                 for(int j=n-1;j>=i+1;j--) {
    16                     if(min>a[j])min=a[j];
    17                 }
    18                 if(max<a[i]-min)max=a[i]-min;
    19             }
    20             System.out.println(max);
    21         }
    22         
    23     }
    24 }

    测试点2(最后一个测试点)内存超限(待完善)

    看似不起波澜的日复一日 会突然在某一天 让你看到坚持的意义 ​​​​
  • 相关阅读:
    认识计算机
    Sum 类型题目总结
    3Sum Smaller 解答
    3Sum Closest 解答
    Roman to Integer && Integer to Roman 解答
    Longest Common Prefix 解答
    Shortest Word Distance 解答
    Longest Valid Parentheses 解答
    Lowest Common Ancestor of a Binary Search Tree 解答
    Longest Palindromic Substring 解答
  • 原文地址:https://www.cnblogs.com/Flyfishy/p/12163991.html
Copyright © 2011-2022 走看看