AtCoder Beginner Contest 338 -- E - Chords -- 题解

目录

 E - Chords

题目描述:

思路解析:

代码实现 :


 E - Chords

题目描述:

 

思路解析:

        弦上有没有交点,如果将结点1和结点2n之间的连接断开,其实可以等价于在1-2n的区间上,这n个线段是否有交点。 

这里我们使用栈来实现检查是否发生相交情况,遍历1-2n如果碰到一个线段的左端点将左端点加入栈中,如果碰到一个右端点,弹出栈中最后加入的左端点,如果左右端点不能匹配,则发生相交情况。

代码实现 :

import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Main {
    public static void main(String[] args) throws IOException {
        int n = input.nextInt();
        int[] st = new int[2 * n];
        int[] cnt = new int[n];
        boolean loop = false;
        for (int i = 0; i < n; i++) {
            int a = input.nextInt() - 1;
            int b = input.nextInt() - 1;
           st[a] = i;
           st[b] = i;
        }
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 2 * n; i++) {
            if (cnt[st[i]] == 0){
                cnt[st[i]]++;
                stack.add(st[i]);
            }else{
                if (stack.isEmpty() || st[i] != stack.pop()){
                    loop = true;
                    break;
                }
            }
        }
        if (loop) out.println("Yes");
        else out.println("No");
        out.flush();
        out.close();
        br.close();
    }


    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static Input input = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
    }

}

相关推荐

  1. AtCoder Beginner Contest 335 A-E 题解

    2024-01-29 11:50:03       25 阅读
  2. ABC344 A-E题解

    2024-01-29 11:50:03       22 阅读
  3. ABC349 A-E题解

    2024-01-29 11:50:03       10 阅读
  4. abc348 D~F题解

    2024-01-29 11:50:03       13 阅读
  5. (AtCoder Beginner Contest 330) 题解

    2024-01-29 11:50:03       9 阅读
  6. AtCoder Beginner Contest 338(A~E补题)

    2024-01-29 11:50:03       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 11:50:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 11:50:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 11:50:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 11:50:03       18 阅读

热门阅读

  1. flink源码分析 - 简单解析命令行参数

    2024-01-29 11:50:03       30 阅读
  2. 计算机网络(第六版)复习提纲16

    2024-01-29 11:50:03       25 阅读
  3. 重生之我从零开始学前后端——Week02

    2024-01-29 11:50:03       33 阅读
  4. 从研发转架构之路

    2024-01-29 11:50:03       35 阅读
  5. WebSocket实现私信功能

    2024-01-29 11:50:03       30 阅读
  6. ubuntu 增加 swap 空间大小

    2024-01-29 11:50:03       28 阅读
  7. Ubuntu 16 让ufw防火墙控制docker容器中所有端口

    2024-01-29 11:50:03       34 阅读
  8. 主流排序算法

    2024-01-29 11:50:03       34 阅读