1095. Cars on Campus (30)

102 篇文章 0 订阅
37 篇文章 0 订阅
 

1095. Cars on Campus (30)

时间限制
220 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<= 10000), the number of records, and K (<= 80000) the number of queries. Then N lines follow, each gives a record in the format

plate_number hh:mm:ss status

where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. Each "in" record is paired with the chronologically next record for the same car provided it is an "out" record. Any "in" records that are not paired with an "out" record are ignored, as are "out" records not paired with an "in" record. It is guaranteed that at least one car is well paired in the input, and no car is both "in" and "out" at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:

For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

Sample Input:
16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00
Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
输入
N个车辆进出记录  K个要查询的时间点
N行  车牌  时间(00:00:00~23:59:59)  进in/出out
……
K行  查询的时间(对应输出这个时间点停车场的车辆数。这个时间点如果正好有车有操作,那么车辆数是操作后的 【进+1/出-1/无0】)
……
最后给出一天中累计停放的时间最长的车牌号(多个按字母序输出)   并输出这个最长时间
PS:由于K行的查询是按时间顺序进行的,那么可以直接从头搜到尾。也可以用统计放到数组里面再用二分法,测试出来的时间差不多,内存有点区别。但是如果全部用cin,cout会偶尔提交AC偶尔超时。
下面代码,一:无二分法scanf-printf;
        二:二分法scanf-prinft;
        三:二分法cin-cout(第三个看一下时间,就是会偶尔超时的那个,太酸爽了)

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月19日 15:35答案正确301095C++ (g++ 4.7.2)48948datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确129618/18
1答案正确119483/3
2答案正确115643/3
3答案正确13841/1
4答案正确488203/3
5答案正确108202/2
#include<iostream> 
#include<string.h>
#include<string>
#include<vector> 
#include<algorithm> 
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector<carslist>*cars, int N)
{
  int hh, mm, ss;
  char in_out[5];
  int index; 
  for (index = 0; index < N; index++)
  { 
    scanf("%s%d:%d:%d%s", (*cars)[index].plate, &hh, &mm, &ss,in_out);  
    //cin >> (*cars)[index].plate >> hh >> in_out[0] >> mm >> in_out[0] >> ss >> in_out; 
    (*cars)[index].time = hh * 3600 + mm * 60 + ss;
    (*cars)[index].in = in_out[0] == 'i' ? true : false; 
  }
}
bool everycarsTimecmp(const carslist&A, const carslist&B)
{
  if (strcmp(A.plate,B.plate)==0)return A.time < B.time;
  else return strcmp(A.plate, B.plate)<0;
}
bool TotalTimesort(const carslist&A, const carslist&B)
{
  return A.time < B.time;
}
int KickWrong_Addlongest(vector<carslist>*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector<carslist>::iterator iter = (*cars).begin(); iter != (*cars).end(); )
  {
    if (iter->in&&iter + 1 != (*cars).end() && strcmp(iter->plate, (iter + 1)->plate)== 0 && !(iter + 1)->in) /*in_out一定成对存在,题目要求*/
    {

      if (Precar == iter->plate) 
          ThecarmaxT += (iter + 1)->time - iter->time;/*累记一整天的停车时长*/ 
      else
      {
        Precar = iter->plate;
        ThecarmaxT = (iter + 1)->time - iter->time; 
      }
      if (maxTime < ThecarmaxT)
      {
        (*staylongest).clear();
        (*staylongest).push_back(Precar);
        maxTime = ThecarmaxT;
      }
      else if (maxTime == ThecarmaxT)
        (*staylongest).push_back(Precar);
      iter += 2;
    }
    else iter = (*cars).erase(iter); 
  }
  return maxTime;
} 
void read_ans(int K, vector<carslist>*cars)
{
  int hh, mm, ss,firstindex=0,cnt=0; 
  while (K--)
  {
    //cin >> hh >>maohao>> mm>>maohao >> ss;
    scanf("%d:%d:%d", &hh, &mm, &ss); 
    hh=hh*3600+mm*60+ss;
    for (; firstindex< (*cars).size()&&(*cars)[firstindex].time<=hh;firstindex++)  
      if ((*cars)[firstindex].in)
         cnt++; 
           else cnt--;   
      printf("%d\n",cnt);
  }
} 
int main()
{  
  int N, K;
  scanf("%d%d", &N, &K); 
  vector<carslist>cars(N);
  readln(&cars, N); 
  sort(cars.begin(), cars.end(), everycarsTimecmp);  
  vector<string>staylongest;
    N=KickWrong_Addlongest(&cars,&staylongest);
  sort(cars.begin(), cars.end(), TotalTimesort);   
  read_ans(K, &cars);
  for (vector<string>::iterator iter = staylongest.begin(); iter != staylongest.end(); iter++)
     printf("%s ", (*iter).c_str());
  printf("%02d:%02d:%02d\n", N / 3600, N % 3600 / 60, N % 60); 
  system("pause");
  return 0;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月19日 15:00答案正确301095C++ (g++ 4.7.2)501012datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确129618/18
1答案正确1110123/3
2答案正确116923/3
3答案正确14121/1
4答案正确509483/3
5答案正确109482/2

#include<iostream> 
#include<string.h>
#include<string>
#include<vector> 
#include<algorithm> 
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector<carslist>*cars, int N)
{
  int hh, mm, ss;
  char in_out[5];
  int index; 
  for (index = 0; index < N; index++)
  { 
    scanf("%s%d:%d:%d%s", (*cars)[index].plate, &hh, &mm, &ss,in_out);  
    //cin >> (*cars)[index].plate >> hh >> in_out[0] >> mm >> in_out[0] >> ss >> in_out; 
    (*cars)[index].time = hh * 3600 + mm * 60 + ss;
    (*cars)[index].in = in_out[0] == 'i' ? true : false; 
  }
}
bool everycarsTimecmp(const carslist&A, const carslist&B)
{
  if (strcmp(A.plate,B.plate)==0)return A.time < B.time;
  else return strcmp(A.plate, B.plate)<0;
}
bool TotalTimesort(const carslist&A, const carslist&B)
{
  return A.time < B.time;
}
int KickWrong_Addlongest(vector<carslist>*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector<carslist>::iterator iter = (*cars).begin(); iter != (*cars).end(); )
  {
    if (iter->in&&iter + 1 != (*cars).end() && strcmp(iter->plate, (iter + 1)->plate)== 0 && !(iter + 1)->in) /*in_out一定成对存在,题目要求*/
    {

      if (Precar == iter->plate) 
          ThecarmaxT += (iter + 1)->time - iter->time;/*累记一整天的停车时长*/ 
      else
      {
        Precar = iter->plate;
        ThecarmaxT = (iter + 1)->time - iter->time; 
      }
      if (maxTime < ThecarmaxT)
      {
        (*staylongest).clear();
        (*staylongest).push_back(Precar);
        maxTime = ThecarmaxT;
      }
      else if (maxTime == ThecarmaxT)
        (*staylongest).push_back(Precar);
      iter += 2;
    }
    else iter = (*cars).erase(iter); 
  }
  return maxTime;
}
void numbercount(vector<int>*number, vector<carslist>*cars)
{
  int index, len = (*cars).size(),cnt;
  for (cnt=index = 0; index < len; index++)
  {
    if ((*cars)[index].in)cnt++; else cnt--;
    (*number).push_back(cnt); 
  }
}
int binaryFind_noMoretime(vector<carslist>*cars, int time,int firstindex)
{
  int lastindex = (*cars).size() - 1;
  if (lastindex == -1 || time<(*cars)[0].time)return -1;
  else if (time>(*cars)[lastindex].time) 
    return lastindex;  
  int midindex;
  while (firstindex <= lastindex)
  {
    midindex = (lastindex + firstindex) / 2;
    if ((*cars)[midindex].time > time)lastindex = midindex - 1;  
    else firstindex = midindex + 1;
  }
  return lastindex;
}
void read_ans(int K, vector<carslist>*cars, vector<int>*number)
{
  int hh, mm, ss,firstindex=0; 
  while (K--)
  {
    //cin >> hh >>maohao>> mm>>maohao >> ss;
    scanf("%d:%d:%d", &hh, &mm, &ss);
    firstindex=binaryFind_noMoretime(cars, hh * 3600 + mm * 60 + ss,firstindex);
    if (firstindex == -1) printf("0\n");
    else  printf("%d\n", (*number)[firstindex]);
  }
} 
int main()
{  
  int N, K;
  scanf("%d%d", &N, &K); 
  vector<carslist>cars(N);
  readln(&cars, N); 
  sort(cars.begin(), cars.end(), everycarsTimecmp);  
  vector<string>staylongest;
    N=KickWrong_Addlongest(&cars,&staylongest);
  sort(cars.begin(), cars.end(), TotalTimesort);  
  vector<int>number;
  numbercount(&number, &cars);
  read_ans(K, &cars, &number);
  for (vector<string>::iterator iter = staylongest.begin(); iter != staylongest.end(); iter++)
     printf("%s ", (*iter).c_str());
  printf("%02d:%02d:%02d\n", N / 3600, N % 3600 / 60, N % 60); 
  system("pause");
  return 0;
}


