zoukankan      html  css  js  c++  java
  • 7-48 银行排队问题之单窗口“夹塞”版 (30分)--map,vector

    1 #include <iostream>
    2 #include<iomanip>
    3 #include <map>
    4 #include <string>
    5 #include <cstring>
    6 #include <queue>
    7 #include <vector>
    8 using namespace std;
    9 
    10 int main()
    11 {
    12     double time = 0;//记录当前时间
    13     double wait = 0;//记录总的等待时间
    14     int N, M;
    15     cin >> N >> M;
    16     vector<string>v;//存放所有客户名字
    17     map<string, int>grp;//朋友集合,map[s]=1说明s在第1组
    18     map<string, bool>grp_flag;//标志是否在某个朋友集合内的
    19     map<string, bool>fns_flag;//某人事务是否处理完成
    20     map<string, double>reach;//到达时间
    21     map<string, double>dur;//事务持续时间
    22     for (int i = 0; i < M; i++)
    23     {
    24         int n;
    25         cin >> n;
    26         for (int j = 0; j < n; j++)
    27         {
    28             string s;
    29             cin >> s;
    30             grp[s] =i;
    31             grp_flag[s] = true;
    32         }
    33     }
    34     for (int i = 0; i < N; i++)
    35     {
    36         string s;
    37         cin >> s;
    38         cin >> reach[s] >> dur[s];
    39         v.push_back(s);
    40     }
    41 
    42     for (int i = 0; i < v.size(); i++)
    43     {
    44         if (!fns_flag[v[i]])
    45         {
    46       fns_flag[v[i]]=true;
    47       if(dur[v[i]]>60)dur[v[i]]=60;
    48             cout << v[i] << endl;
    49             if (time > reach[v[i]])
    50                 wait += time-reach[v[i]];
    51             else
    52             {
    53                 time = reach[v[i]];
    54             }
    55             for (int j = 0; j < v.size(); j++)
    56                 {
    57                     if (i != j)
    58                     {
    59                         if (grp[v[j]] == grp[v[i]] && grp_flag[v[j]] && grp_flag[v[i]] && !fns_flag[v[j]])
    60                         {/*一开始漏了最后一个条件,第三四个测试点没有通过,因为不能保证将朋友的
    61             事务全部办理完,所以会有遗漏的朋友,当轮到遗漏的朋友自己排队完处理
    62             事务的时候,又会把他的朋友的事务再处理一遍,导致重复。*/
    63                             if (reach[v[j]] <= time + dur[v[i]])
    64                             {
    65                                 if (dur[v[j]] > 60)dur[v[j]] = 60;
    66                                 wait += time + dur[v[i]] - reach[v[j]];
    67                                 dur[v[i]] += dur[v[j]];
    68                                 fns_flag[v[j]] = true;
    69                 cout << v[j] << endl;
    70                             }
    71                         }
    72                     }
    73                 }
    74                 time += dur[v[i]];
    75         }
    76     }
    77     cout << setprecision(1) << fixed<<wait /N;
    78     return 0;
    79 }
  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/2020R/p/12824721.html
Copyright © 2011-2022 走看看