Dogs' Candies
Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 3381 Accepted Submission(s): 827
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.
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.
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
61 2 11 1 21 1 10 2 1-1 2 10 2 10
Sample Output
54
题目大意:狗王有一个糖果箱,里面放了很多糖,有填的,有酸的,每颗糖的甜度为p,酸度为q。接下来有三种操作:
当t==-1时:狗王从中拿出甜度为x,酸度为y的糖
当t==1时,狗王放进去甜度为x,酸度为y的糖
当t==0时,是一种查询操作,一个小狗有它的糖度和酸度的口味为x,y,你要在箱子里找出最符合他口味的糖,并算出幸福值,p*x+q*y的最大值
#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f3f3f3f3f
#define ll long long
using namespace std;
ll n;
struct node
{
ll p,q;
ll num;
} stu[500010];
int main()
{
ll t,a,b;
while(scanf("%lld",&n)&&n)
{
ll len=0;
for(ll i=0; i<n; i++)
{
scanf("%lld%lld%lld",&t,&a,&b);
if(t==1)
{
stu[len].p=a;
stu[len].q=b;
stu[len++].num=1;
}
else if(t==0)
{
ll maxx=-inf;
for(ll j=0; j<len; j++)
{
if(stu[j].num)
{
maxx=max(maxx,a*stu[j].p+b*stu[j].q);
}
}
printf("%lld\n",maxx);
}
else if(t==-1)
{
for(ll j=0; j<len; j++)
if(stu[j].p==a&&stu[j].q==b&&stu[j].num)
{
stu[j].num=0;
break;
}
}
}
}
return 0;
}
其他大佬的代码
#include <iostream>
#include <string>
#include <vector>
#define ll long long
using namespace std;
struct candy
{
ll x;
ll y;
ll num;//有没有
}c[50005];
//-1 取 1 放 0 判断
int main()
{
int t;
while(cin>>t,t)
{
int index=0;
while(t--)
{
ll n,x,y;
cin>>n;
if(n==1)//放
{
cin>>c[index].x>>c[index].y;
c[index].num=1;
index++;
}
else if(n==-1)//取
{
cin>>x>>y;
for(int j=0;j<index;j++)
{
if(c[j].x==x&&c[j].y==y&&c[j].num)
{
c[j].num=0;
break;
}
}
}
else if(n==0)//判断
{
cin>>x>>y;
ll sum=-99999;
for(int j=0;j<index;j++)
{
if(c[j].num)
{
ll l=c[j].x*x+c[j].y*y;
if(l>sum)
{
// cout<<c[j].x<<" "<<c[j].y<<endl;
sum=l;
}
}
}
cout<<sum<<endl;
}
}
}
return 0;
}