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
题解:
对与某一位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 二分的更多相关文章
- 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 SSH框架
1整体流程: 1:hibernate流程: 2:hibernate使用流程: 2.1:建立用户jar包,导入hibernate相应的jar包 2.2 引入mysql的JDBC驱动包 2.3 在数据库中 ...
- BeanUtils.copyProperties不能copy复杂对象List的解决方式
需要注意的就是把List拆分,遍历add,然后把list设置到返回对象中 package test.test; import java.util.ArrayList; import java.util ...
- Eclipse安装Freemarker Editor插件
在下面网址里下载freemarker-ide: http://sourceforge.net/projects/freemarker-ide/files/ 下载完成后后解压,由于该IDE里面的free ...
- iOS 通过URL网络获取XML数据的两种方式
转载于:http://blog.csdn.net/crayondeng/article/details/8738768 下面简单介绍如何通过url获取xml的两种方式. 第一种方式相对简单,使用NSD ...
- FME2010 案例分析: 动态批量转换
Link: http://blog.163.com/antufme@126/blog/static/140492492201022545726452/?suggestedreading&wum ...
- FM模型
[ 闲聊DNN CTR预估模型] http://www.mamicode.com/info-detail-1465813.html http://blog.csdn.net/bitcarmanlee/ ...
- js 倒计时功能,获取当前时间的年月日,时分秒
一.实现当前时间到指定截止时间的倒计时功能 <html> <head> <title>TEST</title> </head> <bo ...
- 「红米 2A 标准版」闪屏救砖、更正官方线刷救砖工具
问题描述 用 ES 浏览器 卸载了内置软件后重启无法开机,停在 MI android 界面并出现屏幕忽明忽暗的现象,无法进入系统. 漫长的救砖探索,直白的解决方案 总体来说,林林总总下了六个 G 的教 ...
- vmware虚拟机开机报附件中的错误的解决办法
Virtualized Inter VT-x/EPT is incompatible with this virtual machine configuration 在没有虚拟化msg.inter.h ...
- c语言静态断言-定义自己的静态断言
c语言里面可以自己定义静态断言,更加方便的调试代码. 使用静态断言 #include<stdio.h> #include<stdlib.h> #include<asser ...