Autumn strokes puppy Collection

Puppy Map Design

Title Description

Now need to design a Map <key, Val> , the following conditional
Map capacity is a fixed value N , up to N records stored
when the insert operation, the first query key exists:

  1. If there: if val> old_val when only update val
  2. If there is no: When size <N times , the value is stored, when the N == size , out of the first record, and adding new key and val

Description : When a record is updated at time T, the record then becomes the update time T.

Entry

The first line contains the maximum capacity of the Map N
after each line comprising a key and a val (key.length <16, val is unsigned int)

Export

Output record to be eliminated

Sample input

2
10_123_50_A0 1566918054
10_123_50_A1 1566918054
10_123_50_A1 1566918055
10_123_50_A3 1566918055
10_123_50_A4 1566918056

Sample input

1566918054 10_123_50_A0
10_123_50_A1 1566918055

Thinking

Map normal line application, using the queue write down the recording sequence can be inserted

coding

import java.util.Map;
import java.util.Queue;
import java.util.HashMap;
import java.util.Scanner;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        Map<String, Long> map = new HashMap<>();
        Queue<String> queue = new LinkedList<>();  // 记录 Map记录插入的顺序
        while(true) {
            String key = scn.next();
            long val = scn.nextLong();
            if(map.containsKey(key)) {
                if(map.get(key) < val) {
                    map.put(key, val);
                    queue.remove(key);
                    queue.add(key);
                }
            } else {
                if(map.size() < n) {
                    map.put(key, val);
                    queue.add(key);
                } else {
                    String firstKey = queue.poll();
                    System.out.println(firstKey + " " + map.get(firstKey));
                    map.remove(firstKey);
                    map.put(key, val);
                    queue.add(key);
                }
            }
        }
    }
}

Puppy server distribution

Title Description

We have a total n servers, each server to be several sub-service data is transmitted, n servers form a tree-like structure .
Now you want to copy data from the root node start distributed to all servers.
A data transfer required an hour, a node can simultaneously k son nodes transmit
different nodes can be parallel distribution , and asked, all the distribution is completed, the minimum number of hours required?
Description : The server has only one root and numbered 0, the number of servers <= 1e6, numbers in the range [0, 1e6)
[example]
        0
      / \
    12
  / \
34
When k = 1 when the optimal transmission process as follows:
first hour: 0 -> 1
second hour: 1 -> 4 && 0 -> 2
third hour: 1 -> 4
Therefore, when k = 1, the minimum required to complete all of the distribution . 3 th hours
when k 2 = time,Optimum transmission process is as follows:
first hour: 0 -> 1 && 0 -> 2
second hour: 1 -> 1 && 3 -> 4
so that when k = 2, the minimum required to complete all of the distribution 2 Ge Xiaoshi

Entry

The first input line of the maximum number of concurrent single node k, and the remaining input line number n
next n rows, each row of the first number indicates the number of nodes of the back, the second number of the parent node ID, back are all sub-node number

Export

All server output distribution is completed, the minimum time required

Sample input

1 2
3 0 1 2
2 1 3

Sample Output

2

Thinking

Topic unspecified binary tree, temporarily not think of any better solution
then simulate again the subject of a given operation.
When thinking about the custom containing numbers and with the son of a list of classes (in accordance with their son list in descending order of the number of sons )
reason for the number of sons as weights: In accordance with greedy thoughts, the more the son, they can pass the time in their offspring, You can also pass other son to save time
and then define a Queue, ran over and over again is the transmission of data (List may be)
defined count counter, you can get the answer
NOTE: because when possible traversal Queue involving insertion and deletion, traversal might the problem
definition of the queue prior tmpList recording element, and can be updated while traversing tmpList queue
Further if only binary tree lower attachment practice, of course, should be in accordance with the meaning of the title is unlikely. . .

coding

import java.util.*;

public class Main {
    private static final TreeNode root = new TreeNode(0);  // 定义好一定有的根服务器
    private static class TreeNode implements Comparable<TreeNode> {
        int val, weight, tag;
        // val:服务器编号,weight:儿子数量,tag:用于标记遍历到哪个儿子
        List<TreeNode> child;

