标签:osi namespace view may 目的 思路 rom spl opp
描述
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river‘s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up to n cars that are waiting to cross. If there are more than n, those that have been waiting the longest are loaded. If there are no cars waiting on either bank, the ferry waits until one arrives, loads it (if it arrives on the same bank of the ferry), and crosses the river. At what time does each car reach the other side of the river?
输入
The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day), and the bank at which the car arrives ("left" or "right").
输出
For each test case, output one line per car, in the same order as the input, giving the time at which that car is unloaded at the opposite bank. Output an empty line between cases.
You may assume that 0 < n, t, m <= 10000. The arrival times for each test case are strictly increasing. The ferry is initially on the left bank. Loading and unloading time may be considered to be 0.
样例输入
2
2 10 10
0 left
10 left
20 left
30 left
40 left
50 left
60 left
70 left
80 left
90 left
2 10 3
10 right
25 left
40 left
样例输出
10
30
30
50
50
70
70
90
90
110
30
40
60
题意:
一条船两头跑,把左岸的车运到右岸,右岸的车运到左岸。一条船一次可以运最多n辆车,一共m辆车。往返的时间相同,均为t,求每辆车到达目的地的时间(船初始在左岸)。
船的运输原则时有车就运,如果船的当前所在岸无车等待,另一岸有车等待,船就必续向另一岸航行。
思路:简单的模拟过程,两个队列维护两岸的来车。每次让当前岸口正在等待的车上船(当然是来的早的先上),要是满了剩下的就等下一批,当前上船的车就都知道了到达时间。
若当前的维护的时间没车,时间就跳到两岸来车的时间较早者,并按题目运输原则。
#include<bits/stdc++.h> using namespace std; struct node{ int id,t,ans; bool operator <(const node &a){ return id<a.id; } }u; queue<node> q[2]; int main(){ int cas,n,m,t,d=0; scanf("%d",&cas); while(cas--){ if(d)printf("\n"); d++; scanf("%d%d%d",&n,&t,&m); for(int i=1;i<=m;i++){ int x; char str[10]; scanf("%d%s",&x,str); if(str[0]==‘l‘)q[0].push((node){i,x,0}); else q[1].push((node){i,x,0}); } int pos=0,pos_time=0; vector<node> vec; while(!q[0].empty()||!q[1].empty()){ int num=0; while(!q[pos].empty()&&q[pos].front().t<=pos_time&&num<n){ num++; u=q[pos].front(); u.ans=pos_time+t; q[pos].pop(); vec.push_back(u); } if(num==0){ if(q[1-pos].empty()||!q[pos].empty()&&q[pos].front().t<q[1-pos].front().t){ pos_time=q[pos].front().t; }else if(!q[1-pos].empty()){ pos_time=max(pos_time,q[1-pos].front().t)+t; pos=1-pos; } }else { pos=1-pos; pos_time+=t; } } sort(vec.begin(),vec.end()); for(int i=0;i<vec.size();i++){ printf("%d\n",vec[i].ans); } } }
标签:osi namespace view may 目的 思路 rom spl opp
原文地址:https://www.cnblogs.com/llhsbg/p/12909985.html