1006 Sign In and Sign Out (25分)

解法一:排序

struct Stu {
    string id;
    string start_time;
    string end_time;
};
vector<Stu> v;
int n;

bool cmp1(Stu &a,Stu &b)
{
    return a.start_time<b.start_time;
}

bool cmp2(Stu &a,Stu &b)
{
    return a.end_time>b.end_time;
}

int main()
{
    cin>>n;

    for(int i=0;i<n;i++)
    {
        string a,b,c;
        cin>>a>>b>>c;
        v.pb({a,b,c});
    }

    sort(v.begin(),v.end(),cmp1);
    cout<<v[0].id;

    sort(v.begin(),v.end(),cmp2);
    cout<<' '<<v[0].id<<endl;

    //system("pause");
    return 0;
}

解法二:线性扫描即可,string是按字典序大小比较的

int n;

int main()
{
    cin>>n;

    string open_id,open_time;
    string close_id,close_time;
    for(int i=0;i<n;i++)
    {
        string id,come_time,leave_time;
        cin>>id>>come_time>>leave_time;

        if(!i || come_time < open_time)
        {
            open_time = come_time;
            open_id = id;
        }

        if(!i || leave_time > close_time)
        {
            close_time = leave_time;
            close_id = id;
        }
    }

    cout<<open_id<<' '<<close_id<<endl;

    //system("pause");
    return 0;
}

解法三;常规做法。

struct Node
{
    char id[N];
    int hh,mm,ss;
    bool operator<(const Node &W) const
    {
        if(hh == W.hh)
        {
            if(mm == W.mm) return ss<W.ss;
            else return mm<W.mm;
        }
        else return hh<W.hh;
    }
}cur,earliest,latest;
int n;

void init()
{
    earliest.hh=23,earliest.mm=59,earliest.ss=59;
    latest.hh=0,latest.mm=0,latest.ss=0;
}

int main()
{
    init();

    cin>>n;

    for(int i=0;i<n;i++)
    {
        scanf("%s %d:%d:%d",cur.id,&cur.hh,&cur.mm,&cur.ss);
        if(cur < earliest) earliest=cur;

        scanf("%d:%d:%d",&cur.hh,&cur.mm,&cur.ss);
        if(latest < cur)  latest=cur;
    }

    cout<<earliest.id<<' '<<latest.id<<endl;
    //system("pause");
    return 0;
}
posted @ 2021-01-02 15:49  Dazzling!  阅读(58)  评论(0编辑  收藏  举报