Codeforces Round #312 (Div. 2)-A. Lala Land and Apple Trees-水题-模拟

坐标轴上除了0点 都种了苹果树,从零点开始,往左或往右,遇到新数则收割,然后转头走,,直到不能遇到新树就停止。。

显然只有两个选择,不是先左走就是先右走。。。。都来一遍就OK了。。

预先把 X>0的数全都放在一个优先队列里,队列中 x越小越优先

把x<0的数都放在另一个优先队列里,x越大越优先。。。


那么对 先左走就每次

x<0队列取一个,

x>0队列取一个

如果哪一个队列为空,就停止。

来两遍,输出小的那个ans就ok,

wa了一次。。手误。。。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define INF 0x7f7f7f7f
const __int64 maxn = 200005; 
 struct node
 {
     int x;
     int num; 

     node (){}
     node (int i,int h){x=i;num=h;}
 
 };
 struct cmp1
 {
         bool operator () (const node &a,const node &b) const
     {
         return a.x>b.x;  //小优先
        }  
 };
  struct cmp2
 {
         bool operator () (const node &a,const node &b) const
     {
         return a.x<b.x;
        } 
 };



 priority_queue <node,vector<node>,cmp2>  q1;
 priority_queue <node,vector<node>,cmp1>  q2;

int main()
{  
    int n,i;
     scanf("%d",&n);
for (i=1;i<=n;i++)
{
    int a;int b;
    scanf("%d%d",&a,&b);
    if (a<0) 
        q1.push(node (a,b));
    else
        q2.push(node (a,b));
}
 
int ans=0; 
while (1)
{
    if (!q2.empty())
    {
        ans+=q2.top().num;
        q2.pop();
    }
    else
    break;
        if (!q1.empty())
    {
        ans+=q1.top().num;
        q1.pop();
    }
    else
    break;

}

int ans2=0; 
while (1)
{
        if (!q1.empty())
    {
        ans+=q1.top().num;
        q1.pop();
    }
    else
    break;
    if (!q2.empty())
    {
        ans+=q2.top().num;
        q2.pop();
    }
    else
    break;

}
printf("%d\n",ans<ans2?ans2:ans);

 





  
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值