题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=772
时间超限的代码:
超限原因:sort函数占时过多
#include <cstdio>
#include <algorithm>
#include <string.h>using namespace std;int n,m;typedef struct
{int num;char time[10];char res[10];int value;
}SS;bool cmp(const SS &x,const SS &y)
{if(x.num==y.num) return strcmp(x.time,y.time)<0;else return x.num<y.num;
}SS a[20];int tran(char *sj)
{int ans=0;int i;int tmp;tmp=0;for(i=0;sj[i]!=':';i++){tmp=tmp*10+sj[i]-'0';//printf("%d ",tmp);}ans=tmp*60;i++;tmp=0;for(;sj[i]!='\0';i++){tmp=tmp*10+sj[i]-'0';// printf(":%d ",tmp);}ans=ans+tmp;return ans;
}void solve()
{int sum=0;int i;int ans=0;int start=0;;int right=0;int flag=-1;sort(a,a+m,cmp);for( i=0;i<m;i++){while(strcmp(a[i].res,"AC")!=0 && i<m) i++;if(i>=m) break;if(a[i].num==flag) continue;// printf("%d %s %s \n",a[i].num,a[i].time,a[i].res);if(i<m){sum++;flag=a[i].num;ans+=tran(a[i].time);right=i;for(;start<right;start++){if(a[start].num==flag)ans+=20;}}}printf("%d %d\n",sum,ans);
}int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(int i=0;i<m;i++){scanf("%d%s%s",&a[i].num,a[i].time,a[i].res);}solve();}return 0;
}
改正思路:
题目所给输入数据已经按一定的规则排了序,可以不必重新排序,直接模拟