17-比赛1 F - 较小元素 Weak in the Middle (set)
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 ;
}
17-比赛1 F - 较小元素 Weak in the Middle (set)的更多相关文章
- 基于visual Studio2013解决算法导论之017查找第n小元素
题目 查找第n小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...
- 基于visual Studio2013解决算法导论之015第二小元素
题目 查找第二小元素 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...
- Ex 2_22 两个有序列表合并后的第k小元素..._第四次作业
package org.xiu68.ch02; public class Ex2_22 { public static void main(String[] args) { // TODO Auto- ...
- 快速排序以及第k小元素的线性选择算法
简要介绍下快速排序的思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此 ...
- 中位数与第K小元素
算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...
- 清橙OJ 1082 查找第K小元素 -- 快速排序
题目地址:http://oj.tsinsen.com/A1082 问题描述 给定一个大小为n的数组s和一个整数K,请找出数组中的第K小元素. 这是一个补充程序的试题,你需要完成一个函数: int fi ...
- 寻找第K小元素
要在一个序列里找出第K小元素,可以用排序算法,然后再找.可以证明,排序算法的上界为O(nlogn). 在这里,给出两种可以在线性时间内找出第K小元素的方法. 方法1: (1) 选定一个比较小的阈值(如 ...
- 算法导论学习之线性时间求第k小元素+堆思想求前k大元素
对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...
- 减治算法之寻找第K小元素问题
一.问题描写叙述 给定一个整数数列,寻找其按递增排序后的第k个位置上的元素. 二.问题分析 借助类似快排思想实现pation函数.再利用递归思想寻找k位置. 三.算法代码 public static ...
随机推荐
- 有意思的jsonp
<style> body{margin: 0;} ul{margin: 0;padding: 0;list-style: none;} a{color:inherit;text-decor ...
- 好用的切换滑动焦点图框架jquery.superslide
拿到学习网站:http://www.superslide2.com/
- 【Node.js】Stream(流)的学习笔记
最近学习使用Node.js创建http proxy server,少不了要跟Stream打交道.昨天开始查阅一些资料,多少有了一些粗浅了解.整理在这里,供学习之用. 从Node.js API文档中可知 ...
- vos2009如何监听客户行业是否正规
在对接被叫改写规则中将改写规则由原来0:9150 改为0:9150+自己手机号,这样客户打出话全转接到自己手机上,可以接听客户销售行业:
- php:定义时间跳转到指定页面
我们想要定义延迟时间,再跳转到指定页面,只要用header()即可,语法: header("Refresh:延迟时间;url=要跳转的页面"); 例子: 注意注意:我们在heade ...
- April 25 2017 Week 17 Tuesday
Have you ever known the theory of chocie? There are a bunch of axiems, but there are only two thing ...
- IOS GCD03-其他用法
#define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define main_queu ...
- caffe resize用interpolation
opencv的resize默认的是使用双线性插值INTER_LINEAR,也可以是尝试其他的方式进行插值操作 if (param.random_interpolation_method()) { // ...
- JSON对象与XML相互转换工具类
依赖jar <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId ...
- lasagne保存网络参数
# Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...