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> ...
随机推荐
- Pthreads 读写锁
▶ 使用读写锁来限制同一数据多线程读写.若任何线程拥有读锁,则其他任何请求写锁的线程将阻塞在其写锁函数的调用上:若任何线程拥有写锁,则其他任何请求读锁和写锁的线程将阻塞在其对应的锁函数上,相当于将读与 ...
- Spring Boot自动配置
Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...
- 可视化库-Matplotlib-散点图(第四天)
1. 画基本的散点图 plt.scatterdata[:, 0], data[:, 1], marker='o', color='r', label='class1', alpha=0.4) np.r ...
- c# 之 new 关键字
1.实例化变量 DataTable dt = new DataTable(); 2.调用构造函数 class CoOrds { public int x, y; // 实例构造函数(默认构造函数) ...
- asp.net 初级程序员面试题【待续】
C# 常见的排序方式 冒泡排序(Bubble sort) 堆排序(Heap sort) 插入排序(Insertion sort) 归并排序(Merge sort) 快速排序(Quick sort) ...
- 通过nginx + lua来统计nginx上的监控网络请求和性能
介绍 以前我们为nginx做统计,都是通过对日志的分析来完成.比较麻烦,现在基于ngx_lua插件,开发了实时统计站点状态的脚本,解放生产力. 项目主页: https://github.com/sky ...
- k8s问题记录
1. kubectl run 起来的pod 用 kubectl delete po删不掉 kubectl delete deployment my-nginx kubelet# 看到最后一行:erro ...
- readonly const
readonly:只读域,只能在初始化--声明初始化或构造器初始化--的过程中赋值,其他地方不能进行对只读域的赋值操作,否则编译器会报错.只读域可以是实例域也可以是静态域.只读域的类型可以是C#语言的 ...
- nyoj36-最长公共子序列 (LCS)
http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...