#训练5# 3-银行排队问题之单窗口“夹塞”版

题目

https://pintia.cn/problem-sets/15/problems/895
在这里插入图片描述

思路

看其他人的题解,都是用了queue,但是明明两个循环更方便一些嘛~
在这里插入图片描述
这个提示救了我一命!一开始情况没考虑全面
在这里插入图片描述
过程太乱了,就不放上来了(●’◡’●)

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>

#define maxx 10010
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    string name;
    int no;
    int hour;
} people[maxx];
int n, m;
map<string, int> friends;
bool vis[maxx];
int main()
{
    cin >> n >> m;
    if (n==1)
    {
        string  string1;
        int a,b;
        cin>>string1>>a>>b;
        cout<<string1<<endl;
        cout<<"0.0"<<endl;
        return 0;
    } 
    for (int i = 1; i <= m; ++i) //从1开始,因为map不匹配的话返回值是0
    {
        int x;
        cin>>x;
        while (x--)
        {
            string s;
            cin >> s;
            friends[s] = i;
        }
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> people[i].name >> people[i].no >> people[i].hour;
        if (people[i].hour > 60)
            people[i].hour = 60;
    }

    double time = 0;
    int wait = people[0].no;

    for (int i = 0; i < n; ++i)
    {
        int f = 0;
        if(!vis[i])
        {
            vis[i] = 1;
            cout<<people[i].name<<endl;
            time += wait-people[i].no;
            wait += people[i].hour;
            if (i<n && people[i+1].no> wait)
            {
                f = 1;
                int bre = people[i+1].no - wait;
                //time+=bre;
                wait+=bre;
            }
            for (int j = i+1; j < n && f==0; ++j)
            {
                if (people[j].no > wait)
                    break;
                if(friends[people[i].name] == friends[people[j].name])  //如果后面有人是一个集合里的
                {
                    vis[j] = 1;
                    cout<<people[j].name<<endl;
                    time += wait - people[j].no;
                    wait+=people[j].hour;
                }
            }
        }

    }
    printf("%.1f\n",time/n);
    return 0;
}

总结

  • map的匹配要从1开始,一开始总是把people[3]当成一个friends里的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值