UVA 540 and UVA 136 队列及优先队列

本文详细解析了UVA 540团体队列问题的解决思路及代码实现,并介绍了使用map和queue数据结构进行高效操作的方法。同时分享了UVA136丑数问题的解题思路,利用优先队列和set避免重复元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目连接:UVA 136
UVA 540

UVA 540 团体队列:
题目不难理解;但是在操作的过程中需要注意一些问题, 如:
1.关于如何按照team插入元素,怎么找到他原来的队伍,然后插进去,还有一个更棘手的问题就是,queue没法随便插入,他是一个顺序结构的东西,所以我觉得刘老师的做法真的太赞了,我在总队里给team排队,然后你们小队再自己玩去吧,注意::总队里排的是team号!!!
2.还有就是他输入不止一组数据,所以要清空所用的东西,map可以用clear这好办,但是queue没有啊,而且,queue还定义了队列数组,那你清空可就来费事了,所以在合适的地方建立这些容器,这是一个不错的方法。

代码:

//map 和 queue 用完都要clear一下,但是由于queue没有clear()这个功能,所以,要在合适的地方建queue
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<queue>
using namespace std;
const int maxn = 1000 + 10;
int main()
{
    int n;
    int x = 0;
    while(cin >> n && n != 0)
    {
        cout << "Scenario #" << x + 1 << endl;
        map<string, int> team;
        for(int i = 0; i < n; i++)
        {
            int m;
            cin >> m;
            while(m--)
            {
                string str;
                cin >> str;
                team[str] = i;
            }
        }// 人员全部输入进来了
    queue<int> q;
    queue<string> q2[maxn];
        string s;
        while(cin >> s)
        {
            if(s == "STOP") break;
            else if(s == "DEQUEUE")
            {
                int t = q.front();
                cout << q2[t].front() <<endl;;
                q2[t].pop();
                if(q2[t].empty()) q.pop();
            }
            else if(s == "ENQUEUE")
            {
                string ss;
                cin >> ss;
                int t = team[ss];
                if(q2[t].empty()) q.push(t);
                q2[t].push(ss);
            }
        }
        x++;
        printf("\n");
        team.clear();
    }
    return 0;
}

UVA 136 丑数

这道题就更不难理解了,其实主要就是联系一下优先队列,但是这个对于自定义的优先队列好像也没练到,其中有一点我感觉比较好的就是刘老师为了防止重复,用了一个set来做监视器,这个想法很妙啊。

代码:

//用set来巧妙的判断了原来queue中是不是存在过这个元素。
#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;


int main()
{
    priority_queue<long long int, vector<long long int>, greater<long long int> > pq;
    set<long long int> num;
        num.insert(1);
        pq.push(1);
    for(int i = 1; ; i++)
    {
        long long int x0 = pq.top(); // 取首元素要用top
        pq.pop();
        if(i == 1500)
            {cout << "The 1500'th ugly number is " << x0 << "." << endl;
            break;}
        else
        {
            long long int x1, x2, x3;
            x1 = x0 * 2; x2 = x0 * 3; x3 = x0 * 5;
            if(!num.count(x1)) {num.insert(x1); pq.push(x1);}
            if(!num.count(x2)) {num.insert(x2); pq.push(x2);}
            if(!num.count(x3)) {num.insert(x3); pq.push(x3);}
        }
    }
    return 0;
}

关于自定义类型Point类的queue:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
class Point
{
    public:
        int x_, y_;
        Point(int x, int y): x_(x), y_(y) {}
        int x()
        {
            return x_;
        }
        int y()
        {
            return y_;
        }
};
        void show(Point p)
        {
            cout << "( " << p.x_ << ", " << p.y_ << " )" << endl;
        }
struct cmp {
   bool operator () (Point p1, Point p2)
   {
        if(p1.x_ < p2.x_) return true;
        else if(p1.x_ == p2.x_)
        {
            if(p1.y_ < p1.y_) return true;
            else return false;
        }
        else return false;
   }
};
int main()
{
    priority_queue<int, vector<Point>, cmp> pq;
    int x, y;
    while(1)
    {
        int x; int y;
        cin >> x >> y;
        if(x == 0 && y == 0) break;
        Point point(x, y);
        pq.push(point);
    }
    while(!pq.empty())
    {
        show(pq.top());
        pq.pop();
    }
    return 0;
}


//pq.top() 取的是优先级最高的元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值