HDU5127 Dogs' Candies 暴力+list

本文介绍了一种基于简单数据结构实现的糖果查询算法。该算法通过一系列操作(如添加、删除特定属性的糖果及查询最佳糖果)来帮助狗王管理其糖果箱,并确保能够快速找到对于特定口味偏好最美味的糖果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Dogs' Candies

Time Limit: 30000/30000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1478    Accepted Submission(s): 351


Problem Description
Far far away, there live a lot of dogs in the forest. Unlike other dogs, those dogs love candies much more than bones.

Every candy has two attributes: the sweetness degree p and the sourness degree q. Different dogs like different candies. A dog also has two attributes: the fondness degree for sweetness x and the fondness degree for sourness y. So the deliciousness degree of a candy for a dog is defined as p×x + q×y.

The dog king has a huge candy box. At first, the box is empty. The king can add candies to the box or take some candies from the box and eat them. There are always some dogs who want to know which candies in the box are the most delicious for them. Please help the king to answer their questions.
 

Input
The input consists of at most 10 test cases. For each test case, the first line contains an integer n indicating that there are n candy box operations(1 <= n <= 50000).

The following n lines describe the n operations.

Each operation contains three integers t, x and y( 0 <= |x|, |y| <= 109). The first integer t may be -1, 0, or 1.

If t equals -1, it means that a candy in the box with sweetness degree x and sourness degree y is eaten by the dog king.

If t equals 1, it means that a candy with sweetness degree x and sourness degree y is added to the candy box.

If t equals 0, it means that a dog with sweetness fondness degree x and sourness fondness degree y wants to know the maximal deliciousness degree of the candies in the box for him.

It is guaranteed that every candy is unique in the box.

The input ends by n = 0.
 

Output
For each operation in which t equals to 0, you should print the maximal deliciousness degree of the best candy for the dog.
 

Sample Input
6 1 2 1 1 1 2 1 1 1 0 2 1 -1 2 1 0 2 1 0
 

Sample Output
5 4
题意:每个物品有两种属性,给出n个操作,t==1:将属性为x,y的物品放进一个容器。t==-1:从容器中删除一个属性为x,y的物品。t==0:在容器中查询最大的p*x+q*y(p,q为输入值)。
题解:时限很大,尝试了暴力,可解。list+结构体当作容器,对于每添加一个物品。要添加在头,添加在尾会超时(不知道为什么)。删除和查找操作就是暴力枚举,(在查询时不可排序,因为排序o(nlogn),亲测过不去)。
代码:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <list>
#include <utility>
#define ll long long
using namespace std;
struct node{ll x,y;}T;
//typedef pair<long long,long long> T,Y;
list<node> L;
list<node>::iterator it;
/*struct cmp
{
    bool operator()(node xx,node yy)
    {
        return xx.x>yy.x||xx.x==yy.x&&xx.y>yy.y;
    }
};
struct cmp1
{
     bool operator()(node xx,node yy)
    {
        return xx.y>yy.y||xx.y==yy.y&&xx.x>yy.x;
    }

};*/
int t,x;
ll y,z;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        L.clear();
        if(t==0) break;
        while(t--)
        {
            scanf("%d%I64d%I64d",&x,&y,&z);
            if(x==1)
            {
                L.push_front((node){y,z});
            }
            else if(x==-1)
            {
                for ( it = L.begin(); it != L.end(); it++)
                {
                    T=*it;
                    if(T.x==y&&T.y==z)
                    {
                        L.erase(it);
                        break;
                    }
                }
            }
            else
            {
                ll ans=-0x7f7f7f7f;
                /*if(y>z)
                {
                    L.sort(cmp());
                    it=L.begin();
                    T=*it;
                    ans=T.x*y+T.y*z;
                }
                else
                {
                    L.sort(cmp1());
                    it=L.begin();
                    T=*it;
                    ans=T.x*y+T.y*z;

                }*/
              for ( it = L.begin(); it != L.end(); it++)
                {
                    T=*it;
                    ans=max(ans,T.x*y+T.y*z);
                }
                printf("%I64d\n",ans);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值