Acwing - 基础算法模板

快排, patition返回值的那个数是排序好的

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i =0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        qs(arr, 0, arr.length - 1);
        for(int i =0; i < n; i++){
            System.out.print(arr[i] + " ");
        }
    }
    
    public static void qs(int[] arr, int l, int r){
        if(l >= r)  return;
        int i = l, j = r + 1, p = arr[l];
        while(i < j){
            while(i < r && arr[++i] < p);
            while(j > l && arr[--j] > p);
            if(i < j) swap(arr, i, j);
        }
        swap(arr, l, j);
        qs(arr, l, j - 1);
        qs(arr, j + 1, r);
    }
    
    public static void swap(int[] arr, int l, int r){
        int tmp = arr[l];
        arr[l] = arr[r];
        arr[r] = tmp;
    }
    
}

第k个数

import java.util.*;
class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);   
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] q = new int[n];
        for(int i =0;i < n; i++){
            q[i] = sc.nextInt();
        }
        
        System.out.println(getK(q, k - 1));
    }
    
    public static int getK(int[] q, int k){
        int l = 0, r = q.length - 1;
        while(l < r){
            int p = patition(q, l, r);
            if(p == k)  return q[k];
            if(p < k){
                l = p + 1;
            }else{
                r = p - 1;
            }
        }
        return q[k];
    }
    
    public static int patition(int[] q, int l, int r){
        int i = l, j = r + 1, p = q[l];
        while(i < j){
            while(i < r && q[++i] < p);
            while(j > l && q[--j] > p);
            if(i < j) swap(q, i, j);
        }
        swap(q, j, l);
        return j;
    }
    
    public static void swap(int[] q, int i, int j){
        int tmp = q[i];
        q[i] = q[j];
        q[j] = tmp;
    }
    
}

归并排序

import java.util.*;
class Main{
    static int[] cp;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        cp = new int[n];
        int[] q = new int[n];
        for(int i = 0; i < n; i++){
            q[i] = sc.nextInt();
        }
        mS(q, 0, n - 1);
        for(int i =0; i < n; i++){
            System.out.print(q[i] + " ");
        }
    }
    
    public static void mS(int[] q, int l, int r){
        if(l >= r)  return;
        int mid = l + r >> 1;
        mS(q, l, mid);
        mS(q, mid + 1, r);
        merge(q, l, mid, r);
    }
    public static void merge(int[] q, int l, int mid, int r){
        int i = l, j = mid + 1;
        for(int k = l; k <= r; k++)
            cp[k] = q[k];
        for(int k = l; k <= r; k++){
            if(i > mid)             q[k] = cp[j++];
            else if(j > r)          q[k] = cp[i++];
            else if(cp[i] <= cp[j]) q[k] = cp[i++];
            else                    q[k] = cp[j++];
        }
    }
    
}

归并排序求逆序对

import java.util.*;

class Main{
    static int[] tmp;
    static double ans = 0;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] q = new int[n];
        tmp = new int[n];
        for(int i =0; i < n; i++){
            q[i] = sc.nextInt();
        }
        ms(q, 0, n - 1);
        System.out.printf("%.0f", ans);
        
    }
    
    public static void ms(int[] q, int l, int r){
        if(l >= r)  return;
        int mid = l + r >> 1;
        ms(q, l, mid);
        ms(q, mid + 1, r);
        merge(q, l, mid, r);
    }
    
    public static void merge(int[] q, int l, int mid, int r){
        int i = l, j = mid + 1;
        for(int k = l; k <= r; k++)
            tmp[k] = q[k];
        for(int k = l; k <= r; k++){
            if(i > mid)  q[k] = tmp[j++];
            else if(j > r)   q[k] = tmp[i++];
            else if(tmp[i] <= tmp[j])   q[k] = tmp[i++];
            else{
                ans += mid - i + 1;
                q[k] = tmp[j++];
            }
        }
    }
}

整数二分搜索

import java.util.*;
class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sTimes = sc.nextInt();
        int[] arr = new int[n];
        for(int i =0; i < n; i++){
            arr[i] = sc.nextInt();
        }
        for(int i = 0; i < sTimes; i++){
            int target = sc.nextInt();
            int left = leftBound(arr, target);
            if(left == -1){
                System.out.println("-1 -1");
                continue;
            }
            int right = rightBound(arr, target);
            System.out.println(left + " " + right);
        }
        
    }
    
    public static int leftBound(int[] arr, int target){
        int l = 0, r = arr.length - 1;
        while(l < r){
            int mid = l + r >> 1;
            if(arr[mid] >= target)  r = mid;
            else{
                l = mid + 1;
            }
        }
        return arr[l] == target ? l : -1;
    }
    
    public static int rightBound(int[] arr, int target){
        int l = 0, r = arr.length - 1;
        while(l < r){
            int mid = l + r + 1 >> 1;    //注意这里
            if(arr[mid] <= target) l = mid;
            else r = mid - 1;
        }
        return arr[l] == target ? l : -1;
    }
}

小数二分模板,求3次根方

import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double in = sc.nextDouble();
        double l = -1e4, r = 1e4;
        while(r - l > 1e-8){
            double mid = (l + r) / 2;
            if(mid * mid * mid <= in)   l = mid;
            else r = mid;
        }
        System.out.printf("%.6f",l);
    }
}

 

你可能感兴趣的:(java)