九度oj 题目1178:复数集合

来源:互联网 发布:淘宝打假人查询 编辑:程序博客网 时间:2024/06/08 10:41

链接

http://ac.jobdu.com/problem.php?pid=1178


题目描述:

    一个复数(x+iy)集合,两种操作作用在该集合上:

    1、Pop 表示读出集合中复数模值最大的那个复数,如集合为空 输出  empty  ,不为空就输出最大的那个复数并且从集合中删除那个复数,再输出集合的大小SIZE;

    2 Insert a+ib  指令(a,b表示实部和虚部),将a+ib加入到集合中 ,输出集合的大小SIZE;

    最开始要读入一个int n,表示接下来的n行每一行都是一条命令。

输入:

输入有多组数据。
每组输入一个n(1<=n<=1000),然后再输入n条指令。

输出:

根据指令输出结果。

样例输入:
3PopInsert 1+i2Pop
样例输出:
emptySIZE = 11+i2SIZE = 0
提示:

模相等的输出b较小的复数。

a和b都是非负数。

分析

用优先队列,特别要注意的一点是当完成一组操作后,要将优先队列中的所有元素弹出,再开始下一组操作!!!


ac代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 2<<20
#define BASE 1000
using namespace std;
struct node
{
    int a;
    int b;
};
struct cmp
{
 bool operator()(const node &a,const node &b)
 {
  if(a.a*a.a+a.b*a.b!=b.a*b.a+b.b*b.b)
        return a.a*a.a+a.b*a.b<b.a*b.a+b.b*b.b;
    else
        return a.b>b.b;
 }
};
int main ()
{
    priority_queue<node,vector<node>,cmp >q;
    int n;
    string s1;
    while(cin >> n)
    {
        for(int i=0;i<n;i++)
        {
            cin >> s1;
            if(s1[0]=='P')
            {
                if(q.empty())
                    printf("empty\n");
                else
                {
                    printf("%d+i%d\n",q.top().a,q.top().b);
                    q.pop();
                    printf("SIZE = %d\n",q.size());
                }
            }
            else if(s1[0]=='I')
            {
                int a,b;
                scanf("%d+i%d",&a,&b);
                node temp;
                temp.a = a;
                temp.b = b;
                q.push(temp);
                printf("SIZE = %d\n",q.size());
            }
        }
        while(!q.empty())
            q.pop();
    }
    return 0;
}

0 0
原创粉丝点击