UVA 822 Queue and A

桃扇骨 2023-06-04 14:54 132阅读 0赞

题目链接:https://vjudge.net/problem/UVA-822

翻译摘自:《算法禁赛入门经典》

题目大意

  你的任务是模拟一个客户中心运作情况。客服请求一共有n(1 ≤ n ≤ 20)种主题,每种主 题用 5 个整数描述:tid, num, t0, t, dt,其中 tid 为主题的唯一标识符,num为该主题的请求个 数,t0 为第一个请求的时刻,t 为处理一个请求的时间,dt 为相邻两个请求之间的间隔(为了简化情况,假定同一个主题的请求按照相同的间隔到达)。

  客户中心有m(1 ≤ m ≤ 5)个客服,每个客服用至少 3 个整数描述:pid, k, tid 1 , tid 2 , …, tid k,表示一个标识符为 pid 的人可以处理 k 种主题的请求,按照优先级从大到小依次为 tid 1 , tid 2 , …, tid k。当一个人有空时,他会按照优先级顺序找到第一个可以处理的请求。如果有多 个人同时选中了某个请求,上次开始处理请求的时间早的人优先;如果有并列,再输入列表前面的优先。输出最后一个请求处理完毕的时刻。

分析

  这道题我是崩溃的,首先 udebug 上的程序跑出来的数据是错误的,我在网上找了一份 AC 代码,和《算法禁赛入门经典》的 AC 代码对拍,两个跑出来还是不一样。

  题目是好题,但 不推荐做,因为数据实在太马虎了。

