• 【t056】智力问答(链表+计数排序做法)


    Time Limit: 1 second
    Memory Limit: 128 MB

    【问题描述】

    新年联欢会上,G.Sha负责组织智力问答节目。G.Sha建立了一个很大很大的超级题库,并衡量了每道题的难度wi。由于不可以让选手
    一上场就被绝顶难题撂倒,所以难度必须循序渐进,从简到繁。
    G.Sha制定了一套具体的抽题规则。首先,每位选手的第1道题一定是当前题库中最简单的。每位选手的下一道题,一定是题库中(1
    )比刚刚答过的题严格地更难,(2)难度尽可能低的题。当然,每道题使用一遍就从题库中删掉。(可以参考样例数据)
    G.Sha刚开完联欢会很累,所以他提供了抽题程序的日志,希望你能重现答题的过程。
    简单起见,你只需要依次输出每位选手都答了哪些难度的题就可以了。
    【数据规模】
    40%的数据中,m≤50, n≤1000。
    100%的数据中,m≤1000, n≤100000。
    测试数据保证 ,即比赛中使用的题目,不仅比题库少,并且是远远的少。
    测试数据保证每位选手都不会出现无题可选的情况。
    【提示】
    本题数据严格而全面,请注意优化算法,谨防超时。
    【输入格式】

    输入文件 quiz.in 包含3 行。
    第 1 行,整数 n, m。代表题库最初的题目数 n,和选手数 m。
    第 2 行,n个整数 wi,代表题目的难度,无序,0≤wi≤100000。
    第 3 行,m个整数 ai,代表依次每位选手作答的题目数量。

    【输出格式】

    输出文件quiz.out包含n行。
    第i行,每行ai个整数,代表选手i回答的每道题目的难度。
    选手和每位选手的题目均按比赛时间的推移有序。
    说明:游戏开始前的题库是{30, 1, 7, 3, 1, 14, 8, 20, 2, 1, 999, 2}。
    选手1,依次作答了1,2,3,7四道题。作答后题库剩余{30,1,14,8,20,1,999,2}。
    选手2,依次作答了1, 2两道题。作答后题库剩余{30, 14, 8, 20, 1, 999}。
    选手3,依次作答了1, 8, 14三道题。作答后题库剩余{30, 20, 999}。
    选手4,依次作答了20一道题,作答后题库剩余{30, 999}。
    可以注意到,选手1~3都答了难度为1的题。这是因为难度1的题有3道,而对于每位选手来说难度都是递增的,一位选手不可能连续答
    同样难度的题。
    同理,选手1~2都答了难度为2的题。
    Sample Input

    12 4
    30 1 7 3 1 14 8 20 2 1 999 2
    4 2 3 1

    Sample Output

    1 2 3 7
    1 2
    1 8 14
    20

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t056

    【题解】

    可以在读入问题难度的时候直接cnt[nandu]++;
    然后从小到大枚举一下难度;
    对于那些有该难度的题;
    加入链表末尾;
    每次询问答题者回答哪些问题的时候,直接从链表的头节点开始往后遍历一下链表就好;
    (链表还有一个cnt域,表示有几道该难度的题);
    如果cnt域变为0了,就把该节点删掉;所以需要一个前缀指针;

    【完整代码】

    #include <cstdio>
    #include <cmath>
    #include <set>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int MAXM = 1000+100;
    const int MAXNUM = 1e5+10;
    
    struct node
    {
        int num,cnt;
        node *nex;
    };
    
    int n,m;
    int cnt[MAXNUM],total;
    node *h,*p,*prep;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(m);
        rep1(i,1,n)
        {
            int x;
            rei(x);
            cnt[x]++;
        }
        h = new node;
        h->nex = new node;
        p = h->nex;
        p->nex = NULL;
        rep1(i,0,1e5)
        {
            if (cnt[i]>0)
            {
                p->num = i;
                p->cnt = cnt[i];
                p->nex = new node;
                p = p->nex;
                p->nex = NULL;
            }
        }
        rep1(i,1,m)
        {
            prep = h;
            p = h->nex;
            rei(total);
            rep1(j,1,total)
            {
                printf("%d ",p->num);
                p->cnt--;
                if (p->cnt==0)
                {
                    prep->nex = p->nex;
                    p = p->nex;
                }
                else
                {
                    prep = p;
                    p = p->nex;
                }
            }
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    easyui带file上传控件表达提交
    WebApi返回json
    同一个项目中使用MVC控制器和WebAPI控制器
    jquery disabled
    ITIL(Information Technology Infrastructure Library )
    jquery.formatDateTime
    sqlserver 表连接更新字段
    C#分页的总页数算法
    Angular入门教程三
    Angular入门教程二
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626647.html
Copyright © 2020-2023  润新知