头条编程

https://www.nowcoder.com/test/8537279/summary

算法1:排序,Java过不了,得C++,MMP

package toutiao1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[][]a=new int[n][2];
        int maxY=-1, idx=-1;
        for(int i=0;imaxY) {
                maxY=a[i][1];
                idx=i;
            }
        }
        
        Stack st = new Stack();
        st.push(a[idx]);
        for(int i=0;it[0] && a[i][1]>t[1])) break;
                st.pop();
            }
            st.push(st.isEmpty()?a[idx]:a[i]);
        }
        
        Listl=new ArrayList();
        while(!st.isEmpty()) l.add(st.pop());
        Collections.sort(l, new Comparator(){
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        for(int[] t:l)
            System.out.println(t[0]+" "+t[1]);
    }
}

算法2:单调栈

package toutiao2;

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[]a=new int[n];
        for(int i=0;ist=new Stack();
        for(int i=0;i=a[st.peek()]) {
                    st.push(i);
                } else {
                    while(!st.isEmpty() && a[i]

算法3:模拟一遍

你可能感兴趣的:(头条编程)