PAT 1095. Cars on Campus (30)
来源:互联网 发布:java poi jar包下载 编辑:程序博客网 时间:2024/06/07 18:15
思路不难,就是处理起来略繁
查询的时候题目说明按照时间顺序查询,就暗示要进行前面的record删掉,一开始用STL中的list的erase超时了,后来用vector记录上次不满足条件的起始地址,类似于伪删除
#include<iostream>#include<vector>#include<map>#include<string.h>#include<string>#include<list>#include<algorithm>#define IN 0#define OUT 1using namespace std;int second[100000];struct car{ int hour, min, sec; int time; int state; string card; bool checked;}node;struct duration{ int begin,end;}item;struct List{ struct duration node; struct List *next;}*head, *p,*tail;vector<struct car> record;vector<struct duration> rec;map<string, int> m,t;vector<string> stored;vector<int> order[10000];list<struct duration>ca;bool cmp_record(const struct car a, const struct car b){ return a.time < b.time;}void PrintTime(int max){ int hour, min, sec; hour = max / 3600; max -= hour * 3600; min = max / 60; sec = max - min * 60; if (hour < 10) printf("0%d:", hour); else printf("%d:", hour); if (min < 10) printf("0%d:", min); else printf("%d:", min); if (sec < 10) printf("0%d\n", sec); else printf("%d\n",sec);}bool cmp_duration(const struct duration &a, const struct duration &b){ if (a.begin == b.begin) return a.end < b.end; else return a.begin < b.begin;}int main(){ freopen("1.in", "r", stdin); int numofrecord, numofquery; cin >> numofrecord >> numofquery; int i; int hour, min, sec; char card[10], state[10]; for (i = 0; i < numofrecord; i++){ scanf("%s %d:%d:%d %s", card, &hour, &min, &sec, state); node.card = card; node.hour = hour; node.min = min; node.sec = sec; node.time = hour * 3600 + min * 60 + sec; node.checked = false; if (strcmp(state,"in") == 0) node.state = IN; else node.state = OUT; record.push_back(node); } sort(record.begin(), record.end(),cmp_record); vector<struct car>::iterator itrecord; int j,index; int countcar = 1; head = tail = NULL; for (i = 0;i<record.size(); i++){ if (record[i].state == IN) { if (m[record[i].card]>0) order[m[record[i].card]-1].push_back(i); else { m[record[i].card] = countcar; order[countcar-1].push_back(i); countcar++; } } else if (record[i].state == OUT&&m[record[i].card]){ index = m[record[i].card] - 1; for (j = order[index].size()-1; j >= 0; j--) if (record[order[index][j]].checked == false) { record[order[index][j]].checked = true; item.begin = record[order[index][j]].time; item.end = record[i].time; rec.push_back(item); t[record[i].card] += item.end - item.begin; break; } else break; } } int time = 0; sort(rec.begin(), rec.end(), cmp_duration); int pre = 0; list<struct duration>::iterator lit; vector<struct duration>::iterator rit; for (i = 0; i < numofquery; i++) { scanf("%d:%d:%d", &hour, &min, &sec); time = hour * 3600 + min * 60 + sec; int count = 0; while (pre!=rec.size()&&rec[pre].end<=time){ pre++; } for (j = pre; j < rec.size(); j++) if (time>=rec[j].begin&&time<rec[j].end) count++; else if (rec[j].begin > time) break; printf("%d\n", count); } int max = 0; map<string, int>::iterator mit; for (mit = t.begin(); mit != t.end();mit++) if (mit->second>max){ stored.clear(); max = mit->second; stored.push_back(mit->first); } else if (mit->second == max){ stored.push_back(mit->first); } sort(stored.begin(), stored.end()); for (i = 0; i < stored.size(); i++) cout << stored[i] << " "; PrintTime(max); return 0;}
0 0
- PAT A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- 【PAT】1095. Cars on Campus (30)
- pat-A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- 1095. Cars on Campus (30) PAT 甲级
- PAT 1095. Cars on Campus (30)
- PAT-A-1095. Cars on Campus (30)
- PAT A 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- Pat(A) 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus (30)
- PAT 甲级 1095. Cars on Campus (30)
- PAT 1095. Cars on Campus
- 【PAT】1095. Cars on Campus
- PAT Cars on Campus (30)
- [转]PAT甲级练习1095. Cars on Campus (30)
- 发布网站遇到的问题
- WebService随笔
- angularJS 学习笔记3 injector创建过程
- Spring4 MVC Hibernate4集成 Annotation
- python总结(二)
- PAT 1095. Cars on Campus (30)
- 七大排序算法java实现
- SQL语句操作数据库中表和字段的COMMENT值
- nginx 下载限速
- hdu2544最短路(floyed)
- 反编译android应用,降低权限去广告及重新签名
- Android 自定义控件 轻松实现360软件详情页
- MFC中picture控件坐标转换
- ajax提交数据struts2取得参数值为空的问题