PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)

简介: PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)

题目链接:点击打开链接

题目大意:略。

解题思路:先筛选所有 in-out 合法的一对车,顺便记录最长的时间(见注释“core”),接着分别存入所有合法 in,合法 out 的 time,从小到大排序 + 二分查找,最后做相减(in-out)操作就可以知道还有多少车在停车场内。

AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
structnode{
stringid;
intsta,time,h,m,s,sum;
}nds[10009];
intn;
intin[10009], out[10009];
map<string,vector<node>>mpv;
intcmp(noden1, noden2)
{
returnn1.id==n2.id?n1.time<n2.time:n1.id<n2.id;
}
intmain()
{
intq,h,m,s,ma=-1;
charid[20], sta[5];
scanf("%d%d",&n,&q);
for(inti=0;i<n;i++)
    {
scanf("%s%d:%d:%d%s",id,&h,&m,&s,sta);
nds[i].id=id, nds[i].h=h, nds[i].m=m, nds[i].s=s, nds[i].sta=sta[0]=='i'?1:0;
nds[i].time=h*3600+m*60+s;
    }
sort(nds,nds+n,cmp);
for(inti=1;i<n;i++)
    {
if(nds[i].id==nds[i-1].id&&nds[i].sta==0&&nds[i-1].sta==1) // core        {
intlen=mpv[nds[i].id].size();
if(len>0)
            {
nds[i].sum+=mpv[nds[i].id][len-1].sum;
            }
nds[i].sum+=nds[i].time-nds[i-1].time;
ma=max(ma,nds[i].sum);
mpv[nds[i-1].id].push_back(nds[i-1]);
mpv[nds[i].id].push_back(nds[i]);
        }
    }
intl1=0,l2=0,len;
for(autoit:mpv) // 只需 in/out 的车,存入time即可    {
len=it.second.size();
for(inti=0;i<len;i+=2)
in[l1++]=it.second[i].time;
for(inti=1;i<len;i+=2)
out[l2++]=it.second[i].time;
    }
sort(in,in+l1);
sort(out,out+l2);
for(inti=0,rs1,rs2;i<q;i++)
    {
scanf("%d:%d:%d",&h,&m,&s);
rs1=upper_bound(in,in+l1,h*3600+m*60+s)-in;
rs2=upper_bound(out,out+l2,h*3600+m*60+s)-out;
printf("%d\n",rs1-rs2);
    }
for(autoit:mpv)
    {
len=it.second.size();
if(it.second[len-1].sum==ma)
        {
printf("%s ",it.first.c_str());
        }
    }
printf("%02d:%02d:%02d\n",ma/3600,ma%3600/60,ma%60);
return0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
95 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
108 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
119 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
140 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
138 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
117 0
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
86 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
126 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
122 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
112 0