zoukankan      html  css  js  c++  java
  • 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) --- the number of photos.

    The second line contains n integers a1​​,  a2​​ , …,  an​​  where  ai​​  (0)  is  th e  “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
    
     
    作者: NEUACM
    单位: 东北大学
    时间限制: 1000 ms
    内存限制: 64 MB
    代码长度限制: 16 KB

     题意: 输入n个数存入数组 a[],求和,求和的方式是 a[i]的值为前面所有大于a[i]的数中最小的那个,如果没有则为0;

    思路:先介绍两个函数

    lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。

    upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。
    前提是:数组必须从小到大排序。

    由题得选择 upper_bound 函数,从小到大排序 则用set

    AC代码:

    #include<bits/stdc++.h>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<string>
    #include<list>
    #include<set>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5+10;
    
    int main()
    {
        set<ll> v;
        ll n;
        cin>>n;
        ll node[maxn];
        for(int i=0;i<n;i++)
        {
            cin>>node[i];
        } 
        ll sum = 0;
        v.insert(node[0]);
        for(int i=1;i<n;i++)
        {
            auto it = v.upper_bound(node[i]);
            if(it!=v.end())
            {
                sum+=*it;
            }
            v.insert(node[i]);
        }
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    【深度学习系列1】 深度学习在腾讯的平台化和应用实践
    js复制button在ie下的解决方式
    兔子--Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
    UART串口协议基础1
    高校站点群建设方案简单介绍
    oracle10G之前介质下载地址【珍藏版】
    程序猿打新总结 6月份 新股申购秘籍
    斯坦福IOS开发第五课(第一部分)
    O2O领域添新军,正品网加快布局的战略考量
    如风一样,飞翔------Day37
  • 原文地址:https://www.cnblogs.com/subject/p/12445777.html
Copyright © 2011-2022 走看看