Problem A. Fractional addition
Title description
Input format
Output format
sample input
two 2 4 3 2
sample output
5/16 3/8
Analysis of ideas
-
It may not be taken into account that it is reduced to the simplest fraction. At first, I thought it was impossible to simplify! The following is an even number, and the above is an even number+1 must be an odd number. How can we simplify it It was later discovered that the above may also be 1+1, that is, a=b
AC code
package bupt; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0; i < num; i++) { int a = scanner.nextInt(); int b =scanner.nextInt(); int max= Math.max(a, b); int min = Math.min(a, b); int down = (int) Math.pow(2, max); int up = (int) Math.pow(2, max-min); up = up + 1; while (up%2==0){ up/=2; down/=2; }//For example, 1/4+1/4 needs to be reduced to the simplest fraction System.out.println(up + "/" + down); } } }
Problem B. Minimum Heap
Title description
Input format
Output format
sample input
three one ten three 10 5 3 1 2 1 3 five 1 2 3 4 5 1 3 1 2 2 4 2 5
sample output
Yes No Yes
Analysis of ideas
-
This problem is because only to determine whether it is the minimum heap, it does not need to represent the tree, and only needs to store the weight of each vertex. Then judge whether the starting point weight value is less than the ending point weight value while inputting and changing. -
The general OJ questions have the following representations for common trees Father representation (There is only one father for a node in the naming tree, which is called the parental representation. I took it.), Child representation , Child brother representation 。 -
For binary tree representation.
Father representation
AC code
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0; i < num; i++) { int N = scanner.nextInt();// the number of vertexes; int[] vetexs = new int[101]; for (int j = 1; j <= N; j++) { //input weight of vertexes vetexs[j] = scanner.nextInt(); } boolean flag = true; for (int j = 0; j < N-1; j++) { //input relationship of edges int parent = scanner.nextInt(); int child = scanner.nextInt(); if (vetexs[parent] > vetexs[child]) { if (flag) { flag = false; } } } if (flag) { System.out.println("Yes"); }else { System.out.println("No"); } } scanner.close(); } }
Problem C. Process Management
Title description
Input format
Output format
sample input
two five FORK 0 1 QUERY 1 KILL 1 QUERY 1 QUERY 2 one QUERY 0
sample output
Yes No No Yes
Reference link: BOJ Problem Solving Report-268 Process Management
Analysis of ideas
-
KILL is the difficulty and pitfall of this question. KILL PID, The child process of the child process also needs to be KILL. therefore One way Each process has a member variable of the child process list. When FORK, the current process is chained along the direction of the parent process to the list of child processes of each parent process along the way. (I do this) Another method is to delete recursively when KILL (can't understand!) -
such as FORK 0 1 Because Java cannot format input, only output 。 See details below. The list remove method. The remove (int index) and remove (Object o) functions are completely different. The first remove is easy to understand. It is deleted according to the index. The second is to Object o And List<Object>list Use the equals function for loop matching, so you must override the equals function. Object. equals (Object o) This function also has many considerations. Let me list two articles:
next() And
nextLine() Input difference
next() Valid input (carriage return, line feed, TAB, and space are all invalid inputs) must be made before the input can be ended (otherwise, it is always in the status of waiting for input), and then the current input (carriage return, line feed, TAB, and space) can be ended using the ending symbol nextLine() : There is no constraint above, but there is only one ending symbol, namely carriage return, so you can enter a string containing spaces. But this function will receive carriage return.
-
Next () After entering, \n Will not disappear (either filtered by the next next () or NextLine() receive ), -
The nextLine() will disappear directly after entering, so the next nextLine() Can receive input normally, but not enter 。
scanner.nextLine();// Receive enter symbol
AC code
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Scanner; public class Process { public static Scanner scanner; public List<Aprocess> list = new ArrayList<Aprocess>(); public static void main(String[] args) { scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0; i < num; i++) { Process process = new Process(); process.handle(); } } public void handle(){ int N = scanner.nextInt(); list.add(new Aprocess("0","0")); scanner.nextLine();// Receive enter symbol for (int j = 0; j < N; j++) { String operation = scanner.nextLine(); String[] array = operation.split(" "); if (array[0].equals("QUERY")) { int index = queryPidInList(array[1]); if (index != - 1) { System.out.println("Yes"); }else{ System.out.println("No"); } }else if (array[0].equals("FORK")) { forkPidInList(array[2],array[1]); }else if (array[0].equals("KILL")) { killPidInList(array[1]); } } } public int queryPidInList(String pid){ for (ListIterator<Aprocess> iterator = list.listIterator(); iterator.hasNext();) { Aprocess aprocess = (Aprocess) iterator.next(); if (pid.equals(aprocess.PID)) { return iterator.nextIndex() -1; } } return -1; } public void forkPidInList(String pid,String parent){ int parentIndex = queryPidInList(parent); if (parentIndex != - 1) { Aprocess newAprocess = new Aprocess(pid, parent); list.add(newAprocess); while(true){ if (list.get(parentIndex). PID.equals("0")) { break; } list.get(parentIndex).childPid.add(newAprocess); parentIndex = queryPidInList(list.get(parentIndex).parentPid); } } } public void killPidInList(String pid){ if (! pid.equals("0")) { int pidIndex = queryPidInList(pid); if (pidIndex!=- 1) { List<Aprocess> lStrings = list.get(pidIndex).childPid; for (Iterator<Aprocess> iterator = lStrings.iterator(); iterator.hasNext();) { Aprocess aprocess = (Aprocess) iterator.next(); list.remove(aprocess); } list.remove(pidIndex); } } } class Aprocess{ String PID; String parentPid; List<Aprocess> childPid = new ArrayList<Aprocess>(); public Aprocess() { } public Aprocess(String pid){ this.PID = pid; this.parentPid = "0"; } public Aprocess(String pid,String parent){ this.PID = pid; this.parentPid = parent; } @Override public boolean equals(Object obj) { if (this. PID.equals(((Aprocess)obj). PID)) { return true; } return false; } } }
Problem D. Network transmission
Title description
Input format
Output format
sample input
two 3 2 2 1 3 1 1 2 3 2 3 6 6 4 1 5 1 5 6 2 2 1 20 2 3 5 3 4 5 6 3 1 2 3 4 6
sample output
five nineteen
Thinking analysis
-
Understand the question correctly. Node 1 will only send data once. So the shortest path is actually a Full arrangement In the inside, find the appropriate path. -
To find the shortest path, the Freudian algorithm is said to timeout, so the Kth time Dijkstra algorithm is used -
At the beginning, I used the set of triples to represent the graph. Triples are start 、 end 、 weight (weight value), which will result in Runtime error problems (Memory overrun is not an array or collection fetching overrun, but a memory overrun). Finally, the adjacency matrix is used to represent the graph.
Java full arrangement
AC code
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0; i < num; i++) { Main network = new Main(); network.handle(); } } public void handle() { MGraph mGraph = new MGraph(); mGraph.vertexNum = scanner.nextInt(); mGraph.edgesNum = scanner.nextInt(); mGraph.tartgetVertexNum = scanner.nextInt(); mGraph.initEmptyDist(); for (int i = 0; i < mGraph.edgesNum; i++) { int start = scanner.nextInt(); int end = scanner.nextInt(); int weight = scanner.nextInt(); mGraph.edges[start][end] = weight; mGraph.edges[end][start] = weight; mGraph.dist[start][end] = weight; mGraph.dist[end][start] = weight; } for (int i = 0; i < mGraph.tartgetVertexNum; i++) { mGraph.tartgetVetexes.add(scanner.nextInt()); } mGraph.Dijkstra(1); // output mGraph.calculateMinToTarger(); } class MGraph { int vertexNum; int edgesNum; int tartgetVertexNum; int[][] edges; List<Integer> tartgetVetexes = new ArrayList<Integer>(); int[][] dist; List<Integer> T = new ArrayList<Integer>(); int rootLength = -1; public void initEmptyDist() { dist = new int[vertexNum + 1][vertexNum + 1]; edges = new int[vertexNum + 1][vertexNum + 1]; for (int i = 1; i <= vertexNum; i++) { for (int j = 1; j <= vertexNum; j++) { dist[i][j] = -1; edges[i][j] = -1; } } } public void Dijkstra(int origin) { // init vertexes data for (int i = 1; i <= vertexNum; i++) { if (i != origin) { T.add(i); } } while (T.size() != 0) { int selectVertex = -1; int selectVertexInT = 0; int min = -1;// position is not corrent in the past time for (int i = 0; i < T.size(); i++) { if (dist[origin][T.get(i)] != - 1 && (dist[origin][T.get(i)] < min || min == -1)) { min = dist[origin][T.get(i)]; selectVertex = T.get(i); selectVertexInT = i; } } // use this vertex to modify others dists for (int i = 0; i < T.size(); i++) { int currentTargetVertex = T.get(i); int query = query(selectVertex, currentTargetVertex); if (query != - 1) { int modified = min + query(selectVertex, currentTargetVertex); if (dist[origin][currentTargetVertex] == -1 || dist[origin][currentTargetVertex] > modified) { dist[origin][currentTargetVertex] = modified; } } } T.remove(selectVertexInT); } } public int query(int start, int end) { int result = -1; if (edges[start][end]!= - 1) { result = edges[start][end]; } return result; } public void calculateMinToTarger() { for (int i = 0; i < tartgetVetexes.size(); i++) { Dijkstra(tartgetVetexes.get(i)); } permutation(tartgetVetexes, 0, tartgetVertexNum - 1); System.out.println(rootLength); } public void permutation(List<Integer> list, int from, int to) { if (to < from) return; if (from == to) { int tempRootLength = dist[1][list.get(0)]; for (int i = 1; i < list.size(); i++) { tempRootLength += dist[list.get(i - 1)][list.get(i)]; } if (rootLength == -1 || rootLength > tempRootLength) { rootLength = tempRootLength; } } else { for (int i = from; i <= to; i++) { swap(list, i, from); permutation(list, from + 1, to); swap(list, from, i); } } } public void swap(List<Integer> s, int i, int j) { int tmp = s.get(i); s.set(i, s.get(j)); s.set(j, tmp); } } class Edge{ public int end; public int weight; public Edge() { //TODO auto generated constructor stub } Edge(int end,int weight){ this.end = end; this.weight = weight; } } }
Code for runtime error problems
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); int num = scanner.nextInt(); for (int i = 0; i < num; i++) { Main network = new Main(); network.handle(); } } public void handle() { MGraph mGraph = new MGraph(); mGraph.vertexNum = scanner.nextInt(); mGraph.edgesNum = scanner.nextInt(); mGraph.tartgetVertexNum = scanner.nextInt(); mGraph.initEmptyDist(); for (int i = 0; i < mGraph.edgesNum; i++) { int start = scanner.nextInt(); int end = scanner.nextInt(); int weight = scanner.nextInt(); mGraph.SGroups.add(new ThreeGronp(start, end, weight)); mGraph.dist[start][end] = weight; mGraph.dist[end][start] = weight; } for (int i = 0; i < mGraph.tartgetVertexNum; i++) { mGraph.tartgetVetexes.add(scanner.nextInt()); } mGraph.Dijkstra(1); // output mGraph.calculateMinToTarger(); } class MGraph { int vertexNum; int edgesNum; int tartgetVertexNum; List<ThreeGronp> SGroups = new ArrayList<ThreeGronp>();// Information of // edges List<Integer> tartgetVetexes = new ArrayList<Integer>(); int[][] dist; List<Integer> T = new ArrayList<Integer>(); int rootLength = -1; public void initEmptyDist() { dist = new int[vertexNum + 10][vertexNum + 10]; for (int i = 1; i <= vertexNum; i++) { for (int j = 1; j <= vertexNum; j++) { dist[i][j] = -1; } } } public void Dijkstra(int origin) { // init vertexes data for (int i = 1; i <= vertexNum; i++) { if (i != origin) { T.add(i); } } while (T.size() != 0) { int selectVertex = -1; int selectVertexInT = 0; int min = -1;// position is not corrent in the past time for (int i = 0; i < T.size(); i++) { if (dist[origin][T.get(i)] != - 1 && (dist[origin][T.get(i)] < min || min == -1)) { min = dist[origin][T.get(i)]; selectVertex = T.get(i); selectVertexInT = i; } } // use this vertex to modify others dists for (int i = 0; i < T.size(); i++) { int currentTargetVertex = T.get(i); int query = query(selectVertex, currentTargetVertex); if (query != - 1) { int modified = min + query(selectVertex, currentTargetVertex); if (dist[origin][currentTargetVertex] == -1 || dist[origin][currentTargetVertex] > modified) { dist[origin][currentTargetVertex] = modified; } } } T.remove(selectVertexInT); } } public int query(int start, int end) { for (Iterator<ThreeGronp> iterator = SGroups.iterator(); iterator.hasNext();) { ThreeGronp threeGronp = (ThreeGronp) iterator.next(); if ((threeGronp.start == start && threeGronp.end == end) || (threeGronp.start == end && threeGronp.end == start)) { return threeGronp.weight; } } return -1; } public void calculateMinToTarger() { for (int i = 0; i < tartgetVetexes.size(); i++) { Dijkstra(tartgetVetexes.get(i)); } permutation(tartgetVetexes, 0, tartgetVertexNum - 1); System.out.println(rootLength); } public void permutation(List<Integer> list, int from, int to) { if (to < from) return; if (from == to) { int tempRootLength = dist[1][list.get(0)]; for (int i = 1; i < list.size(); i++) { tempRootLength += dist[list.get(i - 1)][list.get(i)]; } if (rootLength == -1 || rootLength > tempRootLength) { rootLength = tempRootLength; } } else { for (int i = from; i <= to; i++) { swap(list, i, from); permutation(list, from + 1, to); swap(list, from, i); } } } public void swap(List<Integer> s, int i, int j) { int tmp = s.get(i); s.set(i, s.get(j)); s.set(j, tmp); } } class ThreeGronp { int start; int end; int weight; public ThreeGronp(int start, int end, int weight) { this.start = start; this.end = end; this.weight = weight; } } }
C++Version Guide https://blog.csdn.net/TQCAI666/article/details/86765736