Leftbest
链接:https://ac.nowcoder.com/acm/contest/7830/A
来源:牛客网

题目描述

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.
输入描述:
The first line contains an integer {n}n (1 \le n \le 100,0001≤n≤100000) — the number of photos.
The second line contains n integers is the “fake impression point” of the i-th photo.
输出描述:
Output a single integer — the sum of the “true impression point” of all photos.
示例1
输入
复制

4 2 1 4 3

输出
复制

6

题意:

排在a[i]前面 比a[i]大的数中最小数的和是多少?

题解:

第一反应是用大小堆来做,思路很简单,但是却超时了。。
后来想起来set里面本身就是函数是用来求这个的

lower_bound(val);        //查找大于等于val第一个元素的位置,没有找到返回set::end()upper_bound(val);        //查找大于val第一个元素的位置,没有找到返回set::end()

所以直接
j=s.upper_bound(x)就行
STL有好多现成的东西,太久没用都忘得差不多了

代码:

一开始大小堆的代码:

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=1e5+8;
int a[maxn];
typedef long long ll;
priority_queue<int,vector<int>,less<int> >q;//从小到大
priority_queue<int,vector<int>,greater<int> >w;//从大到小
int main()
{ios::sync_with_stdio(false);int n;scanf("%d",&n);ll sum=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);while(!q.empty()){//  printf("q.top=%d\n",q.top());//cout<<"q.top="<<q.top<<endl;if(q.top()>x){w.push(q.top());q.pop();}else break;}if(!w.empty())sum+=w.top();while(!w.empty()){q.push(w.top());w.pop();} q.push(x);}cout<<sum;return 0;
}

AC代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1e5+8;
set<int>s;
typedef long long ll;
int main()
{ios::sync_with_stdio(0);int n;cin>>n;ll sum=0;while(n--){int x;cin>>x;s.insert(x);auto j=s.upper_bound(x);if(j!=s.end())sum+=*j;}cout<<sum;return 0;
}

扩展

刚才那个题求的是前缀较大的最小值
我们扩展到其他几个
注意:
set在内部会自动排序,且会自动查重(即每个数最多出现一次)

前缀较大的最大值

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;cin>>x;s.insert(x);n--;while(n--){cin>>x;auto it=s.rbegin();  //不能"auto it=s.end()-1;"会报错//auto it =s.end(); it--;if(x<*it) ans+=*it;s.insert(x);}cout<<ans<<endl;return 0;
}

前缀较小的最大值

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;while(n--){cin>>x;s.insert(x);auto it=s.lower_bound(x);if(it!=s.begin() && it!=s.end()){  //两边都要考虑,begin是因为可能找不到,end是因为可能都比查找值小it--;ans+=*it;} }cout<<ans<<endl;return 0;
}

前缀较小的最小值

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
int main(){int n,x;ll ans=0;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);set<int> s;cin>>n;cin>>x;s.insert(x);n--;while(n--){cin>>x;auto it=s.begin();if(x>*it){ans+=*it;}s.insert(x);}cout<<ans<<endl;return 0;
}

