A-Leftbest(沈阳站)

A-Leftbest

Jack is worried about being single for his whole life, so he begins to use a famous dating app. In this app, the user is shown single men/women’s photos one by one, and the user may choose between “yes” and “no”. Choosing “yes” means an invitation while choosing “no” means nothing. The photos would be shown one by one until the number of rest photos to be shown reaches zero. Of course, efficient and single Jack would always choose “yes”.

When viewing photos, Jack would have a “fake impression point” on every photo, which is not accurate. To calculate the “true impression point” of one photo, Jack would recall the “fake impression point” of every previous photo whose “fake impression point” is larger than this photo, and regard the smallest “fake impression point” of them as the “true impression point” of this photo. Jack would like to sum the “true impression point” of all photos as the outcome of his effort.

Note that if such a larger “fake impression point” does not exist, the “true impression point” of this photo is zero.

Input
The first line contains an integer (1≤n≤100000) — the number of photos.

The second line contains n integers a1, a2,…, an where ai (0 <=ai <=108) is the “fake impression point”of the i-th photo.
Output
Output a single integer — the sum of the “true impression point” of all photos.

Sample Input

4
2 1 4 3

Sample Output

6

题目大意

给你一个长度为 n 的数组然后让找出第 i 个数字之前的比第 i 个数字大的最小的那个数字,然后求这样的数字之和;比如说样例
2之前比2大的数字没有,所以ans+=0,
1之前比1大的数字只有2,所以ans+=2
4之前比4大的数字没有,所以ans+=0
3之前比3大的数字是4,所以ans+=4;最后答案就是6;

解题思路

简单的话,我们可以直接用set一次的添加,然后通过setfind()函数进行查找或者二分查找都可以;这样就可以了;
当然也可以用线段树去写,就是麻烦点,我们先将这下数据离散化,然后按照顺序去更新线段树,从第一个数据开始插入,我们
构造的线段树是按照离散化处理后的值插入对应离散前的值,这样每次去离散化后的哪个位置以后的这个区间大的最小值就可以了

比如说下面这组数据
5
3 2 4 5 4
离散化处理后数组的值分别变成
2 1 3 4 3
开始的时候我们在第2个位置插入3,然后看第二个位置以后的最小值是多少
第二次在1这个位置插入2看第一个位置之后的最小值是多少,发现是2
然后依次类推就可以了
当然也有点小细节就是 我们刚开始构建树的时候是先给树赋上一个不可能出现的值,当我们查找过后如果发现答案是这个不可能出现的值时,就不用管了;
具体点的看下代码吧;
第一个是用set写的,第二个使用线段树写的

代码

set代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[100100];
set<int>se;
int main()
{
   
	ios::sync_with_stdio(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
   
		se.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值