2019CSP-J T2 【交通换乘】

Description

1.png

Solutions

这道题考虑用 模拟 来解题。

这道题纯属模拟,把每个记录依次读入。

如果是乘地铁的记录,就把它丢进一个栈里面,并将此票的价钱加入总花费中;

如果是乘公交的记录,开始处理它:如果有票可以用,就用掉符合条件的票,然后把这张票的记录从栈里删去(可以用一个bool数组来存该票的状态),如果无票符合条件,直接把要花的钱加入总花费中。

最后输出即可。

这道题就做完了。

Code

1
#include<cmath>
2
#include<cstdio>
3
#include<cstring>
4
#include<iostream>
5
#include<algorithm>
6
using namespace std;
7
const int N=1e5+1;
8
int n,tou,wei;
9
struct nod{int a,t;}f[100001];
10
long long int ans=0;
11
bool bz[100001],ls;
12
int main()
13
{
14
	freopen("transfer.in","r",stdin);
15
	freopen("transfer.out","w",stdout);
16
	scanf("%d",&n);tou=0;wei=0;
17
	for(int i=1;i<=n;i++)
18
	{
19
		int s,a,t;
20
		scanf("%d%d%d",&s,&a,&t);
21
		while(t-f[tou].t>45&&tou<=wei)tou++;
22
		if(s==0)
23
		{
24
			ans+=a;
25
			f[++wei].a=a;
26
			f[wei].t=t;
27
		}
28
		else
29
		{
30
			ls=0;
31
			for(int j=tou;j<=wei;j++)
32
			{
33
				if(!bz[j])
34
				{
35
					if(f[j].a>=a)
36
					{
37
						bz[j]=ls=1;
38
						break;
39
					}
40
				}
41
			}
42
			if(!ls)ans+=a;
43
		}
44
	}
45
	printf("%lld",ans);
46
	return 0;
47
}