思路:on off 分两拨 用vector存储 on从大到小 off从小到大
map 将人名和 on 的时间连接起来 人名和off的时间连接起来
然后 两个map开始匹配
最初的想法是off在外面 on在内 若人名不匹配 说明小的那个肯定没有对应的,erase 但是会有运行时错误 所以 反正也匹配不了。。就留着了,
在时间对应的关系上,默认是可以直接匹配的。
但是 只有第一个过了 (15) 其余三个答案错误,暂时没找出来,待解决
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std;
struct P {
int mon;
int d;
int h;
int m;
bool flag;
P() {
flag = false;
}
friend ostream& operator << (ostream &Out, const P &t) {
Out << "mon:" << t.mon << " " << "d:" << t.d<< " " << "h:" << t.h<< " " << "m:" << t.m;
return Out;
}
};
int mh[25];
bool cmpa(P a, P b) {
if(a.mon == b.mon) {
if(a.d == b.d) {
if(a.h == b.h) {
return a.m > b.m;
}
return a.h > b.h;
}
return a.d > b.d;
}
return a.mon > b.mon;
}
bool cmpi(P a, P b) {
if(a.mon == b.mon) {
if(a.d == b.d) {
if(a.h == b.h) {
return a.m < b.m;
}
return a.h < b.h;
}
return a.d < b.d;
}
return a.mon < b.mon;
}
bool mm[13];
int main() {
vector<P> vn;
vector<P> vf;
map<string, vector<P> > an;
map<string, vector<P> > af;
mh[24] = 0;
for(int i=0; i<24; i++) {
cin >> mh[i];
mh[24] += mh[i];
}
int n;
cin >> n;
P p;
string na, j;
getchar();
for(int i=0; i<n; i++) {
stringstream ss;
getline(cin, j);
ss << j;
string tmp;
ss >> na;
ss >> tmp;
ss >> j;
sscanf(tmp.c_str(), "%d:%d:%d:%d", &p.mon, &p.d, &p.h, &p.m);
//if(j == "on-line")
string on = "on-line";
if(!strcmp(j.c_str(), on.c_str())) {
an[na].push_back(p);
} else {
af[na].push_back(p);
}
}
for(map<string, vector<P> >::iterator it=an.begin(); it!=an.end(); it++) {
// cout << it->first << endl;
vf = it->second;
if(vf.size() > 0)
sort(it->second.begin(), it->second.end(), cmpa);
/*
for(int i=0; i<vf.size(); i++) {
cout << vf[i] << endl;
}
*/
}//大->小
for(map<string, vector<P> >::iterator it=af.begin(); it!=af.end(); it++) {
// cout << it->first << endl;
vf = it->second;
if(vf.size() > 0) sort(it->second.begin(), it->second.end(), cmpi);
/*
for(int i=0; i<vf.size(); i++) {
cout << vf[i] << endl;
}*/
}//小->大
map<string, vector<P> >::iterator tmp;
for(map<string, vector<P> >::iterator it=af.begin(); it!=af.end(); it++) {
if(an.size()==0 || af.size() == 0) break;
for(map<string, vector<P> >::iterator it1=an.begin(); it1!=an.end();) {
if(an.size() == 0) break;
if(it->first != it1->first) { //没有对应的
if(it->first < it1->first) { //没有对应的
it++;
continue;
} else {
/* tmp = it1;
an.erase(it1);//因为每次都会在on-line从头开始匹配 erase是为了不在看这个 但是会运行时错误。。
it1 = tmp;*/
it1++;
continue;
}
} else {
// cout << "可以找到对应的的" << endl;
for(int i=0; i<=12; i++) {
mm[i] = false;
}
vf = it->second;
vn = it1->second;
it1++;
int total = 0;
// for(vector<P>::iterator it3 = vf.begin(); it!)
for(int i=0; i<vf.size(); i++) {
P tmpf = vf[i];
if(tmpf.flag) {
continue;
}
// cout << i << vf[i] << endl;
for(int j=0; j<vn.size(); j++) {
P tmpo = vn[j];
if(tmpo.flag || tmpo.mon != tmpf.mon) {
// cout << "mon" << endl;
continue;
}
if(tmpo.d > tmpf.d) {
// cout << "d" << endl;
continue;
} //可以正常计算
vf[i].flag = true;
vn[j].flag = true;
float s = (tmpf.d-tmpo.d)*mh[24]*60;
// cout <<mh[24] << "day:" << s << endl;
for(int i=tmpo.h; i<tmpf.h; i++) {
if(tmpo.m == 0 || i!= tmpo.h)
s += mh[i]*60;
else s += mh[i]*(60-tmpo.m);
}
s+= mh[tmpf.h]*tmpf.m;
if(s == 0)continue;
if(!mm[tmpo.mon]) {
mm[tmpo.mon] = true;
string tt = it->first;
cout << tt;
printf(" %02d\n", tmpf.mon);
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", tmpo.d, tmpo.h, tmpo.m, tmpf.d, tmpf.h, tmpf.m, ((tmpf.d-tmpo.d)*24+tmpf.h-tmpo.h)*60+tmpf.m-tmpo.m, (float)s/100);
total += s;
break;
}
}
if(total)
printf("Total amount: $%.2f\n", (float)total/100);
}
}
}
return 0;
}
/*
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:00:07:00 off-line
CYLL 01:01:06:01 off-line
CYJJ 01:01:07:00 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line
*/