A1095 Cars on Campus (30 分)


#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
struct record
{
	char id[10];
	int time;
	bool s;//in/out
}rec[10010], valid[10010];

map<string, int> parktime;//车牌号-总停留时间

bool cmp(record a, record b)
{
	if (strcmp(a.id, b.id) != 0) return strcmp(a.id, b.id) < 0;
	else  return a.time < b.time;

}
bool cmpt(record a, record b)
{
	return a.time < b.time;
}
int time(int hh, int mm, int ss)//化为秒计算时间的函数
{
	return hh * 3600 + mm * 60 + ss;
}
int main()
{
	int n, k;//n-records k-queries
	scanf("%d %d", &n, &k);
	char temp[20];
	int hh, mm, ss;
	for (int i = 0; i < n; i++)//input records
	{
		scanf("%s", rec[i].id);
		scanf("%d:%d:%d", &hh, &mm, &ss);
		scanf("%s", temp);
		rec[i].time = time(hh, mm, ss);
		if (strcmp(temp, "in") == 0) rec[i].s = 1;//in时为1,out为0
		else rec[i].s = 0;
	}
	sort(rec, rec + n, cmp);//第一次对全体sort 按照车牌号时间sort
	//输入完记录 开始整理有效记录
	int num = 0;//有效记录的计数
	int maxstay = -1;
	for (int i = 0; i < n - 1; i++)
	{
		if (rec[i].s == 1 && rec[i + 1].s == 0 && strcmp(rec[i].id, rec[i + 1].id) == 0)
		{
			valid[num++] = rec[i];
			valid[num++] = rec[i + 1];//最后的num为有效记录数
		   //处理最长时间问题
			int stay = rec[i + 1].time - rec[i].time;//停留时间
			if (parktime.count(rec[i].id) == 0) { parktime[rec[i].id] = 0; }//键id第一次出现时候,置零 count为小括号
			parktime[rec[i].id] += stay;
			maxstay = max(maxstay, parktime[rec[i].id]);
		}//if括号的位置,注意小细节
	}
	sort(valid, valid + num, cmpt);//第二次按照时间先后顺序sort 方便后面按照时间确定进出车数量
   //输入query
	for (int i = 0; i < k; i++)
	{
		scanf("%d:%d:%d", &hh, &mm, &ss);
		int nowtime = time(hh, mm, ss);
		int parknum = 0;
		for (int j = 0; j < num&&nowtime >= valid[j].time; j++)
		{
			if (valid[j].s == 1) parknum++;//当是in时候,停车数加一
			else parknum--;
		}
		printf("%d\n", parknum);
	}
	//输出停车时间最长的车牌号和时间
	map<string, int>::iterator it;
	for (it = parktime.begin(); it != parktime.end(); it++)//新学到的map用法
	{
		if (it->second == maxstay) printf("%s ", it->first.c_str());
		//第一次见到.c_str()的用法(注意str后面还有括号) string里面的输出,要在c下输出需要加这个,还可以加iostream的头文件,然后用cout输出
	}
	printf("%02d:%02d:%02d", maxstay / 3600, maxstay % 3600 / 60, maxstay % 60);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can generate an English speech on the topic of modern technology for you. Here it is: Good afternoon, ladies and gentlemen. The topic for my speech today is modern technology. Technology has transformed our lives in many ways, and it is difficult to imagine a world without it. One of the most significant technological advancements has been the internet. The internet has enabled us to communicate with anyone, anywhere in the world, and has made the world a smaller place. It has also revolutionized industries such as e-commerce, social media, and online education. Another technological advancement that has changed the way we live our lives is smartphones. These pocket-sized devices have become an indispensable part of our daily routine, allowing us to stay connected on the go, take photos and videos, and access information at our fingertips. Artificial intelligence and machine learning have also transformed the way we live and work. Self-driving cars, virtual assistants, and chatbots are just some of the ways in which AI is making our lives easier and more efficient. However, with every new technological development, there are also concerns about privacy, security, and the impact on jobs. It is important that we continue to investigate these issues and ensure that technology is used ethically. In conclusion, modern technology has had a tremendous impact on our lives, and it will continue to do so in the future. It is up to us to embrace these advancements while also being mindful of their potential effects. Thank you.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值