达达0920

package dada;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main01 {
    static List<String> func01(int[][] a, int[] b, int x) {
        List<String> res = new ArrayList<>();
        int n = a.length;
        int bl = b[0], br = b[1];
        for(int i=0; i < n; i++) {
            int al = a[i][0], ar=a[i][1];
            if(x == 1 && bl <= al && br >= ar) res.add(al+","+ar);
            else if(x == 2) {
                if((al <= bl && ar >= bl) || (bl <= al && br >= al)) {
                    res.add(al+","+ar);
                }
            }
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int n = sc.nextInt();
        int[][] a = new int[n][2];
        for(int i=0; i < n; i++) {
            String[] str = sc.next().split(",");
            a[i][0] = Integer.valueOf(str[0]);
            a[i][1] = Integer.valueOf(str[1]);
        }
        int[] b = new int[2];
        String[] st = sc.next().split(",");
        b[0] = Integer.valueOf(st[0]);
        b[1] = Integer.valueOf(st[1]);
        //Arrays.sort(a,(o1,o2)->o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
        List<String> res = func01(a,b,x);
        for(int i=0; i < res.size(); i++) {
            System.out.println(res.get(i));
        }

    }
}

package dada;

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

public class Main02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.next().split(",");
        int n = str.length;
        int[] a = new int[n+1];
        for(int i=0; i < n; i++) {
            a[i+1] = Integer.valueOf(str[i]);
        }
        int m = sc.nextInt();
        int[][] f = new int[m][n+1];
        for(int i=0; i < m; i++) Arrays.fill(f[i],Integer.MAX_VALUE/2);
        f[0][1] = a[1];
        for(int i=2; i <=n; i++) f[0][i] = Math.max(f[0][i-1], a[i-1]);
        for(int i=1; i < m; i++) f[i][1] = a[1];
        for(int i=1; i < m; i++) {
            for(int j=2; j <= n; j++) {
                int maxn = a[j];
                for(int k= 1; j-k >= i; k++) {
                    maxn = Math.max(maxn, a[j-k+1]);
                    // f[i-1][j-k] + max(a[j-k+1,...,j]
                    f[i][j] = Math.min(f[i][j], f[i-1][j-k] + maxn);
                }
            }
        }
        System.out.println(Arrays.deepToString(f));
        if(Integer.MAX_VALUE/2 == f[m-1][n])
            System.out.println(-1);
        else
            System.out.println(f[m-1][n]);
    }


}

/*
* 第i天完成前j个项目最少的花费数
* f[i][j] = Math.min(f[i-1][k] + Math.max(f[k+1..j]), k=1,2,j-1)
*
* */
package dada;

import java.util.Random;
import java.util.Scanner;

public class Main03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] a = new int[n][2];
        int sum = 0;
        for(int i=0; i < n; i++) {
            String[] str = sc.next().split(",");
            a[i][0] = Integer.valueOf(str[0]);
            int t = Integer.valueOf(str[1]);
            sum += t;
            a[i][1] = sum;
        }
        //System.out.println(sum);
        for(int j=0; j < 10000; j++) {
            double t = Math.random() * sum;
            for(int i=0; i < n; i++) {
                if(t <= a[i][1]) {
                    //System.out.println(t+","+a[i][0]);
                    System.out.println(a[i][0]);
                    break;
                }
            }
        }


    }
}

posted @ 2020-09-20 17:38  li修远  阅读(131)  评论(0编辑  收藏  举报