【无标题】


package org.uestc.demo.shixi;

import java.util.ArrayDeque;
import java.util.PriorityQueue;
import java.util.Scanner;

public class CodeTrain1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCount = sc.nextInt();
        boolean[] res = new boolean[testCount];
        for (int i = 0; i < testCount; i++) {
            int N = sc.nextInt(), M = sc.nextInt(), A = sc.nextInt(), B = sc.nextInt();
            ArrayDeque<Edge>[] adj = new ArrayDeque[N];
            for (int j = 0; j < M; j++) {
                int u = sc.nextInt(), v = sc.nextInt(), c = sc.nextInt();
                ArrayDeque<Edge> uedges = adj[u];
                if (uedges == null) {
                    uedges.add(new Edge(u, v, c));
                }
                ArrayDeque<Edge> vedges = adj[v];
                if (vedges == null) {
                    vedges.add(new Edge(v, u, c));
                }
            }
        }
    }

    /**
     * 求最短路径值
     *
     * @param adj 邻接表
     * @param s   起点
     * @param e   终点
     * @return 范湖最短路径的长度
     */
    private static int dijkstra(ArrayDeque<Edge> adj, int s, int e) {
        int[] edgeTo = new int[adj.size()], distTo = new int[adj.size()];
        for (int i = 0; i < distTo.length; i++) {
            distTo[i] = Integer.MAX_VALUE;
        }
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        edgeTo[s] = 0;
    }

    private static class Edge {
        public int from;
        public int to;
        public int weight;

        public Edge(int from, int to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }
    }
}

package org.uestc.demo.shixi;

import java.util.Scanner;

public class CodeTrain2 {
    public static void main(String[] args) {
        compute_fib_array();
    }

    private static void compute_fib_array() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; ++i) {
            arr[i] = sc.nextInt();
        }
        int rowCnt = m / n;
        int columnCnt = m % n - 1;
        int[] comarr = new int[n];
        for (int i = 0; i < rowCnt; i++) {
            for (int j = 0; j < n; j++) {
                int left = j == 0 ? n - 1 : j - 1;
                int right = (j + 1)% n;
                comarr[j] = arr[left] + arr[right];
            }
            System.arraycopy(comarr, 0, arr, 0, n);
        }
        System.out.println(arr[columnCnt]);
    }

}


package org.uestc.demo.shixi;

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

public class CodeTrain3 {
    public static void main(String[] args) {
        diff_perfect_num();
    }

    private static void diff_perfect_num() {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int[] arr = new int[num];
        for (int i = 0; i < num; i++) {
            arr[i] = scanner.nextInt();
        }
        int len = scanner.nextInt();
        int count = 0;
        Arrays.sort(arr);
        int last = num - 1;
        while (true) {
            if (minus(arr, last, len)) {
                count++;
            } else {
                break;
            }
            if (arr[last] == 0) last--;
        }
        System.out.println(count);
    }

    private static boolean minus(int[] arr, int end, int len) {
        if (end + 1 < len) return false;
        int count = len;
        for (int i = end; i >= end - len && count > 0; i--) {
            if (arr[i] > 0) {
                arr[i]--;
                count--;
            }
        }
        return count == 0;
    }
}

package org.uestc.demo.shixi;

import java.util.Scanner;

public class CodeTrain4 {
    public static void main(String[] args) {
        most_cheap_plan();
    }

    private static void most_cheap_plan() {
        Scanner scanner = new Scanner(System.in);
        int fishNum = scanner.nextInt();
        int c1 = scanner.nextInt(), n1 = scanner.nextInt(), c2 = scanner.nextInt(), n2 = scanner.nextByte();
        long price = Integer.MAX_VALUE;
        int m1 = -1, m2 = -1;
        int ic = fishNum / n1;
        int jc = fishNum / n2;
        for (int i = 0; i < ic + 1; i++) {
            for (int j = 0; j < jc + 1; j++) {
                int fishCount = i * n1 + j * n2;
                int pri = i * c1 + j * c2;
                if (i * n1 + j * n2 == fishNum && price > pri) {
                    price = pri;
                    m1 = i;
                    m2 = j;
                } else if (fishCount < fishNum) {
                    continue;
                } else if (fishCount > fishNum){
                    break;
                }
            }
        }
        if (m1 == -1 && m2 == -1) {
            System.out.println("failed");
        } else {
            System.out.println(m1 + " " + m2);
        }
    }
}

package org.uestc.demo.shixi;

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

/**
 * 求解区间并集
 */
public class CodeTrain5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] intervals = new int[n][2];
        for (int i = 0; i < n; i++) intervals[i][0] = sc.nextInt();
        for (int i = 0; i < n; i++) intervals[i][1] = sc.nextInt();
        List<Integer[]> sections = mergeAndSumIntervals(intervals);
        int count = 0;
        for (Integer[] section : sections) {
            count += section[1] - section[0] + 1;
        }
        System.out.println(count);
    }

    /**
     * 从这些区间中选出至少选出一个数,求这个数的返回
     * @return 这个数的返回,区间的集合
     */
    private static List<Integer[]> mergeAndSumIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> (a[0] - b [0]));
        List<Integer[]> res = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            int start = intervals[i][0], end = intervals[i][1];
            if (res.isEmpty()) {
                res.add(new Integer[]{start, end});
                continue;
            }
            Integer[] lastInterval = res.get(res.size() - 1);
            int oldEnd = lastInterval[1];
            if (start < oldEnd) {
                lastInterval[1] = oldEnd + end;
            } else if (start > oldEnd) {
                end += oldEnd;
                res.add(new Integer[]{start, end});
            }
        }
        return res;
    }

    /**
     * 合并区间
     * @param intervals 要合并的区间
     * @return
     */
    private static List<Integer[]> mergeSection(int[][] intervals) {
        List<Integer[]> res = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            int start = intervals[i][0], end = intervals[i][1];
            if (res.isEmpty()) {
                res.add(new Integer[]{start, end});
            } else {
                Integer[] lastIntervals = res.get(res.size() - 1);
                if (start > lastIntervals[1]) {
                    res.add(new Integer[]{start, end});
                } else if (end > lastIntervals[1]) {
                    lastIntervals[1] = end;
                }
            }
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值