8.10贝壳找房

最长递归子序列(要用二分法那种)

从头到尾遍历一遍就行了

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static int way(double A[]){
        int count=0;
        Arrays.sort(A);
        for (int i = 0; i <A.length-1 ; i++) {
            for (int j = i+1; j <A.length ; j++) {
                if (A[i]>=A[j]*0.9)
                    count++;
                else
                    break;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=Integer.parseInt(sc.nextLine());
        String s=sc.nextLine();
        String ss[]=s.split(" ");
        double A[]=new double[n];
        for (int i = 0; i <n ; i++) {
            A[i]=Integer.parseInt(ss[i]);
        }
        System.out.println(way(A));
    }
}

计算累加的差值的最小值(必须是先递增再递减的顺序,允许一个方向的递增递减)

5

1 1 1 1 1 -> 1 2 3 2 1 最小值是4

public int get(int[] a){
    int i=0,j=n-1;
    int sum=0;
    while(i<j){
        if(a[i]<=a[j]){
            i++;
            if(a[i]<=a[i-1]){
                sum+=a[i-1]+1-a[i];
                a[i]=a[i-1]+1;
            }
        }
        else{
           j--;
           if(a[j]<=a[j+1]){
            sum+=a[j+1]+1-a[j];
            a[j]=a[j+1]+1;
           }
        }
    }
    return sum;
}

 

posted @ 2019-08-10 21:58  LeeJuly  阅读(134)  评论(0)    收藏  举报