首页 > 试题广场 >

会议室问题

[编程题]会议室问题
  • 热度指数:2171 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
一些项目要占用一个会议室宣讲,会议室不能同时容纳两个项目的宣讲。 给你每一个项目开始的时间和结束的时间(给你一个数 组,里面是一个个具体 的项目),你来安排宣讲的日程,要求会议室进行的宣讲的场次最多。 返回这个最多的宣讲场次。

输入描述:
第一行输入一个n代表有n场演讲(n <= 200)
下面n行需要输入两个整数 start、end代表会议开始时间和结束时间,其中(1<= start<=end <= 24)


输出描述:
输出一个整数,这个整数代表最多的宣讲场次
示例1

输入

3
1 10
11 20
10 11

输出

3
示例2

输入

3
6 12
7 8 
8 9

输出

2

n=int(input())
a=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    a[i][0],a[i][1]=list(map(int,input().split()))
a.sort(key=(lambda x: (x[1],x[0])))              
x=a[0][1]
c=1
for i in range(1,n):
    if a[i][0]>=x:
        x=a[i][1]
        c+=1
print(c)

发表于 2021-04-22 18:49:01 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<vector<int>> grid(n, vector<int>(2));
    for(int i = 0; i < n; i++){
        cin>>grid[i][0]>>grid[i][1];
    }
    // 按区间end值升序排序,注意若end值相等时保证start升序
    sort(grid.begin(), grid.end(), [](const auto &a, const auto &b){
        if(a[1]==b[1])
            return a[0] < b[0];
        return a[1] < b[1];
    });
    int res = 1, right = grid[0][1];
    for(int i = 1; i < n; i++){
        if(right<=grid[i][0]){
            res++;
            right = grid[i][1];
        }
    }
    cout<<res;
    return 0;
}
发表于 2023-09-27 18:29:54 回复(0)

Java 贪心每次安排会议时间最早结束的会议

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main{

    public static Integer method(int[][] meets){
          Integer res = 1;
          Arrays.sort(meets,(int[] o1,int[] o2)->{
              if(o1[1] > o2[1]){
                  return 1;
              }
              if(o1[1] == o2[1]){
                  return o1[0] - o2[0];
              }
              // o1[1] < O2[1]
              return -1;
          });

          int preEnd = meets[0][1];              
          for(int i=1; i < meets.length; i++){
             // System.out.print(meets[i][0]+" ");
           //   System.out.println(meets[i][1]);
              int nextStart = meets[i][0];
              if(nextStart >= preEnd){
                  res++;
                  preEnd = meets[i][1];
               //  System.out.println("---------------------");
              }
          }
          return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        if(n < 2){
            System.out.println(n);
            return;
        }
        int[][] meets = new int[n][2]; 
        for(int i = 0;i<n;i++){
            String[] meetTime = bf.readLine().split(" ");
            int start = Integer.parseInt(meetTime[0]);
            int end = Integer.parseInt(meetTime[1]);
            meets[i] = new int[]{start, end};
        }
       System.out.println(method(meets));
    }

}
发表于 2022-09-10 17:23:00 回复(0)
这道题有bug
测试样例:
127 2 23 12 19 8 13 6 15 10 11 1 5 8 9 10 10 2 23 1 17 11 12 7 9 2 4 14 14 10 16 15 22 3 16 11 19 5 10 16 20 19 22 2 23 2 22 13 17 10 19 18 23 17 18 5 16 5 20 18 19 2 7 8 12 2 18 18 23 16 21 7 13 9 22 8 21 6 9 15 23 10 16 3 9 9 19 4 8 11 22 6 6 3 11 12 12 1 5 10 19 9 10 6 19 10 18 17 19 4 12 2 16 14 16 16 18 2 9 12 16 3 22 11 14 6 11 6 23 16 22 8 17 2 18 11 12 14 16 1 18 14 23 6 22 6 6 21 23 2 13 2 17 4 16 14 20 1 4 1 4 4 22 2 21 6 12 3 9 13 23 16 19 8 22 18 19 16 22 2 19 15 23 1 15 19 21 2 16 8 19 8 9 13 16 21 23 8 19 11 16 3 8 1 19 14 16 5 11 5 23 15 21 3 16 8 9 4 8 23 23 3 9 14 21 1 18 4 23 10 12 11 15 12 17 7 22 22 22 12 13 16 20 12 14 9 14 11 16 2 8 5 13 8 17
答案输出的会议顺序是:
[1,4] [6, 6] [6, 6] [6, 9] [9, 10] [10, 10] [10, 11] [11, 12] [12, 12] [12, 13] [14, 14] [14, 16] [16, 18] [18, 19] [19, 21] [22, 22] [23, 23]
[6, 6] [6, 6] [6,9]这个答案明显是不对的
贴一个自己写的代码,包含去重,相同的开始和结束时间的会议只能有一个
def solution(matrix):
    matrix = set(matrix)
    matrix = list(matrix)
    matrix.sort(key=lambda x: x[1])  # 按照结束时间对会议进行排序,不需要考虑开始时间,然后贪心求解
    ans = 1
    n = len(matrix)
    pre_end = matrix[0][1]
    for i in range(1, n):
        start, end = matrix[i][0], matrix[i][1]
        if pre_end<=start:
            ans+=1
            pre_end = matrix[i][1]
    return ans
 
