PAT 1095. Cars on Campus (30)

思路不难,就是处理起来略繁
查询的时候题目说明按照时间顺序查询,就暗示要进行前面的record删掉,一开始用STL中的list的erase超时了,后来用vector记录上次不满足条件的起始地址,类似于伪删除

#include
#include
#include
#include
#include
#include
#include
#define IN 0
#define OUT 1
using 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;iif (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&&timeelse 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;
}

你可能感兴趣的:(Pat)