2019-ICPC沈阳重现赛 A.Leftbest

传送门

题意:对于每个数字,找到它左面比它大的数字中最小的那一个,然后求和。

题解:这个题考查STL中的set集合。set自动升序排列。读取每一个数字放入set,通过迭代器求这个数字在set中的位置,则这个位置的下一个位置上的数字就是题目要求的数字。

#include <iostream> 
#include <algorithm>
#include <set> 

using namespace std;

typedef long long ll;

int main()
{
	set<ll> st;
	set<ll>::iterator it;
	ll n;
	cin>>n;
	ll sum=0;
	for(ll i=0;i<n;i++)
	{
		ll x;
		cin>>x;
		st.insert(x);
		it=st.find(x);
		it++;
		if(it!=st.end()) sum+=*it;//如果这个数字是最大的就不加了 
	}
	cout<<sum;
	
	return 0;
}
原创文章 99 获赞 15 访问量 7327

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/104742116
今日推荐