if __name__=='__main__':
    n = int(input())
    matrix = []
    for i in range(n):
        start, end = map(int, input().split())
        matrix.append((start, end))
    ans = solution(matrix)
    print(ans)




发表于 2022-07-21 14:44:45 回复(2)
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(pair<int, int> & a, pair<int, int> & b)
{
	if (a.second == b.second)	return a.first < b.first;
    // 当end相等时,按照start升序,例如:[1,2] [2,2] 是可以算两天的
	return a.second < b.second;
}

int main(void)
{
	int n, start, end,result = 0;
	scanf("%d", &n);
	vector<pair<int, int>> conference; // first:start,second:end

	for (int i = 0; i < n; i++) {
		scanf("%d %d", &start, &end);
		conference.push_back(make_pair(start, end));
	}

	sort(conference.begin(), conference.end(),cmp);

	start = conference[0].first;
	for (int i = 0; i < n; i++) {
		if(start <= conference[i].first){
			result++;
			start = conference[i].second;
		}
		//cout << conference[i].first << "  " << conference[i].second << endl;
	}
	
	cout << result;

	return 0;
}

发表于 2022-07-03 22:21:21 回复(0)
import java.util.*;
public class Main {
    private static class Node{
        public int start;
        public int end;
        public Node(int start,int end){
            this.start=start;
            this.end=end;
        }     
    }
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int lineCount=input.nextInt();
        if(lineCount<=0){
            System.out.println(0);
            return;
        }
        Node[] array=new Node[lineCount];
        for(int i=0;i<lineCount;i++){
            array[i]=new Node(input.nextInt(),input.nextInt());
        }
        Arrays.sort(array,(e1,e2)->e1.start-e2.start);
        Arrays.sort(array,(e1,e2)->e1.end-e2.end);
        
        int start=array[0].start;
        int count=0;
        for(Node node: array){
            if(start<=node.start){
                count++;
                start=node.end;
            }
        }
        System.out.println(count);
    }
}


发表于 2022-05-04 16:05:40 回复(0)
package main

import (
    "sort"
    "fmt"
)

func main(){
   // 输入 n 
    n := 0
    fmt.Scanln(&n)

    //buffer := bufio.NewScanner(os.Stdin)
    //for buffer.Scan(){
    //    buffer.Text()
    //}
    nums := make([][]int, n)

    for i := 0; i < n; i++ {
        x, y := 0, 0
        fmt.Scanf("%d %d", &x, &y)
        nums[i] = []int{x,y}
    }

    // fmt.Println(nums)
    
    sort.Slice(nums, func(i, j int) bool {
        // 题目中给定了start 可能等于end,所以,end值相等时,将start值升序排列
        return nums[i][1]<nums[j][1] || nums[i][1]==nums[j][1] && nums[i][0]<nums[j][0]
    })
    x := nums[0][1]
    res:=1
    for i := 1; i <n ; i++ {
        if x<=nums[i][0]{
            res++
            x = nums[i][1]
        }
    }
    fmt.Println(res)
}
发表于 2022-01-23 15:30:31 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<vector<int>> grid(n, vector<int>(2));
    for(int i = 0; i < n; i++){
        cin>>grid[i][0]>>grid[i][1];
    }
    // 按区间end值升序排序,注意若end值相等时保证start升序
    sort(grid.begin(), grid.end(), [](const auto &a, const auto &b){
        if(a[1]==b[1])
            return a[0] < b[0];
        return a[1] < b[1];
    });
    int res = 1, right = grid[0][1];
    for(int i = 1; i < n; i++){
        if(right<=grid[i][0]){
            res++;
            right = grid[i][1];
        }
    }
    cout<<res;
    return 0;
}


编辑于 2021-11-06 11:38:57 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[][] time = new int[n][2];
        for(int i = 0; i < n; i++){
            String[] chars = br.readLine().split(" ");
            for(int j = 0; j < chars.length; j++){
                time[i][j] = Integer.parseInt(chars[j]);
            }
        }
        Arrays.sort(time, new Comparator<int[]>(){
            @Override
            public int compare(int[] o1, int[] o2){
                if(o1[0] == o2[0]){
                    // 若俩数组的第一个元素相等,则比较它们的第二个元素
                    return o1[1] - o2[1];
                }else {
                    // 若俩数组的第一个元素不相等,则按从小到大的顺序排列
                    return o1[0] - o2[0];
                }
            }
        });

        int lastMeeting = 0;
        int sum = 1;
        int endTime = time[0][1];
        for(int i = 1; i < n; i++){
            if(time[i][0] < endTime){ //有冲突了,比较上一个回会议和这个会议的停止时间
                if(time[lastMeeting][1] > time[i][1]){ //上一次的结束时间更晚,就放弃上一次会议
                    lastMeeting = i;
                    endTime = time[i][1];
                }
            } else { //没有冲突,把这次会议加进去
                lastMeeting = i;
                endTime = time[i][1];
                sum++;
            }
        }
        System.out.println(sum);
    }
}
发表于 2021-05-12 21:53:13 回复(0)

问题信息

上传者:小小
难度:
9条回答 4358浏览

热门推荐

通过挑战的用户

会议室问题