C. Emergency Evacuation (greedy)

ICPC — International Collegiate Programming Contest
Asia Regional Contest, Yokohama, 2018–12–09

C. Emergency Evacuation (greedy)

Original question:
Insert picture description hereInsert picture description here
Question intent: Send the designated person out of the car from the same exit, and there cannot be two people in the same position at the same time, ask for the shortest time required.

At first glance, I felt greedy, but from the perspective of pushing everyone out, it didn’t feel good to find a greedy strategy. The simulation was unrealistic and would definitely blow up. . .

Problem-solving method:
Reverse thinking, consider sending everyone back to their original position from the exit, and sort according to the distance from their original position to the exit. Put the farther distance in the front, which can be understood as the original position farther from the exit The person enters first, and the person whose original position is closer to the exit enters later, so that the time spent can be minimized and the optimal solution can be obtained.

#include<bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define scan(n) scanf("%d",&n)

struct per
{
    
    
  int r,c;//原位置
  int d;//原位置离出口的距离
};
struct per pe[500005];

bool cmp(struct per a,struct per b)
{
    
    
    return a.d>b.d;
}

int main()
{
    
    
    int r,s,p,i;
    cin>>r>>s>>p;
    for(i=0;i<p;i++)
    {
    
    
        cin>>pe[i].r>>pe[i].c;
        if(pe[i].c>s)
            pe[i].d=(pe[i].c-s)+(r-pe[i].r+1);
        else
            pe[i].d=(s-pe[i].c+1)+(r-pe[i].r+1);
    }
    sort(pe,pe+p,cmp);
    int k=1;//在队列中等待进入车厢的时间
    int maxtime=pe[0].d;
    for(i=1;i<p;i++)
    {
    
    
        if(pe[i].d+k>maxtime)
            maxtime=pe[i].d+k;
        k++;
    }
    cout<<maxtime<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_40534166/article/details/88894175