hdu 1146 UVa10901 Ferry Loading III

看了网上很多代码,都不是很好。我觉得我这个最好理解。

用两个队列储存 左边和右边正在等待的车

用数组e[]  储存所有车

#include<stdio.h>
#include<stdlib.h>
#include <algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pi acos(-1.0)
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x));
#define N 10005
#define eps 1e-8
void fre(){freopen("t.txt","r",stdin);}
using namespace std;

struct node
{
    int l,t,index;//l表示位置0为左,1为右,t表示入队时间,index表示其标号
}e[N];
queue<node>q[2];//0:l  1:r  
int ans[N];
bool cmp(node a,node b) {return a.t < b.t;}
int main(void)
{
 //   fre();
    int T,i,j,n,t,m;
    char s[10];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&t,&m);
        for(i=0; i <m; ++i) //input
        {
            scanf("%d%s",&e[i].t,s);
            if(s[0]=='l') e[i].l = 0;
            else e[i].l = 1;
            e[i].index = i;
        }
        sort(e,e+m,cmp);
        e[m].t = 1<<30;//为了方便判断到达终点,防止越界
        i = 0;
        int nt=0,nl=0;
        while(i!=m||!q[0].empty()||!q[1].empty())//solve
        {
            if(q[nl].empty())//所在地无车的情况
            {
                if(q[nl^1].empty())//两边无车的情况
                {
                    nt = e[i].t;
                    q[e[i].l].push(e[i]);i++;
                    while(e[i].t <= nt) {q[e[i].l].push(e[i]);i++;}
                }
                else//只有对面有车的情况
                {
                    nt+=t; nl = nl^1;// printf("%d\n",e[i].t);
                    while(e[i].t <= nt) {q[e[i].l].push(e[i]);i++;}
                }
            }
            else//所在地有车的情况
            {
                nt+=t;
                for(j = 0; j < n&&!q[nl].empty(); ++j)
                {
                    node tem=q[nl].front();
                    q[nl].pop();
                    ans[tem.index] = nt;
                }
                while(e[i].t <= nt) {q[e[i].l].push(e[i]);i++;}
                nl = nl^1;
            }
        }
        for(i = 0; i < m; ++i)//output
        {
            printf("%d\n",ans[i]);
        }
        if(T) puts("");
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值