复习 排序 | 优先队列

POJ 3664 排序水题

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <set>

using namespace std;
typedef long long ll;
typedef pair<int, int>P;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const ll mod = 998244353;
const double eps = 1e-11;
const double pi = 3.141592653;

int n, k;
struct node
{
    int a, b, id;
}cow[50010];

bool cmp1(const node& x, const node& y)
{
    return x.a > y.a;
}

bool cmp2(const node& x, const node& y)
{
    return x.b > y.b;
}

int main()
{
    cin >> n >> k;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d %d", &cow[i].a, &cow[i].b);
        cow[i].id = i;
    }
    sort(cow + 1, cow + 1 + n, cmp1);
    sort(cow + 1, cow + 1 + k, cmp2);
    cout << cow[1].id << endl;
}

POJ 2442 贪心+优先队列

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <set>

using namespace std;
typedef long long ll;
typedef pair<int, int>P;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const ll mod = 998244353;
const double eps = 1e-11;
const double pi = 3.141592653;

struct node
{
    int a, b, val;
    bool f;

    node(int a, int b, int val, bool f): a(a), b(b), val(val), f(f) {}
    node(){}

    bool operator<(const node& x) const
    {
        return val > x.val;
    }
};

int seq[110][2010];
int tmp[2010];
priority_queue<node> q;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int m, n;
        scanf("%d %d", &m, &n);
        for(int i = 1; i <= m; ++i)
        {
            for(int j = 1; j <= n; ++j)
                scanf("%d", &seq[i][j]);
            sort(seq[i] + 1, seq[i] + n + 1);
        }

        for(int i = 1; i < m; ++i)
        {
            while(!q.empty()) q.pop();
            q.push(node(1, 1, seq[i][1] + seq[i+1][1], true));
            for(int j = 1; j <= n; ++j)
            {
                node cur = q.top(); q.pop();
                if(cur.f)
                {
                    q.push(node(cur.a + 1, cur.b, seq[i][cur.a+1]+seq[i+1][cur.b], true));
                    q.push(node(cur.a, cur.b + 1, seq[i][cur.a]+seq[i+1][cur.b+1], false));
                }
                else q.push(node(cur.a, cur.b + 1, seq[i][cur.a]+seq[i+1][cur.b+1], false));
                tmp[j] = cur.val;
            }
            for(int j = 1; j <= n; ++j) seq[i+1][j] = tmp[j];
        }
        for(int i = 1; i <= n; ++i) cout << seq[m][i] << " \n"[i==n];
    }
}

https://www.luogu.com.cn/blog/ZJsheep/poj2442-sequence-heap
https://www.cnblogs.com/dilthey/p/9804025.html

posted @ 2024-01-04 16:24  .Ivorelectra  阅读(2)  评论(0编辑  收藏  举报