POJ-1862-Stripies

周六有空闲就要好好写博客哦

整理一个题吧

给出n 个物体,分别给出每个的质量,并且两个物体(假设质量分别为m1,m2)相撞的时候变成一个物体,质量为2*sqrt(m1*m2),并且只会出现两个两个物品碰撞的情况,问最终能得到的物体的最小质量是多少。
不难理解,应该让最大的先进行碰撞,因为大的开方次数多了整体结果就小,所以我们只需对所有元素从大到小排列就OK了,但是,值得注意的是,碰撞过程中会产出新物体,那么假设有四个物体要进行三次碰撞,也就会有两次排列,以此类推可以得到n个物体就要有n-2次排列,如果选择用sort排列会非常之繁琐
因此,优先队列在这里就派上用场了(我也是第一次用优先队列,再打一遍规则熟悉一下)。

头文件: #include <queue>
定义:priority_queue <data_type> priority_queue_name;
	如:priority_queue <int> q;//默认是大顶堆
操作:
	q.push(elem) 将元素elem置入优先队列
	q.top() 返回优先队列的下一个元素
	q.pop() 移除一个元素
	q.size() 返回队列中元素的个数
	q.empty() 返回优先队列是否为空

先上代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
struct Node
{
    double weight;
    bool operator<(const Node &a)const
    {
        return weight<a.weight;
    }
};
int n;
priority_queue<Node>q;
int main()
{
    while(cin>>n)
    {
        Node node;
        for(int i=0;i<n;i++)
        {
            double weight;
            cin>>weight;
            node.weight=weight;
            q.push(node);
        }
        for(int i=1;i<n;i++)
        {
            double a=q.top().weight;
            q.pop();
            double b=q.top().weight;
            q.pop();
            node.weight=2*sqrt(a*b);
            q.push(node);//碰撞之后的元素放回队列
        }
       printf("%.3f\n",q.top().weight);
       q.pop();
    }
    return 0;
}

这里又接触一个新的内容啦,运算符重载

声明:以下内容转载于:https://blog.csdn.net/weixin_30800987/article/details/96774335?utm_source=app

1.bool operator<(const node&x)const{}//结构体内嵌比较函数,
重载运算符
一般情况
struct node
 {
     int l,r;
     bool operator <(const node &a)const{
         return r < a.r;
    }
}a[maxn];
直接写比较函数是裸的r表示当前的值,如果r<a.r,那么就是从小到大排序,但是优先队列的是相反的。

sort默认为从小到大排序,优先队列默认为从大到小。
struct node
 {
     int l,r;
     bool operator <(const node &a)const{
         return r >a.r;
    }
};
那么这个队列就是按照r小的优先队列。
以上只是按照一个变量排序的,如果我两个变量都考虑,那怎么排序的呢?

 

  #include<bits/stdc++.h>
  using namespace std;
  struct node{
      int l,r;
  
      bool operator < (const node &x) const {
          if(l<=r&&x.l> x.r) return true;
          if(l> r&&x.l< x.r) return false;
          if(l<=r&&x.l<=x.r) return l<x.l;
         if(l> r&&x.l> x.r) return r>x.r;
         //return false;
     }
 }a[100];
 
 int main()
 {
     int n;
     scanf("%d",&n);
     for(int i=1;i<=n;i++)
         scanf("%d%d",&a[i].l,&a[i].r);
     sort(a+1,a+1+n);
     for(int i=1;i<=n;i++)
         cout<<a[i].l<<" "<<a[i].r<<endl;
 }
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季沐晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值