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> ...
随机推荐
- leetcode225
public class MyStack { Queue<int> Q = new Queue<int>(); /** Initialize your data structu ...
- div高度自适应的问题
对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{height:100%;} 上面的css样式是无效的,不会产生任何效果. 需要改写: html,body{ m ...
- MS SQL Server 无法添加、更新或删除从msx服务器上发起的作业(或其步骤或调度)
因为 服务器 的名字 更改过 use msdb go SP_CONFIGURE 'ALLOW UPDATES',1 RECONFIGURE WITH OVE ...
- python 发送带附件的 邮件
from email.MIMETextimportMIMETextfrom email.MIMEMultipartimportMIMEMultipartimport smtplib mail_host ...
- 用API处理位图
procedure TForm1.Button1Click(Sender: TObject); var dc : hdc; MemDc : hdc; MemBitmap : hBitmap; OldM ...
- Selection Tools
[Selection Tools] 1.Marquee Tools. OptionBar其中四年控件涵意如下: 2.Magnetic Lasso Tool,根据属标轨迹自动画点. 3.Magic Wa ...
- 并发之CAS无锁技术
CAS算法即是:Compare And Swap,比较并且替换: CAS算法存在着三个参数,内存值V,旧的预期值A,以及要更新的值B.当且仅当内存值V和预期值B相等的时候,才会将内存值 ...
- 交互原型画得丑?29个优秀UI/UX线框草图
现在越来越多UI设计师都需要画一些交互的线框图,然而作为视觉专家,当然要把它画得靓靓的嘛,是不是?所以很多用户会使用<5款高效的原型设计工具>来绘制,或者直接手绘更有逼格. 今天达人手工整 ...
- 无脑无负担网站架构-- Application Request Route的一些应用
首先作为一个.net 程序员,多数情况你懒的整什么架构啊.框架啊.还有那命令行的linux,别说linux也有桌面,那个桌面用起来更让人抓狂,一直不明白居然有人说喜欢上linux的命令行,装B还是SB ...
- select for update [nowait]
Syntax The NOWAIT and WAIT clauses let you tell the database how to proceed if the SELECT statement ...