代码如下

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include <bits/stdc++.h>
  2. 2 using namespace std;
  3. 3
  4. 4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. 5 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
  6. 6 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
  7. 7 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
  8. 8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. 9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12. 12
  13. 13 #define pr(x) cout << #x << " = " << x << " "
  14. 14 #define prln(x) cout << #x << " = " << x << endl
  15. 15
  16. 16 #define LOWBIT(x) ((x)&(-x))
  17. 17
  18. 18 #define ALL(x) x.begin(),x.end()
  19. 19 #define INS(x) inserter(x,x.begin())
  20. 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. 21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
  22. 22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. 23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24. 24
  25. 25 #define ms0(a) memset(a,0,sizeof(a))
  26. 26 #define msI(a) memset(a,0x3f,sizeof(a))
  27. 27 #define msM(a) memset(a,-1,sizeof(a))
  28. 28
  29. 29 #define MP make_pair
  30. 30 #define PB push_back
  31. 31 #define ft first
  32. 32 #define sd second
  33. 33
  34. 34 template<typename T1, typename T2>
  35. 35 istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. 36 in >> p.first >> p.second;
  37. 37 return in;
  38. 38 }
  39. 39
  40. 40 template<typename T>
  41. 41 istream &operator>>(istream &in, vector<T> &v) {
  42. 42 for (auto &x: v)
  43. 43 in >> x;
  44. 44 return in;
  45. 45 }
  46. 46
  47. 47 template<typename T>
  48. 48 ostream &operator<<(ostream &out, vector<T> &v) {
  49. 49 Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - 1];
  50. 50 return out;
  51. 51 }
  52. 52
  53. 53 template<typename T1, typename T2>
  54. 54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. 55 out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. 56 return out;
  57. 57 }
  58. 58
  59. 59 inline int gc(){
  60. 60 static const int BUF = 1e7;
  61. 61 static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62. 62
  63. 63 if(bg == ed) fread(bg = buf, 1, BUF, stdin);
  64. 64 return *bg++;
  65. 65 }
  66. 66
  67. 67 inline int ri(){
  68. 68 int x = 0, f = 1, c = gc();
  69. 69 for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
  70. 70 for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
  71. 71 return x*f;
  72. 72 }
  73. 73
  74. 74 template<class T>
  75. 75 inline string toString(T x) {
  76. 76 ostringstream sout;
  77. 77 sout << x;
  78. 78 return sout.str();
  79. 79 }
  80. 80
  81. 81 inline int toInt(string s) {
  82. 82 int v;
  83. 83 istringstream sin(s);
  84. 84 sin >> v;
  85. 85 return v;
  86. 86 }
  87. 87
  88. 88 //min <= aim <= max
  89. 89 template<typename T>
  90. 90 inline bool BETWEEN(const T aim, const T min, const T max) {
  91. 91 return min <= aim && aim <= max;
  92. 92 }
  93. 93
  94. 94 typedef unsigned int uI;
  95. 95 typedef long long LL;
  96. 96 typedef unsigned long long uLL;
  97. 97 typedef vector< int > VI;
  98. 98 typedef vector< bool > VB;
  99. 99 typedef vector< char > VC;
  100. 100 typedef vector< double > VD;
  101. 101 typedef vector< string > VS;
  102. 102 typedef vector< LL > VL;
  103. 103 typedef vector< VI > VVI;
  104. 104 typedef vector< VB > VVB;
  105. 105 typedef vector< VS > VVS;
  106. 106 typedef vector< VL > VVL;
  107. 107 typedef vector< VVI > VVVI;
  108. 108 typedef vector< VVL > VVVL;
  109. 109 typedef pair< int, int > PII;
  110. 110 typedef pair< LL, LL > PLL;
  111. 111 typedef pair< int, string > PIS;
  112. 112 typedef pair< string, int > PSI;
  113. 113 typedef pair< string, string > PSS;
  114. 114 typedef pair< double, double > PDD;
  115. 115 typedef vector< PII > VPII;
  116. 116 typedef vector< PLL > VPLL;
  117. 117 typedef vector< VPII > VVPII;
  118. 118 typedef vector< VPLL > VVPLL;
  119. 119 typedef vector< VS > VVS;
  120. 120 typedef map< int, int > MII;
  121. 121 typedef unordered_map< int, int > uMII;
  122. 122 typedef map< LL, LL > MLL;
  123. 123 typedef map< string, int > MSI;
  124. 124 typedef map< int, string > MIS;
  125. 125 typedef multiset< int > mSI;
  126. 126 typedef set< int > SI;
  127. 127 typedef stack< int > SKI;
  128. 128 typedef deque< int > DQI;
  129. 129 typedef queue< int > QI;
  130. 130 typedef priority_queue< int > PQIMax;
  131. 131 typedef priority_queue< int, VI, greater< int > > PQIMin;
  132. 132 const double EPS = 1e-8;
  133. 133 const LL inf = 0x7fffffff;
  134. 134 const LL infLL = 0x7fffffffffffffffLL;
  135. 135 const LL mod = 1e9 + 7;
  136. 136 const int maxN = 1e5 + 7;
  137. 137 const LL ONE = 1;
  138. 138 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  139. 139 const LL oddBits = 0x5555555555555555;
  140. 140
  141. 141 struct Event{
  142. 142 int id; // 活动对应实体在数组中的序号
  143. 143 int time;
  144. 144 bool isRorC; // 是请求到达还是客服空闲事件
  145. 145
  146. 146 Event(int x, int y, bool z) {
  147. 147 time = x;
  148. 148 id = y;
  149. 149 isRorC = z;
  150. 150 }
  151. 151
  152. 152 bool operator< (const Event &x) const {
  153. 153 return time > x.time;
  154. 154 }
  155. 155 };
  156. 156
  157. 157 struct ReqInfo {
  158. 158 int tid; // 请求id
  159. 159 int idx; // 数组下标id
  160. 160 int num; // 请求数量
  161. 161 int t0; // 第一个请求到达时间
  162. 162 int t; // 请求处理耗时
  163. 163 int dt; // 两个相邻请求之间的到达间隔
  164. 164 };
  165. 165
  166. 166 istream& operator>> (istream& in, ReqInfo &x) {
  167. 167 in >> x.tid >> x.num >> x.t0 >> x.t >> x.dt;
  168. 168 return in;
  169. 169 }
  170. 170
  171. 171 struct StaffInfo {
  172. 172 int pid; // 职员id
  173. 173 int idx; // 数组下标id
  174. 174 int k; // 职员所能处理的请求数量
  175. 175 VI tids; // 所能处理的请求id,优先级从大到小
  176. 176 int last; // 上次开始处理请求的时间
  177. 177
  178. 178 bool operator< (const StaffInfo &x) const {
  179. 179 return last < x.last || last == x.last && idx < x.idx;
  180. 180 }
  181. 181 };
  182. 182
  183. 183 istream& operator>> (istream& in, StaffInfo &x) {
  184. 184 in >> x.pid >> x.k;
  185. 185 x.tids.resize(x.k);
  186. 186 Rep(i, x.k) in >> x.tids[i];
  187. 187 return in;
  188. 188 }
  189. 189
  190. 190 int N, M, T, ans;
  191. 191 vector< ReqInfo > reqs;
  192. 192 vector< StaffInfo > staffs;
  193. 193
  194. 194 priority_queue< Event > waitQ; // 等待队列,按时间升序
  195. 195 mSI mInQ; // 当前队列中的所有任务
  196. 196 SI freeStaffs; // 空闲员工集合
  197. 197
  198. 198 bool readInfoAndGenerateEvent() {
  199. 199 MII ridToIdx; // 存请求id与实际存的数组下标的对应关系
  200. 200
  201. 201 cin >> N;
  202. 202 if(!N) return false;
  203. 203 reqs.resize(N);
  204. 204 Rep(i, N) {
  205. 205 auto &r = reqs[i];
  206. 206
  207. 207 cin >> r;
  208. 208 ridToIdx[r.tid] = i;
  209. 209 r.idx = i; // 把请求id变成数组下标,方便写代码
  210. 210
  211. 211 Rep(j, r.num) waitQ.push(Event(r.t0 + r.dt * j, i, true)); // 请求到达活动
  212. 212 }
  213. 213
  214. 214 cin >> M;
  215. 215 staffs.resize(M);
  216. 216 Rep(i, M) {
  217. 217 auto &sf = staffs[i];
  218. 218
  219. 219 cin >> sf;
  220. 220 Rep(j, sf.k) sf.tids[j] = ridToIdx[sf.tids[j]];
  221. 221 sf.idx = i; // 把客服id变成数组下标,方便写代码
  222. 222 sf.last = 0;
  223. 223
  224. 224 waitQ.push(Event(0, i, false)); // 客服空闲活动
  225. 225 }
  226. 226
  227. 227 return true;
  228. 228 }
  229. 229
  230. 230 struct StaffCompByIdx {
  231. 231 bool operator() (int x, int y) const {
  232. 232 return staffs[x] < staffs[y];
  233. 233 }
  234. 234 };
  235. 235
  236. 236 void solve() {
  237. 237 int nowTime = waitQ.top().time;
  238. 238 ans = nowTime;
  239. 239
  240. 240 while(!waitQ.empty() && nowTime == waitQ.top().time) { // 把到达的活动全取出来
  241. 241 const Event &e = waitQ.top();
  242. 242
  243. 243 if(e.isRorC) mInQ.insert(e.id); // 有新请求到达了
  244. 244 else freeStaffs.insert(e.id); // 有客服空闲了
  245. 245
  246. 246 waitQ.pop();
  247. 247 }
  248. 248
  249. 249 // 分配任务
  250. 250 while(!mInQ.empty() && !freeStaffs.empty()) { // 任务队列里有请求并且有员工空闲
  251. 251 vector< set< int, StaffCompByIdx > > req_staffs(N); // 每种请求可供受理的员工集合
  252. 252 bool canAssign = false; // 是否能分配任务
  253. 253
  254. 254 for(auto &i : freeStaffs) { // 枚举每个空闲客服
  255. 255 auto &fs = staffs[i];
  256. 256
  257. 257 Rep(j, fs.k) { // 依次遍历每一项可处理的请求
  258. 258 int tid = fs.tids[j];
  259. 259
  260. 260 if(mInQ.find(tid) == mInQ.end()) continue;
  261. 261 canAssign = true;
  262. 262
  263. 263 req_staffs[tid].insert(fs.idx); // 第一个可处理的请求
  264. 264 break; // 直接退出即可,后面优先级的请求等这轮循环没分配到再进入下一轮分配
  265. 265 }
  266. 266 }
  267. 267
  268. 268 if(!canAssign) break; // 没人分配到工作就退出
  269. 269
  270. 270 Rep(i, N) { // 枚举请求号
  271. 271 auto &rs = req_staffs[i];
  272. 272
  273. 273 while(mInQ.find(i) != mInQ.end() && !rs.empty()) { // 任务队列中有这个请求并且有员工能处理这个请求
  274. 274 mInQ.erase(mInQ.find(i));
  275. 275
  276. 276 int pid = *(rs.begin()); // 排名第一的能处理 i 号请求的员工 id,给他分配任务
  277. 277 auto &sf = staffs[pid];
  278. 278
  279. 279 sf.last = nowTime;
  280. 280 waitQ.push(Event(nowTime + reqs[i].t, sf.idx, false)); // 下一个客服空闲请求
  281. 281 freeStaffs.erase(sf.idx);
  282. 282 rs.erase(pid);
  283. 283 }
  284. 284 }
  285. 285 }
  286. 286 }
  287. 287
  288. 288 int main() {
  289. 289 //freopen("MyOutput.txt","w",stdout);
  290. 290 //freopen("input.txt","r",stdin);
  291. 291 //INIT();
  292. 292 while(readInfoAndGenerateEvent()) {
  293. 293 ans = 0;
  294. 294 freeStaffs.clear();
  295. 295
  296. 296 while(!waitQ.empty()) solve();
  297. 297 printf("Scenario %d: All requests are serviced within %d minutes.\n", ++T, ans);
  298. 298 }
  299. 299 return 0;
  300. 300 }

转载于:https://www.cnblogs.com/zaq19970105/p/11378887.html

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读