hdu 4546

来源:互联网 发布:阿里政务云 编辑:程序博客网 时间:2024/06/08 13:05

优先队列的应用, 求全排列的第m大。

参考  点击打开链接

注:自定义cmp的operator需返回值 类似

return x.now+c[x.pos]>y.now+c[y.pos];

#include<stdio.h>#include<string.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;#define N 10005typedef __int64 LL;struct node{    LL now;    int pos;}a;LL c[N];struct cmp  {      bool operator()(const node &x, const node &y)      {          return x.now+c[x.pos]>y.now+c[y.pos];      }  };priority_queue < node, vector<node>, cmp > q;    //最小值优先  int main(){    int n, m, tot, ca = 1;    for(scanf("%d", &tot); tot--; )    {        scanf("%d%d", &n, &m);        for(int i = 0; i < n; i++)        {            scanf("%I64d", &c[i]);        }        sort(c, c + n);        while(!q.empty())            q.pop();        a.now = 0, a.pos = 0;        q.push(a);        int ans = 0;        while(m--)        {            if(q.empty())                break;            node tep = q.top();            ans = tep.now + c[tep.pos++];            q.pop();            if(tep.pos < n)            {                q.push(tep);                tep.now = ans;                q.push(tep);            }            //printf("ans = %d\n", ans);        }        printf("Case #%d: %d\n", ca++, ans);    }    return 0;}


0 0