评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月19日 13:41答案正确301095C++ (g++ 4.7.2)217948datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130418/18
1答案正确418963/3
2答案正确356923/3
3答案正确13081/1
4答案正确2178803/3
5答案正确199482/2
#include<iostream> 
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<iomanip>
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector<carslist>*cars, int N)
{
  int hh, mm, ss;
  char in_out[4];
  int index;
  for (index = 0; index < N; index++)
  {
    cin >> (*cars)[index].plate >> hh >> in_out[0] >> mm >> in_out[0] >> ss >> in_out; 
    (*cars)[index].time = hh * 3600 + mm * 60 + ss;
    (*cars)[index].in = in_out[0] == 'i' ? true : false; 
  }
}
bool everycarsTimecmp(const carslist&A, const carslist&B)
{
  if (strcmp(A.plate, B.plate) == 0)return A.time < B.time;
  else return strcmp(A.plate, B.plate) < 0;
}
bool TotalTimesort(const carslist&A, const carslist&B)
{
  return A.time < B.time;
}
int KickWrong_Addlongest(vector<carslist>*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector<carslist>::iterator iter = (*cars).begin(); iter != (*cars).end(); )
  {
    if (iter->in&&iter + 1 != (*cars).end() && strcmp(iter->plate, (iter + 1)->plate)== 0 && !(iter + 1)->in) /*in_out一定成对存在,题目要求*/
    {

      if (Precar == iter->plate) 
          ThecarmaxT += (iter + 1)->time - iter->time;/*累记一整天的停车时长*/ 
      else
      {
        Precar = iter->plate;
        ThecarmaxT = (iter + 1)->time - iter->time; 
      }
      if (maxTime < ThecarmaxT)
      {
        (*staylongest).clear();
        (*staylongest).push_back(Precar);
        maxTime = ThecarmaxT;
      }
      else if (maxTime == ThecarmaxT)
        (*staylongest).push_back(Precar);
      iter += 2;
    }
    else iter = (*cars).erase(iter); 
  }
  return maxTime;
}
void numbercount(vector<int>*number, vector<carslist>*cars)
{
  int index, len = (*cars).size(),cnt;
  for (cnt=index = 0; index < len; index++)
  {
    if ((*cars)[index].in)cnt++; else cnt--;
    (*number).push_back(cnt); 
  }
}
int binaryFind_noMoretime(vector<carslist>*cars, int time)
{
  int lastindex = (*cars).size() - 1;
  if (lastindex == -1 || time<(*cars)[0].time)return -1;
  else if (time>(*cars)[lastindex].time)return lastindex;  
  int firstindex = 0,midindex;
  while (firstindex <= lastindex)
  {
    midindex = (lastindex + firstindex) / 2;
    if ((*cars)[midindex].time > time)lastindex = midindex - 1;
    else firstindex = midindex + 1;
  }
  return lastindex;
}
void read_ans(int K, vector<carslist>*cars, vector<int>*number)
{
  int hh, mm, ss;
  char maohao;
  while (K--)
  {
    cin >> hh >>maohao>> mm>>maohao >> ss;
    hh=binaryFind_noMoretime(cars, hh * 3600 + mm * 60 + ss);
    if (hh == -1)cout << "0" << endl;
    else cout << (*number)[hh] << endl;
  }
}
void format(int t)
{
  cout << setw(2) << setfill('0') << t;
}
int main()
{  
  int N, K;
  cin >> N >> K;
  vector<carslist>cars(N);
  readln(&cars, N); 
  sort(cars.begin(), cars.end(), everycarsTimecmp);  
  vector<string>staylongest;
    N=KickWrong_Addlongest(&cars,&staylongest);
  sort(cars.begin(), cars.end(), TotalTimesort);  
  vector<int>number;
  numbercount(&number, &cars);
  read_ans(K, &cars, &number);
  for (vector<string>::iterator iter = staylongest.begin(); iter != staylongest.end(); iter++)
    cout << (*iter) << " "; 
  format(N / 3600); 
  N %= 3600;
  cout << ":";
  format(N / 60);
  cout << ":";
  format(N % 60);
  cout << endl;
  system("pause");
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值