Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

    • 数组
      • 华为
        • 机器人监考
    • 二分
    • 树
    • 动态规划
    • 回溯
    • 图
  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 笔试真题
Nreal
2023-12-31
目录

数组

# 华为

# 机器人监考

题目描述:

同时监考一场消耗3个金币,监考多场消耗4个金币,不监考消耗1个金币,计算机器人花费;

输入:

第一行考试次数 n;

接下来n行分别为:起始时间,结束时间;

输出:

花费金币数;

案例:

3
1 5
4 6
6 6
输出 :21
1
2
3
4
5
public class T3 {
    static int[] time = new int[100006];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.valueOf(br.readLine());
        int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            String[] input = br.readLine().split(" ");
            int l = Integer.valueOf(input[0]);
            int r = Integer.valueOf(input[1]);
            min = Math.min(min,l);
            max = Math.max(max,r);
            time[l]++;
            time[r+1]--;
        }

        int ans = 0;
        int pre = 0;
        for(int i=min;i<=max;i++){
            int cur = pre+time[i];
            pre = cur;
            if(cur==0){
                ans += 1;
            }else if(cur==1){
                ans += 3;
            }else {
                ans += 4;
            }
        }
        System.out.println(ans);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
剑指offer(针对hot100补充)
二分

← 剑指offer(针对hot100补充) 二分→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式