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 ...
随机推荐
- Flask-Script-Migrate
Flask-Script 从字面意思上来看就是 Flask 的脚本 是的,熟悉Django的同学是否还记得Django的启动命令呢? python manager.py runserver 大概是这样 ...
- SublimeText插件autoprefixer : css 添加前续
/* 使用前 */ body { background: linear-gradient(to bottom, #DADADA, #000); } p a { -webkit-border-radiu ...
- Java中mouseDragged有效mouseMoved没响应的可能原因
1.这个问题在jdk7与jdk8上都会出现. 2.具体表现为: 单独写个测试例子,用JFrame实现了mouseMoved接口,mouseDragged和mouseMoved都输出方法名和坐标,结果是 ...
- 动态原型模式 js
动态原型模式 function Person(name,age){ this.name = name; this.age = age; if(typeof this.sayName != " ...
- TCP的可靠连接是如何产生的?
http://bbs.csdn.net/topics/190011812 看过TCP/IP的源代码没?tcp中所谓的连接只是在tcp的tcb中存储了对端的地址信息,并且记录连接的状态,通过重发之类的来 ...
- centos6 yum 安装 install c++4.8 gcc4.8
cd /etc/yum.repos.d wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo yum --enablerep ...
- Annual Congress of MUD(最大流)
Annual Congress of MUD 时间限制: 1 Sec 内存限制: 128 MB提交: 80 解决: 10[提交] [状态] [讨论版] [命题人:admin] 题目描述 Multi ...
- P2082 区间覆盖(加强版)
题目 #include<iostream> #include<algorithm> #include<cstring> using namespace std; s ...
- git常用命令图解
- machine learning trends from nips14
from John Platt, Deputy Managing Director and Distinguished Scientist at Microsoft Research http://b ...