2020牛客国庆集训派对day3 Leftbest相关推荐

  1. 2020牛客国庆集训派对day3 I.Rooted Tree(哈代-拉马努金拆分数列)

    2020牛客国庆集训派对day3 I.Rooted Tree(哈代-拉马努金拆分数列) 题目 https://ac.nowcoder.com/acm/contest/7830/I 题意 给你n个点,问 ...

  2. 2020牛客国庆集训派对day3 Points

    Points 题目描述 Jack and Rose are playing games after working out so many difficult problems. They toget ...

  3. 2020牛客国庆集训派对day2 补题J

    2020牛客国庆集训派对day2 补题J:VIRUS OUTBREAK 题目描述 The State Veterinary Services Department recently reported ...

  4. 2020牛客国庆集训派对day2 H-STROOP EFFECT(英语题)

    2020牛客国庆集训派对day2 H-STROOP EFFECT(英语题) 题目 https://ac.nowcoder.com/acm/contest/7818/H 题意 这题目真的太难读懂了,赛后 ...

  5. 2020牛客国庆集训派对day8 G-Shuffle Cards(扩展STL容器,rope可持久化平衡树)

    2020牛客国庆集训派对day8 G-Shuffle Cards(扩展STL容器,rope可持久化平衡树) 题目 https://ac.nowcoder.com/acm/contest/7865/G ...

  6. 2020牛客国庆集训派对day1 A.ABB

    2020牛客国庆集训派对day1 A.ABB 题目链接 题目描述 Fernando was hired by the University of Waterloo to finish a develo ...

  7. 2020牛客国庆集训派对day2 F题 Java大数处理

    题目: 链接:https://ac.nowcoder.com/acm/contest/16913/F 来源:牛客网 The following code snippet calculates the ...

  8. 2020牛客国庆集训派对day8

    牛客网链接 文章目录 Easy Chess 题意: 题解: Easy Problemset 题意 题解: Shuffle Cards 题解: Diff-prime Pairs 题意 题解: 代码: E ...

  9. 2020牛客国庆集训派对day4 Arithmetic Progressions

    Arithmetic Progressions 链接:https://ac.nowcoder.com/acm/contest/7831/B 来源:牛客网 题目描述 An arithmetic prog ...

最新文章

  1. 技术总监的反思录:我是如何失去团队掌控的?
  2. linux 用root安装mysql数据库_Linux上安装Mysql及简单的使用详解
  3. java消息推送与接收
  4. python数据读取失败无法启动应用_tensorflow初学者教程-读取数据集失败
  5. Mac 开发中如何设置 关闭 以及最小化 最大化按钮事件处理
  6. SAP Spartacus RouteGuard路由守卫之CmsPageGuard
  7. Cluster table import - BSP UI component source code is actually stored in cluster table
  8. js实现字体和容器宽高随窗口改变
  9. TextDetection文本检测数据集汇总
  10. jdom编写xml自动缩进_Spring Beans 自动装配
  11. Atitit spirngboot 访问 html文件总结 自设计web服务器原理与实现 Url路由压力,读取url,获得项目更路径绝对路径,拼接为文件路径。读取文建内容输出即可 目录路径 u
  12. 浏览器主页劫持查杀,查杀主页劫持木马方法
  13. 初中计算机成绩评定方案,初中信息技术学科评价方案
  14. 深度解析工业机器人主流离线编程软件
  15. 植物大战僵尸修改存档用户名、关卡、金钱说明
  16. sql基础语法,非常全建议收藏(大白菜程序猿欢迎大家关注)
  17. 西工大noj(25,26)
  18. mac mini php开发,mac mini主要用来干嘛
  19. 短网址生成+域名检测+短网址还原+域名防红四合一前端源码
  20. hacker 入门指南

热门文章

  1. 中考新大纲:初中数学无非就这26个考点!孩子吃透,再笨也能考115分!
  2. 程序员编程10大原则,请牢牢记住!
  3. 一张纸一幅图,竟然提高了10倍的学习和工作效率!?
  4. Python 写各大聊天系统的屏蔽脏话功能原理
  5. linux服务器管理公司用户,在Linux服务器Jenkins中管理用户和角色的方法
  6. oracle crontab e,Linux运维知识之通过crontab -e编辑生成的定时任务,写在哪个文件中...
  7. 如何将mysql文件导入MySQL_如何将mysql5的sql文件导入到mysql4?
  8. dreamweaver连接mysql数据库 发生一个不知名错误_用DREAMWEAVER连接数据库测试时总是弹出发生一个不知名的错误 你好! 请问一下这个问题你是怎么解的?...
  9. python与matlab混合编程_python 与 matlab 混编
  10. html 页面工具,html页面工具-htmlUnit