B. Queue

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

codeforces.com/problemset/problem/91/B

Description

There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

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

题解:

对与某一位i,假设最右边比他大的数为j,那么a[i]>a[j],然后将min{a[j+1],a[j+2],...,a[n]}记作min[j+1], 则a[i]<=min[j+1],

不难想到不等式: min[j]<=a[j]<a[i]<=min[j+1]

这就说明min数组单增,然后在单增序列里面找一个值。

这不就是二分吗?然后就没了。

代码

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
int n,a[N],mi[N];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int ef(int x,int l,int r)
{
if (l==r)return l;
int mid=(l+r+)>>;
if (x>mi[mid])
return ef(x,mid,r);
else return ef(x,l,mid-);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);
for(int i=;i<=n;i++)read(a[i]);
mi[n]=a[n];
for(int i=n-;i>=;i--)mi[i]=min(mi[i+],a[i]);
for(int i=;i<=n;i++)
{
int ans=ef(a[i],i,n)-i-;
printf("%d ",ans);
}
}

Codeforces Beta Round #75 (Div. 1 Only) B. Queue 二分的更多相关文章

  1. 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 ...

  2. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  3. Codeforces Beta Round #67 (Div. 2)

    Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. Pthreads 环境配置,VisualStudio

    ▶ Visual Studio 下配置MPI环境 ● 下载 Pthreads(http://pthreads.org/),解压. ● 针对 x64 程序的配置 ■ 将 Pre-built.2\incl ...

  2. 关于SSE的一些资料

    之前查到的,觉得还不错,一直没时间看,放这里先存着 https://software.intel.com/en-us/articles/using-intel-streaming-simd-exten ...

  3. 跟我学算法-xgboost(集成算法)基本原理推导

    1.构造损失函数的目标函数 2.对目标函数进行泰勒展开 3.把样本遍历转换成叶子节点遍历,合并正则化惩罚项 4.求wj进行求导,使得当目标函数等于0时的wj的值 5.将求解得到的wj反导入方程中,解得 ...

  4. c++经典排序算法全集(转)

    C++排序算法全集 排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法对算法本身的速度要求很高. 一.简单排序算法 由于程序比较简单,所以没有加什么注释.所有的程序都给出了完 ...

  5. centos 使用windows7 存储

    1. 在Windows7上创建一个带密码的用户,如disk 2. 创建一个文件夹,如 D:\centos-disk2 3. 选中此文件夹,点击上方的  共享 -> 特定用户, 添加disk用户, ...

  6. MongoServerSettings Members

    The MongoServerSettings type exposes the following members. Constructors   Name Description MongoSer ...

  7. update-alternatives命令详解

    转载:http://blog.csdn.net/maixia24/article/details/11707289 update-alternatives是ubuntu系统中专门维护系统命令链接符的工 ...

  8. NormalMap原理详细解析

    NormalMap的实现标志着对渲染流水线的各个环节以及矩阵变化有了正确和深入的认识.这里记录一下学习过程,以及关于NormalMap的诸多细节. 刚开始想要实现NormalMap程序的时候,查阅的是 ...

  9. 没有Home键之后的iPhone会是什么样子?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 之前笔者推测新一代的iPhone不会再有“Home”键.果不其然,9月13日苹果发布会证实了笔者的观 ...

  10. JQuery 对象和事件

    JQuery 对象和事件 一:JQuery 对象和 Dom 对象 在使用 JQuery 过程中,我们一般(也是绝大多数情况下,除非我们使用了第二个框架)只有两类对象,即:JQuery 对象和 Dom ...