• PAT (Advanced Level) 1039. Course List for Student (25)


    map会超时,二分吧...

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdio>
    #include<map>
    #include<queue>
    #include<string>
    #include<vector>
    using namespace std;
    
    int n,m;
    int tot;
    
    vector<int>v[50000];
    struct X
    {
        int id;
        int num;
        char name[210][6];
    }s[2600];
    
    struct Name
    {
        char name[6];
    }op[40010];
    
    bool cmp2(const Name&a,const Name&b)
    {
        return strcmp(a.name,b.name)<0;
    }
    
    bool cmp(const X&a,const X&b)
    {
        return a.id<b.id;
    }
    
    char quary[40010][6];
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&s[i].id);
            scanf("%d",&s[i].num);
            for(int j=1;j<=s[i].num;j++) scanf("%s",s[i].name[j]);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",op[i].name);
            strcpy(quary[i],op[i].name);
        }
        sort(op+1,op+1+n,cmp2);
    
        sort(s+1,s+1+m,cmp);
    
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=s[i].num;j++)
            {
                int h=0;
                int l=1,r=n;
                while(l<=r)
                {
                    int mid=(l+r)/2;
                    if(strcmp(op[mid].name,s[i].name[j])<0) l=mid+1;
                    else if(strcmp(op[mid].name,s[i].name[j])>0) r=mid-1;
                    else
                    {
                        h=mid;
                        break;
                    }
                }
                if(h==0) continue;
                v[h].push_back(s[i].id);
            }
        }
    
        for(int i=1;i<=n;i++)
        {
            int q=0;
            int l=1,r=n;
            while(l<=r)
            {
                int mid=(l+r)/2;
                if(strcmp(op[mid].name,quary[i])<0) l=mid+1;
                else if(strcmp(op[mid].name,quary[i])>0) r=mid-1;
                else
                {
                    q=mid;
                    break;
                }
            }
            if(q==0) continue;
    
            printf("%s ",quary[i]);
            printf("%d",v[q].size());
            for(int j=0;j<v[q].size();j++) printf(" %d",v[q][j]);
            printf("
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    获取CheckBox的Text值
    动态绑定数据至Html Table
    二次事件并细化功能
    ASP.NET MVC的JavaScriptResult
    Google Earth 8.0
    ASP.NET MVC的ContentResult
    ASP.NET MVC使用input标签上传文件
    处理动态SQL语句的参数
    Infor SyteLine如何快速锁定用户
    执行git push出现"Everything up-to-date"
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5521808.html
Copyright © 2020-2023  润新知