Calmer的文章

  • 首页
  • 文章归档
  • 关于页面

  • 搜索
体验游戏 笔记 推荐 工具链 工具使用 小游戏 插件 UI 软件 教程

考生位置调度

发表于 2020-08-01 | 分类于 算法 | 0 | 阅读次数 769

题目描述

假设有一个考场,考场有一排共 N 个座位,索引分别是 [0..N-1],考生会陆续进入考场考试,并且可能在任何时候离开考场。

其他解法:连接


using System;
class ExamRoom
{
    struct Line
    {
        public int distance;
        public int startIndex;
        public int endIndex;
        public int subDis;
        public Line(int a,int b,int c,int d)
        {
            distance = a;
            subDis = b;
            startIndex = c;
            endIndex = d;
        }
    }

    int[] seats;

    public ExamRoom(int N)
    {
        seats = new int[N];
        for(int i=0; i<N; i++)
        {
            seats[i]=0;
        }
    }

    public int Seat()
    {
        Line maxLine = new Line(0, -1, 0 ,0);
        int start = 0;
        for(int end=0; end<seats.Length; end++)
        {
            if(seats[end]==1 || end==seats.Length-1)
            {
                int curDis = end - start;
                if(seats[start]==1 && seats[end]==1)
                    curDis--;
                if(curDis > maxLine.distance && (end-start)/2>maxLine.subDis)
                {
                    maxLine.distance = curDis;
                    maxLine.startIndex = start;
                    maxLine.endIndex = end;
                    maxLine.subDis = (end-start)/2;
                }
                start = end;
            }
        }

        if(maxLine.distance > 0)
        {
            if(seats[maxLine.startIndex] == 0 && seats[maxLine.endIndex]==0)
            {
                seats[maxLine.startIndex] =1;
                return maxLine.startIndex;
            }
            else if(seats[maxLine.startIndex] == 0)
            {
                seats[maxLine.startIndex] = 1;
                return maxLine.startIndex;
            }
            else if(seats[maxLine.endIndex]==0)
            {
                seats[maxLine.endIndex] = 1;
                return maxLine.endIndex;
            }
            else
            {
                seats[maxLine.startIndex + (maxLine.endIndex - maxLine.startIndex)/2] = 1;
                return maxLine.startIndex + (maxLine.endIndex - maxLine.startIndex)/2;
            }
        }
        else
        {
            Console.WriteLine("没有位置了");
        }
        return -1;
    }

    public void Leave(int p)
    {
        seats[p] = 0;
    }
}
  • 本文作者: Calmer
  • 本文链接: https://mytechplayer.com/archives/考生位置调度
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
LRU算法
括号匹配
  • 文章目录
  • 站点概览
Calmer

Calmer

88 日志
7 分类
10 标签
RSS
Creative Commons
0%
© 2020 — 2025 Calmer
由 Halo 强力驱动
蜀ICP备20010026号-1川公网安备51019002006543
Copyright © 2020-2025 Calmer的文章