码迷,mamicode.com
首页 > 其他好文 > 详细

计蒜客——踩蚂蚁

时间:2017-04-18 01:00:11      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:数组   power   蚂蚁   ase   img   过程   queue   pac   size   

技术分享

技术分享

 

首先将蚂蚁按照血量从大到小排序,并把鞋子(可以使用结构体或pair来保存鞋子的伤害值和费用)按照伤害值从高到低排序。

逐个计算每只蚂蚁需要用哪双鞋子来踩,对于当前蚂蚁的血量 ant,把所有伤害值不小于 ant 的鞋子放入优先队列中。

在这个优先队列中,费用低的鞋子优先级越高。

在每次将鞋子放入优先队列后,让优先队列的队首元素(费用最低的鞋子)出队并累积总费用。

不断循环,直到算出每只蚂蚁对应的鞋子,或者发现没有可用的鞋子时直接输出No

 

看了提示之后还是写了半天,基础太烂了。。渣渣来讲一下做这题的过程。

贪心策略不多说,挺好理解的。就是对重载不太知道怎么写。用个结构体定义鞋子。

然后重载运算符,这样才可以使用元素类型为shoe的优先队列。

 

struct shoe
{
    int price;
    int power;
    //定义在优先队列中的优先级 价格低的优先级高
    bool operator < (const shoe & a) const
    {
        return price > a.price;
    }
} shoes[100005];

 

在网上看到另外一个方法,感觉挺有用,但是代码比较长,就换成了上面的。同时使用下面的方法定义优先级时

声明优先队列的方式要变化  priority_queue<shoe,vector<shoe>,Cmp> q;

//定义在优先队列中的优先级的另一种方式 价格低的优先级高
//priority_queue<shoe,vector<shoe>,Cmp> q;
struct Cmp
{
    bool operator() (shoe s1, shoe s2)
    {
        if(s1.price==s2.price) return s1.power>s2.power;
        return s1.price > s2.price;
    }
};

然后就开始暴露我的智商了。先贴一波第五组数据超时的代码

#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
using namespace std;
int n,m;
int ants[100005];
int vis[100005];
struct shoe
{
    int index;
    int price;
    int power;
    //定义在优先队列中的优先级 价格低的优先级高
    bool operator < (const shoe & a) const
    {
        return price > a.price;
    }
} shoes[100005];

//定义在优先队列中的优先级的另一种方式 价格低的优先级高
//priority_queue<shoe,vector<shoe>,Cmp> q;
struct Cmp
{
    bool operator() (shoe s1, shoe s2)
    {
        if(s1.price==s2.price) return s1.power>s2.power;
        return s1.price > s2.price;
    }
};

bool cmp(int x,int y)
{
    return x>y;
}

bool cmpshoes(shoe &s1,shoe &s2)
{
    return s1.power>s2.power;
}

int main ()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%d",&ants[i]);
    for(int i=0; i<m; i++)
    {
        scanf("%d",&shoes[i].power);
        shoes[i].index=i;
    }
    for(int i=0; i<m; i++)
        scanf("%d",&shoes[i].price);
    sort(ants,ants+n,cmp);
    sort(shoes,shoes+m,cmpshoes);

    long long ans=0;
    int flag=1;
    for(int i=0; i<n; i++)
    {
        //priority_queue<shoe,vector<shoe>,Cmp> q;
        priority_queue<shoe> q;
        int j=0;
        while(j<m&&shoes[j].power>=ants[i])
        {
            if(!vis[shoes[j].index])
                q.push(shoes[j]);
            j++;
        }
        //while(vis[q.top().index]&&!q.empty()) q.pop();

        if(q.empty())
        {
            flag=0;
            break;
        }
        //cout<<"top:"<<q.top().index<<" "<<q.top().power<<" "<<q.top().price<<endl;
        ans+=q.top().price;
        vis[q.top().index]=1;
    }
    if(flag) printf("%lld\n",ans);
    else printf("No\n");
    return 0;
}

在此之前第四组数据都超时。。因为我把每双鞋子都往里塞,再把用过的慢慢弹...

然后想了半天要怎么优化才能过最后一组数据。然后突然想到

优先队列应该开在外部,不应该开在内部,因为蚂蚁的血量是从大到小排序的,
下一只蚂蚁的血量一定能用能踩死当前蚂蚁的鞋子踩死,只要加入新的能踩死的鞋子就好。

然后用掉的鞋子弹出队列,也不需要vis数组来标记了。好吧 我是zz。。

#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
using namespace std;
int n,m;
int ants[100005];
int vis[100005];
struct shoe
{
    int price;
    int power;
    //定义在优先队列中的优先级 价格低的优先级高
    bool operator < (const shoe & a) const
    {
        return price > a.price;
    }
} shoes[100005];

//定义在优先队列中的优先级的另一种方式 价格低的优先级高
//priority_queue<shoe,vector<shoe>,Cmp> q;
struct Cmp
{
    bool operator() (shoe s1, shoe s2)
    {
        if(s1.price==s2.price) return s1.power>s2.power;
        return s1.price > s2.price;
    }
};

bool cmp(int x,int y)
{
    return x>y;
}

bool cmpshoes(shoe &s1,shoe &s2)
{
    return s1.power>s2.power;
}

int main ()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%d",&ants[i]);
    for(int i=0; i<m; i++)
    {
        scanf("%d",&shoes[i].power);
    }
    for(int i=0; i<m; i++)
        scanf("%d",&shoes[i].price);
    sort(ants,ants+n,cmp);
    sort(shoes,shoes+m,cmpshoes);

    long long ans=0;
    int flag=1;

    //优先队列开在外部 不应该开在内部 因为蚂蚁的血量是从大到小排序的
    //下一只蚂蚁的血量一定能用能踩死当前蚂蚁的鞋子踩死
    //priority_queue<shoe,vector<shoe>,Cmp> q;
    priority_queue<shoe> q;
    int j=0;
    for(int i=0; i<n; i++)
    {
        while(j<m&&shoes[j].power>=ants[i])
        {
            q.push(shoes[j]);
            j++;
        }
        if(q.empty())
        {
            flag=0;
            break;
        }
        ans+=q.top().price;
        q.pop();
    }
    if(flag) printf("%lld\n",ans);
    else printf("No\n");
    return 0;
}

 

计蒜客——踩蚂蚁

标签:数组   power   蚂蚁   ase   img   过程   queue   pac   size   

原文地址:http://www.cnblogs.com/wangkaipeng/p/6725673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!