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> ...
随机推荐
- mac配置appium进行自动化测试
先去brew.sh这个网站,拷贝最新的下载安装命令,黏贴到终端里,回车,按提示下载安装. 安装完毕后,运行brewupdate 看看是否有更新,然后输入brew ios-webkit-debug-pr ...
- ArcGIS API Reference & Flex API samples学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________学习资源: 1.http://help.arcgis.com/en/webapi/flex/apiref/index.html ...
- 再来说说Activity
经过前面多天的了解,现在可以确信一点: activity提供了用户和程序交互的界面. 而且android里有四大组件:Activity,Service,BroadcastReceiver,Conten ...
- Hadoop学习记录(6)|Eclipse安装Hadoop 插件
下载 https://skydrive.live.com/redir.aspx?cid=cf7746837803bc50&resid=CF7746837803BC50!1277&par ...
- 【数据结构与算法分析——C语言描述】第一章总结 引论
这一章主要复习了一些数学知识,像指数.对数.模运算.级数公式:还有2种证明方法,归纳假设法和反证法.所幸以前学过,重新拾捡起来也比较轻松. 简要地复习了递归,提出了编写递归例程的四条基本法则: 基准情 ...
- Linux下静态库生成和使用
Linux下静态库生成和使用 一.静态库概念 1.库是预编译的目标文件(object files)的集合,它们可以被链接进程序.静态库以后缀为”.a”的特殊的存档(archive file)存储. ...
- 有关require package的应用
http://stackoverflow.com/questions/9302284/relative-paths-with-requirejs-modules-packages http://sta ...
- Speex for Android
http://blog.csdn.net/chenfeng0104/article/details/7088138在Android开发中,需要录音并发送到对方设备上.这时问题来了,手机常会是GPRS. ...
- centos安装lxml和pyspider
yum -y install --nogpgcheck python34u-devel.x86_64 yum -y install libcurl-devel yum -y install libxs ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...