文章标题

pair对的用法1  https://blog.csdn.net/effective_coder/article/details/8733645

 


#include<iostream>
#include<set>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 1e5 + 30;
multiset<pair<LL, int> >s;
//set里面是pair对,前面是关键值long long 
int main(){
	int T, n;
	LL x;
	for (scanf_s("%d", &T); T; --T){
		LL a1 = 0, a2 = 0;
		s.clear();
		scanf_s("%d", &n);
		for (int i = 1; i<=n; i++){
			scanf_s("%lld", &x);
			if (s.empty()) s.insert({ x, 1 });
			// 如果是空的 那么直接插入咯
			//s.insert({x,1});
			//作为pair对来说,insert(make_pair(x,1));
			else{
				if ((s.begin()->first) < x){
					//如果不重复set的最开始的first比x要小的话
					a1 += x - (s.begin()->first);//这个应该 是所得到的钱数
					a2 += (s.begin()->second);
					pair<LL, int> tmp = *s.begin();
					s.erase(s.begin());

					//如果首个等于0的话不仅要插入1还要插入0 (对,没错。。因为没有用它了
					//那?本来是5 9 11 ,插入5 1 然后9 1 卖掉,得到4元,1次交易
					//随后,插入9,0 然后11判断,就只要加上2,然后没消耗
					//在这里停下的话,得到6元,一次交易,是最好的(卖过的变0!
					//但是如果是8呢 5 9 11 10  岂不是6+1?
					//如果首个不是0的话只插入0就可以了  因为用掉了、、
					//oo....  9  returns to what it is before..
					if (tmp.second == 0) s.insert({ tmp.first, 1 });
					s.insert({ x, 0 });
				}
				//可以卖就卖,卖完把自己放进set
				else
					s.insert({ x, 1 });
				//如果不重复set的最开始first比x要大或者相等,就加入
				//比如2元,5元,那么卖了、5元,2元,不卖
			}
		}
		printf("%lld %lld\n", a1, a2 * 2);
	}
	return 0;
}

 

 

好了我也A掉了 其中有些不可饶恕的小错误  换了电脑再来补


//reserve是begin和end吗???
//不,, 那是reverse
// 想来  完全可以写成优先队列,反正按顺序的就好了嘛
// 然后就是  set 里面要--的
#include<iostream>
#include<set>

using namespace std;
typedef long long ll;
#define maxn int (1e5+5)
multiset<pair<ll, int> >s;
int main(){
	int t; cin >> t; while (t--){
		ll n;
		cin >> n;
		s.clear();//千万别忘记!!!啊啊啊!!
		ll  sum1 = 0; ll sum2 = 0; ll x;
		for (int i = 1; i <= n; i++){
			cin >> x;
			if (s.empty())s.insert({ x, 1 });
			else if (s.begin()->first < x) {//更大
				sum1 += x - (s.begin()->first);
				sum2 += s.begin()->second;
					pair<ll, int> tmp = *s.begin();
					///这个没打,凉了呀
				s.erase(s.begin());
				if (tmp.second == 0)s.insert({ tmp.first, 1 });
				s.insert({ x, 0 });

			}
			else s.insert({ x, 1});
			

		}
		cout << sum1 <<" "<< sum2*2 << endl;
	}
	return 0;
}

而且没想通  这是为什么呢

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值