Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
B. Queue
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
codeforces.com/problemset/problem/91/B
Description
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Sample Input
6
10 8 5 3 50 45
Sample Output
2 1 0 -1 0 -1
HINT
题意
给你一个数列,让你找到最右边比这个数小的数的位置,如果没有就输出-1
题解:
直接线段树中二分,查询最小值,然后二分区间就好了~
代码
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} //************************************************************************************** int tr[maxn];
int a[maxn];
int ans[maxn];
int tmp;
void build(int x,int l,int r)
{
if(l==r)
{
tr[x]=a[l];
return;
}
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
tr[x]=min(tr[x<<],tr[x<<|]);
}
void query(int x,int l,int r,int t)
{
if(l==r)
{
ans[tmp++]=l-t-;
return;
}
int mid=(l+r)>>;
if(tr[x<<|]<a[t])
query(x<<|,mid+,r,t);
else
query(x<<,l,mid,t);
}
void update(int x,int l,int r,int t)
{
if(l==r)
{
tr[x]=inf;
return;
}
int mid=(l+r)>>;
if(t<=mid)
update(x<<,l,mid,t);
else
update(x<<|,mid+,r,t);
tr[x]=min(tr[x<<],tr[x<<|]);
}
int main()
{
int n=read();
for(int i=;i<=n;i++)
a[i]=read();
build(,,n);
for(int i=;i<=n;i++)
{
if(tr[]>=a[i])
ans[tmp++]=-;
else
query(,,n,i);
update(,,n,i);
}
for(int i=;i<n;i++)
printf("%d ",ans[i]);
}
Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分的更多相关文章
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 二分
B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #67 (Div. 2)
Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- Java并发编程-可重入锁
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍可以获取该锁而不受影响.在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁. publ ...
- 牛课--C/C++
引用是除指针外另一个可以产生多态效果的手段. //引用是除指针外另一个可以产生多态效果的手段. #include<iostream> using namespace std; class ...
- air 移动开发配置文件详解
转自http://www.badyoo.com/index.php/2012/09/12/208/index.html 目录 所需的 AIR 运行时版本 应用程序标识 应用程序版本 主应用程序 SWF ...
- 麒麟OS剽窃
今年对于我们的IT行业来说可以算是耻辱的一年. 首先是“汉芯丑闻”,上海交大研制了一个所谓的国内第一个完全拥有自主知识产 权的DSP芯片(数字信号微处理器)——“汉芯”,研制人陈进教授以此领取政府一亿 ...
- canvas脏域问题纪录
canvas 脏域问题 今天无意之中碰见了一.问题描述: 在支持html5的浏览器中运行javascript脚本,脚本主要是操作网页上的标签canvas,出错的操作为, getImageData(im ...
- 第三百三十二天 how can I 坚持
今天一大早,住的这就施工了,被吵醒了.. 下午去了趟小米之家,小米5还行,黑科技不黑,哈哈. 小米5黑科技不太黑,就知道造词,整体感觉还行,就是感觉屏幕有点长,小米之家人倒是不少,还有老太太去小米之家 ...
- 转】Spark DataFrame小试牛刀
原博文出自于: https://segmentfault.com/a/1190000002614456 感谢! 三月中旬,Spark发布了最新的1.3.0版本,其中最重要的变化,便是DataFrame ...
- 【126】win8的一些问题
1.win8 窗口背景色修改 在Windows默认主题下,打开注册表编辑器(win键+R,即运行,输入regedit),依次双击打开HKEY_CURRENT_USER\Control Panel\Co ...
- jQuery Callback 函数
@(编程) Callback 函数在当前动画 100% 完成之后执行. jQuery 动画的问题 许多 jQuery 函数涉及动画.这些函数也许会将 speed 或 duration 作为可选参数. ...
- [iOS微博项目 - 3.4] - 获取用户信息
github: https://github.com/hellovoidworld/HVWWeibo A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上 ...