17-比赛1 F - 较小元素 Weak in the Middle (set)
2024-08-27 17:43:53
Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton.
He is provided an array Arr[ ] of N elements.
For each element Arr [ i ], he needs to find the largest element Arr [ j ] where j < i and Arr [ j ] < Arr [ i ]
Help him to achieve this task.
Input
First line comprises N- the number of elements in the array.
Second line contains N space separated integers denoting the elements of the array.
Output
Print N lines denoting the required answer as specified above. In case no such value exists for some
Arr [ i ], print "-1" (without quotes).
Constraints
- 1 ≤ N ≤ 200000
- 1 ≤ Arr [ i ] ≤ 1015
Example
5
1 2 3 5 4
Output:
-1
1
2
3
3
通过 set 或 map 就可轻松解决;
关键字 : set ,map , lower_bound()函数
1.学长的代码
# include <bits/stdc++.h>
using namespace std;
set<long long> s;
int main ()
{
int n;
long long x;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%lld", &x);
x = -x;
auto iter = s.lower_bound(x + );
if (iter == s.end()) puts("-1");
else printf("%lld\n", -*iter);
s.insert(x);
}
return ;
}
2 . 一开始不会使用set ,用map自己做的
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<long long,int> imap;
long long t;
int n;
cin>>n;
for(int i =;i<=n;i++)
{
scanf("%lld",&t);
t = -t;
//map<long long,int>::iterator it;
auto it = imap.lower_bound(t+);
if(it == imap.end()) cout<<"-1"<<endl;
else
cout<<-(it->first)<<endl;
imap[t] = i;
}
return ;
}
最新文章
- 女生的最爱,装饰品。WPF也有,Adorner。(上海晒衣服理念)
- [moka同学摘录]SQL内联、外联的简单理解
- jquery实现页面交互的几个小例子
- JS中取整以及随机颜色问题
- oracle触发器及异常处理 简单例子
- Docker CPU 资源限制——CPU固定核功能测试
- 【学习笔记】【C语言】类型说明符
- 24种设计模式--工厂方法模式【Factory Method Pattern】
- CODEVS 1287 矩阵乘法
- laravel框架中所用到的依赖注入
- EF Core学习Code First
- 如何设置织梦cms自定义表单字段为必填项
- docker中安装ps命令
- mysql 开发基础系列5 运算符
- IIS 错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- css 可继承属性 display:inline-block 历史
- 3--Selenium环境准备--Eclipse 引入 selenium-server包
- WEBapi在IIS发布注意事项-发布错误
- SQL Server 用角色(Role)管理数据库权限
- IT小鲜肉 Widgets Tree 单选、多选、相关回调函数、获取选中的节点功能