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. 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree

    ▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题 ▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ ...

  2. Memo synEditor 当前行号

    Memo 当前行号,坐标,位置 可以使用Memo的属性CaretPos.X来取行鼠标所在行的行数与鼠标所在行的第几位 Memo.CaretPos.X 光标或鼠标所在行的列号(第几位),从0开始计数Me ...

  3. mybatis相对于ibatis的优势

    2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...

  4. [X264] MinGW编译x264,VC中调用libx264.dll-------------<参考转>

    1. 下载并按照MinGW,最好就缺省按照    http://sourceforge.net/projects/ ... ler/mingw-get-inst/    把C:\MinGW\bin添加 ...

  5. mpg123解码相关

    int attribute_align_arg mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmem ...

  6. update svn cache 慢

    eclipse 打开了一个工程,就进行了update svn cache,弄了2个多小时了还是在进行.观察这个过程在空文件夹上也进行了不少时间,我感觉到可能方法错了.试了下关闭SVN--> sv ...

  7. [ML]熵、KL散度、信息增益、互信息-学习笔记

    [ML]熵.KL散度.信息增益.互信息-学习笔记 https://segmentfault.com/a/1190000000641079

  8. MySQL之——GROUP BY分组取字段最大值

    转载自:http://blog.csdn.net/l1028386804/article/details/54657412 假设有一个业务场景,需要查询用户登录记录信息,其中表结构如下: CREATE ...

  9. Java 设计模式系列(九)组合模式

    Java 设计模式系列(九)组合模式 将对象组合成树形结构以表示"部分-整体"的层次结构.组合模式使得用户对单个对象的使用具有一致性. 一.组合模式结构 Component: 抽象 ...

  10. Mockplus组件样式库一键解决风格复用

    在Mockplus3.3版本中,新增了组件样式库,可以快速复用组件风格,同时可以将组件风格保存到库中. 官网地址:https://www.mockplus.cn 1. 保存样式 选中组件,设置好该组件 ...