hdu 5127 vector与list的区别

本文介绍了一道关于计算不同狗狗对糖果美味度的程序题,糖果由甜度和酸度构成,而每只狗狗对甜度和酸度有不同的偏好。通过使用vector和list两种数据结构进行对比实验,探讨了不同数据结构在此类问题中的应用效果。

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

http://acm.hdu.edu.cn/showproblem.php?pid=5127


Dogs' Candies

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


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


vector和list的区别(可以看作数组和链表的区别)
(数组)  (链表)
需要大量插入和删除的时候,用list会快很多,

这道题用vector的时间是用list时间的一半,所以应该是测试数据里查找的比较多吧,,,

(注意要用long long才能AC)




下面用vector

/*
5127

糖果两个属性  甜p  酸q
狗 两个数学  喜欢甜x   喜欢酸y

*/

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
vector和list的区别(可以看作数组和链表的区别)
(数组)  (链表)
需要大量插入和删除的时候,用list会快很多,
而且这道题的查找也是没有直接按索引值来的
*/
typedef long long ll;
vector <pair<ll, ll>> vec;
int main()
{
	int n;
	while (scanf("%d", &n) && n)
	{
		int t, x, y;
		vec.clear();
/*
t
-1 拿出来
1  放进去
0  求最大  (在这里就要输出)
*/
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d%d", &t, &x, &y);
			vector<pair<ll, ll>>::iterator it;
			if (t == -1)
			{
				pair<ll, ll> t = pair<ll, ll>(x, y);
				for (it = vec.begin(); it != vec.end(); it++)
				{
					if (*it == t)
					{
						it = vec.erase(it);//其实这里的迭代器保留下来也没用,也可以直接vec.erase()
						break;
					}
				}
			}
			else if (t == 1)
			{
				vec.push_back(pair<ll, ll>(x, y));
			}
			else if (!t)
			{
				ll maxx = 0;
				int flag = 1;
				for (it = vec.begin(); it != vec.end(); it++)
				{
					if (flag)
					{
						maxx = it->first*x + it->second*y;
						flag = 0;
					}
					else
						maxx = max(maxx, it->first*x + it->second*y);
				}
				printf("%I64d\n", maxx);
			}
		}//for i...n
	}//while
//	system("pause");
	return 0;
}



下面用list

/*
5127

糖果两个属性  甜p  酸q
狗 两个数学  喜欢甜x   喜欢酸y

*/

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
/*
vector和list的区别(可以看作数组和链表的区别)
(数组)  (链表)
需要大量插入和删除的时候,用list会快很多,
而且这道题的查找也是没有直接按索引值来的,
*/
typedef long long ll;
list <pair<ll, ll>> lis;
int main()
{
	int n;
	while (scanf("%d", &n) && n)
	{
		int t, x, y;
		lis.clear();
		/*
		t
		-1 拿出来
		1  放进去
		0  求最大  (在这里就要输出)
		*/
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d%d", &t, &x, &y);
			list<pair<ll, ll>>::iterator it;
			if (t == -1)
			{
				pair<ll, ll> t = pair<ll, ll>(x, y);
				for (it = lis.begin(); it != lis.end(); it++)
				{
					if (*it == t)
					{
						it = lis.erase(it);//其实这里的迭代器保留下来也没用,也可以直接vec.erase()
						break;
					}
				}
			}
			else if (t == 1)
			{
				lis.push_back(pair<ll, ll>(x, y));
			}
			else if (!t)
			{
				ll maxx = 0;
				int flag = 1;
				for (it = lis.begin(); it != lis.end(); it++)
				{
					if (flag)
					{
						maxx = it->first*x + it->second*y;
						flag = 0;
					}
					else
						maxx = max(maxx, it->first*x + it->second*y);
				}
				printf("%I64d\n", maxx);
			}
		}//for i...n
	}//while
	 //	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值