Leftbest(set的使用)(pta)

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 a​1​​, a​2​​, …, a​n​​ where a​i​​ (0≤a​i​​≤10​8​​) 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,后面跟着n个数字。

每输入个数字,如果他前面有比它大的,那么取比它大的里面最小的那个;如果它前面没有比它大的了,此位置取零。

把每个位置取得数加起来输出。

 

思路:

继续练习摸索set用法

爱吃代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
set<int>s;
set<int>::iterator it;
int main()
{
    int n,i,a,maxx=0;
    long long ans=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a);
        if(a>=maxx)
            ans+=0;
        else
        {
            it=s.upper_bound(a);
            ans+=*it;
        }
        s.insert(a);
        maxx=max(maxx,a);
    }
    printf("%lld\n",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值