不是AC代碼,4個樣例只過了第一個,這題太麻煩了,有時間再寫,先寫後邊的,次品代碼先存檔在這。
題意:計算月花費賬單。
每個樣例都會在某個月內。
給出一天24小時的收費標準,單位cents/minute。算出每個用戶的月賬單。
對每個on-line記錄,找時間上最近的off-line記錄匹配。
struct customer{
vector<string> online;
vector<string> offline;
};
struct ans{
string ss;
string ee;
int minuu;
double money;
};
bool cmp(ans a1,ans a2){
string ss1 = a1.ss;
string ss2 = a2.ss;
string ss3 = a1.ee;
string ss4 = a2.ee;
bool bb;
if(strcmp(ss1.c_str(),ss2.c_str())>0) bb = false;
else if(strcmp(ss1.c_str(),ss2.c_str())<0) bb = true;
else if(strcmp(ss1.c_str(),ss2.c_str())==0){
if(strcmp(ss3.c_str(),ss4.c_str())>0) bb = true;
if(strcmp(ss3.c_str(),ss4.c_str())<0) bb = false;
}
return bb;
}
int costs[24];
map<string,customer> cmap;
set<string> good;//利用set排序的特點
int get_month(string time){
int mouth = (time[0]-'0')*10 + (time[1]-'0');
return mouth;
}
//轉換爲分鐘數
int min_ver(string time){
int date = (time[3]-'0')*10 + (time[4]-'0');
int hour = (time[6]-'0')*10 + (time[7]-'0');
int minu = (time[9]-'0')*10 + (time[10]-'0');
return date*24*60 + hour*60 + minu;
}
int get_cost(string s,string e){
int c = 0,daycost=0;
for(int i=0;i<24;i++){
daycost += 60*costs[i];
}
int d1 = (s[3]-'0')*10 + s[4]-'0';
int h1 = (s[6]-'0')*10 + s[7]-'0';
int m1 = (s[9]-'0')*10 + s[10]-'0';
int d2 = (e[3]-'0')*10 + e[4]-'0';
int h2 = (e[6]-'0')*10 + e[7]-'0';
int m2 = (e[9]-'0')*10 + e[10]-'0';
if(d1==d2){
c = (60-m1)*costs[h1] + m2*costs[h2];
for(int i=h1+1;i<h2;i++)
c += costs[i]*60;
}else{
for(int i=h1+1;i<24;i++){
c += 60*costs[i];
}
c += costs[h1]*(60-m1);
for(int i=0;i<h2;i++){
c += 60*costs[i];
}
c += costs[h2]*m2;
c += daycost*(d2-d1-1);
}
return c;
}
void process(string name){
int month = get_month(cmap[name].online[0]);
double cnt = 0;
printf("%s %02d\n",name.c_str(),month);
vector<ans> anss;
while(cmap[name].online.size() && cmap[name].offline.size()){
//當online和offline還有能配對的記錄時,就給他們配對
//二重循環進行配對
int minn = INT_MAX,temp,flag=0,i,j,si,ej;
string startt,endd;
for(i=0;i<cmap[name].online.size();i++)
{
for(j=0;j<cmap[name].offline.size();j++)
{
temp = min_ver(cmap[name].offline[j]) - min_ver(cmap[name].online[i]);
if(temp>0 && temp<minn){
flag = 1;
minn = temp;
startt = cmap[name].online[i];
endd = cmap[name].offline[j];
si = i;
ej = j;
}
}
}
if(flag){
//去掉計算過的記錄
cmap[name].online.erase(cmap[name].online.begin()+si);
cmap[name].offline.erase(cmap[name].offline.begin()+ej);
double costt = get_cost(startt,endd)/100.0;
startt = startt.substr(3);//去掉日期
endd = endd.substr(3);
//printf("%s %s %d $%.02f\n",startt.c_str(),endd.c_str(),minn,costt);
ans a;
a.ss = startt;
a.ee = endd;
a.minuu = minn;
a.money = costt;
anss.push_back(a);
cnt += costt;
}else break;
}
//將通話記錄按時間排序
sort(anss.begin(),anss.end(),cmp);
for(int i=0;i<anss.size();i++)
printf("%s %s %d $%.02f\n",anss[i].ss.c_str(),anss[i].ee.c_str(),anss[i].minuu,anss[i].money);
printf("Total amount: $%.02lf\n",cnt);
}
int main()
{
int n;
for(int i=0;i<24;i++) scanf("%d",costs+i);
scanf("%d",&n);
getchar();//需要喫掉回車
while(n--){
string str;
getline(cin,str);
string as,bs,cs;
stringstream ss;
ss<<str;
ss>>as;
ss>>bs;
ss>>cs;
good.insert(as);//借用set自動排序
if(!cs.compare("on-line")) cmap[as].online.push_back(bs);
if(!cs.compare("off-line")) cmap[as].offline.push_back(bs);
}
for(set<string>::iterator it=good.begin();it!=good.end();++it)
process(*it);
return 0;
}