Emergency Evacuation 思维

题意:一辆车,分成两侧,中间有一条直接到车尾的通道,通道两边分别有r排,s列座位,人们要从座位上移动到通道,再到车尾,每次移动1个单位用1的时间,问所有人都离开车的时间。如果一个人从座位移动到通道,如果相邻的通道的位置上有人,座位上的人无法通过,必须等到这个位置没人时才能过。

分析:这个题如果从正面想的话,很难想出一个策略。所以可以转换成从车尾的人移动到各自座位的时间。

    我们让距离车尾远的先走,这样可以保证时间最少。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2 * 500 * 500 + 5;
 4 struct Node{
 5     int r, s, dis;
 6 }a[maxn];
 7 int cmp(Node a, Node b){
 8     return  a.dis > b.dis;
 9 }
10 int r, s, p;
11 int main(){
12     scanf("%d%d%d", &r, &s, &p);
13     for (int i = 1; i <= p; i++){
14         scanf("%d%d", &a[i].r, &a[i].s);
15         if (a[i].s <= s)a[i].dis = r - a[i].r + 1 + s - a[i].s+ 1;
16         else a[i].dis = r - a[i].r + 1 + a[i].s - s;
17     }
18     int k = 1, t = a[1].dis;
19     sort(a + 1, a + 1 + p, cmp);
20     for (int i = 2; i <= p; i++){
21         t = max(t, a[i].dis + k);
22         k++;
23     }
24     cout << t;
25     return 0;
26 }
View Code
原文地址:https://www.cnblogs.com/ghosh/p/12679216.html