        TreeNode(int val) {
            this.val = val;
            weight = 1;
            tag = 0;
            child = null;
        }
        @Override
        public int compareTo(TreeNode treeNode) {  // 按儿子数量降序
            return treeNode.weight - this.weight;
        }
    }
    private static int insert(TreeNode treeNode, int parent, List<TreeNode> child) {
        if(treeNode == null) return 0;
        if(treeNode.val == parent) {
            treeNode.child = child;
            treeNode.weight += child.size();
            return child.size();
        }
        int sum = 0;
        if(treeNode.child != null) {
            for(TreeNode nextNode : treeNode.child) {
                int weight = insert(nextNode, parent, child);
                sum += weight;
                if(weight != 0) break;
            }
        }
        treeNode.weight += sum;
        return sum;
    }

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int k = scn.nextInt(), n = scn.nextInt();
        for(int i = 0; i < n; ++i) {
            int m = scn.nextInt(), parent = scn.nextInt();
            List<TreeNode> child = new ArrayList<>();
            for(int j = 1; j < m; ++j) {
                child.add(new TreeNode(scn.nextInt()));
            }
            insert(root, parent, child);
        }
        Queue<TreeNode> queue = new LinkedList<>();
        List<TreeNode> list = new LinkedList<>();
        queue.add(root);
        Collections.sort(root.child);
        int count = 0;
        while(!queue.isEmpty()) {
            ++count;
            list.addAll(queue);
            for(TreeNode treeNode : list) {
                Collections.sort(treeNode.child);
                for(int i = 0; i < k; ++i) {
                    if(treeNode.tag < treeNode.child.size()) {
                        TreeNode child = treeNode.child.get(treeNode.tag++);
                        if(child.child != null)
                            queue.add(treeNode);
                    }
                    if(treeNode.tag == treeNode.child.size()) {
                        queue.remove(treeNode);
                        break;
                    }
                }
            }
            list.clear();
        }
        System.out.println(count);
    }
}

If only binary tree approach

import java.util.Scanner;

public class Main {
    private static int maxDepth = 0, maxNode = 0;
    public static final int maxLen = 1000001;
    private static int[] tree = new int[maxLen];
    private static void insert(int root, int parent, int leftChild, int rightChild, int depth) {
        if(root != 1 && tree[root] == 0) return;
        int leftNode = root<<1, rightNode = leftNode | 1;
        if(root == parent) {
            maxDepth = Math.max(depth+1, maxDepth);
            maxNode = Math.max(leftChild, maxNode);
            tree[leftNode] = leftChild;
            tree[rightNode] = rightChild;
            return;
        }
        insert(leftNode, parent, leftChild, rightChild, depth+1);
        insert(rightNode, parent, leftChild, rightChild, depth+1);
    }
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int k = scn.nextInt(), n = scn.nextInt();
        for(int i = 0; i < n; ++i) {
            int m = scn.nextInt(), parent = scn.nextInt(), leftChild = scn.nextInt(), rightChild;
            rightChild = m == 3 ? scn.nextInt() : 0;
            insert(1, parent, leftChild, rightChild, 1);
        }
        int ans = k == 1 && tree[maxNode|1] != 0 ? maxDepth+1 : maxDepth;
        System.out.println(ans);
    }
}

Puppy nuclear fusion

Title Description

Please design a program, within a specified number of steps, so that the success of a nucleus queue complete annihilation .
A total of seven kinds of nuclei, the queue in a straight line, the initial state is stable .
Only two cases annihilation occurs:

  1. When inserted when a nucleus, the insertion position is formed the same three or more nuclei , it annihilation
  2. After annihilation , and both sides of the collision ripple formed in the same three or more nuclei , it annihilation

NOTE : Only insert the same nuclei, or collision will annihilate, for example, the case of not
ABBCCC (insert B 1) -> ACCC (into the stable)
for insertion described in terms of position, assuming the queue length is n, numbered from 0 to n- 1 of the n + 1 position.
Requires players to give a elimination process, all of its nuclei are annihilated, simply does not exceed the number of steps can be completed, it does not require the optimal solution

Entry

The first line is a line of characters showing the initial sequence of the nucleus
of the second row is a predetermined number of steps m (m <= 100)

Export

A plurality of rows, each row comprising a letter and a number indicating the position of a letter inserted

Sample input

AAABBAAACCABDCAABC

Sample Output

. 8 C
C. 3
C. 3
D. 5
B 0
B 0
C 0
C 0
Description : valid output is not unique , the optimal solution is not going to, within a predetermined number of steps to.

Thinking

// ALL

coding

Guess you like

Origin www.cnblogs.com/qq188380780/p/11495749.html