UVA_11039 Building Designing

题目请点我
题解:
这道题不难,简单的模拟吧。但是收获是定义优先队列的优先级。同学还有一种思路是只放入一个数组中,重载sort比较函数后输出,也很棒。
代码实现:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;


方法一:放入结构体,重载运算符
//struct point{
//    int x;
//    bool operator <( const point* a){
//        return this->x > a->x;
//    }
//};
//


方法二:定义优先队列时指定好
//priority_queue< int, vector<int>, greater<int> > Q1;
//priority_queue< int, vector<int>, greater<int> > Q2;



int T;
int Num;
int result;
bool flag1,flag2;
priority_queue< int, vector<int>, greater<int> > Q1;
priority_queue< int, vector<int>, greater<int> > Q2;
int main()
{
    scanf("%d",&T);
    while( T-- ){
        while( !Q1.empty() ){
            Q1.pop();
        }
        while( !Q2.empty() ){
            Q2.pop();
        }
        scanf("%d",&Num);
        int tmp;
        int turn;
        int fir1,fir2;
        int result = 0;
        int tag;
        flag1 = flag2 = true;
        for( int i = 0; i < Num; i++ ){
            scanf("%d",&tmp);
            if( tmp > 0 ){
                Q1.push(tmp);
            }
            else{
                Q2.push(-tmp);
            }
        }
        fir1 = Q1.top();
        //Q1.pop();Q2.pop();
        fir2 = Q2.top();
        if( fir1 > fir2 ){
            turn = 2;
        }
        else{
            turn = 1;
        }
        while( 1 ){
            if( turn == 1 ){
                if( Q1.empty() ){
                    break;
                }
                result++;
                tag = Q1.top();
                turn = 2;
                Q1.pop();
            }
            else{
                if( Q2.empty() ){
                    break;
                }
                result++;
                turn = 1;
                tag = Q2.top();
                Q2.pop();
            }
            fir1 = Q1.top();
            fir2 = Q2.top();
            while( turn == 1 && fir1 < tag && !Q1.empty() ){
                Q1.pop();
                fir1 = Q1.top();
            }
            while( turn == 2 && tag > fir2 && !Q2.empty() ){
                Q2.pop();
                fir2 = Q2.top();
            }
        }
        printf("%d\n",result);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值