1095. Cars on Campus (30)

 

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 答案正确 30 1095 C++ (g++ 4.7.2) 48 948 datrilla

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 296 18/18
1 答案正确 11 948 3/3
2 答案正确 11 564 3/3
3 答案正确 1 384 1/1
4 答案正确 48 820 3/3
5 答案正确 10 820 2/2
#include 
#include
#include
#include 
#include 
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector*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*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector::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*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); 
  vectorcars(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 答案正确 30 1095 C++ (g++ 4.7.2) 50 1012 datrilla

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 296 18/18
1 答案正确 11 1012 3/3
2 答案正确 11 692 3/3
3 答案正确 1 412 1/1
4 答案正确 50 948 3/3
5 答案正确 10 948 2/2

#include 
#include
#include
#include 
#include 
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector*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*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector::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*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*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*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); 
  vectorcars(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 答案正确 30 1095 C++ (g++ 4.7.2) 217 948 datrilla

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 304 18/18
1 答案正确 41 896 3/3
2 答案正确 35 692 3/3
3 答案正确 1 308 1/1
4 答案正确 217 880 3/3
5 答案正确 19 948 2/2
#include 
#include
#include
#include
#include
#include
#include
using namespace std;  
struct carslist
{
  char plate[9];
  int time;
  bool in;
};
void readln(vector*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*cars, vector<string>*staylongest)
{
  int maxTime = 0,ThecarmaxT=0;
  string Precar=(*cars)[0].plate;
  for (vector::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*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*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*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;
  vectorcars(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;
}

你可能感兴趣的:(C++,vector,string,printf,sort,二分法,scanf)