技巧题---Single boy
Description
Today is Christmas day. There are n single boys standing in a line. They are numbered form 1 to n from left to right. The i-th single boy has ai single strength. They are singing single boy Christmas song! Single boy,single boy, single all the way, having party together and turning into gay! Hey!
A group of single boys is non-empty contiguous segment of the line. The size of a group is the number of single boys in that group. The single strength of a group is the minimum single strength of the single boy in that group.
Now we want to know for each x such that 1<=x<=n the maximum single strength among all groups of size x.
Input
The first line of input contains T(<=30), the test cases.
For each test case, the first line is a integer n(1 <= n <= 2*10^5), the number of single boys.
The second line contains n integers [1,10^9] separated by space, the single strength of each single boy.
Output
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
Sample Input
1
10
1 2 9 3 8 2 2 10 19 6
Sample Output
19 10 6 2 2 2 2 2 2 1
题意:n个数,(1<=k<=n),在n个数中连续的k个数为一个区间长度,一共有n-k+1个区间,
每个区间选出区间中的最小值min[j](1<=j<=n-k+1),然后在n-k+1个区间中选出最大的
min[j],得到max[i](1<=i<=k),然后按k=1~n顺序输出max[i].
由于数据太大,暴力模拟寻找太慢不能实现,所以我们想到的是求 以num[i]为最小值
的区间长度,然后选择区间长度相等Max(num[i]),开始我用了暴力寻找区间,由于数据
太大,TLE了,后来学习别人方法,用栈求得区间长度,我们把以num[i]为最小值的区间
的左右边界求出来,右边界-左边界就是区间长度;
用栈求区间左右边界:先求左边界后求右边界,开始将下标0入栈,我们的数组是从1
开始的,然后比较以栈顶元素为下标的值是否大于等于当前值,是的话出栈,直到比当前
值小,当前值的左边界就等于当前下标-栈顶元素+1,最后每次都要把当前下标入栈,
求右边界完全一样。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
const int maxn=;
struct ac
{
int num;
int l,r;
}d[maxn];
int ans[maxn];
stack<int>sta;
int main()
{
int t,n,Top,dn;
scanf("%d",&t);
while(t--)
{
int flag=;
scanf("%d",&n);
d[].num=;///以0 ,n+1为下标的值为0.
d[n+].num=; sta.push();///压入0下标
for(int i=; i<=n; i++)
{
scanf("%d",&d[i].num);
while(!sta.empty())///求左边界
{
Top=sta.top();
if(d[Top].num>=d[i].num)///比较当前值与以栈顶元素为下标的值
sta.pop();
else
break;
}
Top=sta.top();
d[i].l=Top+;///左边界
sta.push(i);///每次压入当前下标
}
while(!sta.empty())
sta.pop(); sta.push(n+);///求右边界,压入下标n+1
for(int i=n; i>=; i--)
{
while(!sta.empty())
{
Top=sta.top();
if(d[Top].num>=d[i].num)
sta.pop();
else
break;
}
Top=sta.top();
d[i].r=Top-;
sta.push(i);
}
while(!sta.empty())
sta.pop();
memset(ans,,sizeof(ans));
for(int i=; i<=n; i++)///求ans,每次都更新 以区间长度为dn的最大值
{
dn=d[i].r-d[i].l+;
ans[dn]=max(ans[dn],d[i].num);
}
for(int i=n-;i>=;i--)///有些区间长度是找不到,那么用比他区间长度大的更新其值
{
if(ans[i]<ans[i+])
ans[i]=ans[i+];
} printf("%d",ans[]);
for(int i=; i<=n; i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}
样例值 1 2 9 3 8 2 2 10 19 6
以该值为最
小值的区间长度 10 9 1 3 1 9 9 2 1 3
区间长度为 10有1 ,max[10]=1;
区间长度为 9 有2,2,2,max[9]=2;
区间长度为 4-8 都没有,即为0,那么以区间为长度为9的更新max[4-8]=2;
区间长度为 3有 3,6, max[3]=6;
区间长度为 2有 10, max[2]=10;
区间长度为 1有 9,8,19,max[1]=19;
为什么以区间更大的修改,以为区间越大,数值越小,区间小的的最大值不可能大于
区间大的最大值
技巧题---Single boy的更多相关文章
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- OCP 认证考试报名费技巧题库051052053解析合格线
本人于2017年4月22日通过参加OCP考试,第一次参加,一天之内考了三门,三门一次性通过,052 - 95% ,053 - 86% ,051 - 100% 一.关于考试考试报名费: 052:158$ ...
- LeetCode算法题-Single Number(Java实现)
这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...
- 【LOJ6043】「雅礼集训 2017 Day7」蛐蛐国的修墙方案(搜索技巧题)
点此看题面 大致题意: 给你一个长度为\(n\)的排列\(p\),要求构造一个合法的括号序列,使得如果第\(i\)个位置是左括号,则第\(p_i\)个位置一定是右括号. 暴搜 很容易想出一个暴搜. 即 ...
- POJ 2229 Sumsets(技巧题, 背包变形)
discuss 看到有人讲完全背包可以过, 假如我自己做的话, 也只能想到完全背包了 思路: 1. 当 n 为奇数时, f[n] = f[n-1], 因为只需在所有的序列前添加一个 1 即可, 所有的 ...
- 【动态规划技巧题】POJ2229-Sumsets
[题目大意] 把一个数n分成2的指数幂相加的形式,问有几种情况. [思路] 如果当前i为奇数,则必定有至少一个1,可以看作i-1的情形再加上一个1.即f[i]=f[i-1]. 如果当前i为偶数,假设没 ...
- FZU 1922——非主流——————【技巧题】
非主流 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- HDU 5288——OO’s Sequence——————【技巧题】
OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- nyoj 1205——简单问题——————【技巧题】
简单问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给你一个n*m的矩阵,其中的元素每一行从左到右按递增顺序排序,每一列从上到下按递增顺序排序,然后给你一些数x ...
随机推荐
- QTP对象管理
QTP对象库管理 - 动态绑定对象库文件:http://blog.csdn.net/testing_is_ ... le/details/20569843 用ObjectRepositoryUtil动 ...
- git Please move or remove them before you can merge. 错误解决方案
git pull 时 往往会遇到各种各样的问题 ,下面是常遇到的一种状况 Updating 7c9e086..936acacerror: The following untracked working ...
- php常见小知识总结
1.如果在函数中 unset()一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调unset() 之前一样的值.如果是想把全局变量清空,用$var = array(),注意在他之前别用u ...
- UML3
在UML系统开发中有三个主要的模型: 功能模型: 从用户的角度展示系统的功能,包括用例图. 对象模型: 采用对象,属性,操作,关联等概念展示系统的结构和基础,包括类图. 动态模型: 展现系统的内部行为 ...
- APP-BOM-20516 错误处理一例
昨天在处理一个工单异常时,需要将一个Released的工单改为Unreleased状态,程序报APP-BOM-20516错误,如下图.百度只搜到两条记录,均无用.Google能搜到的多一些,也无用.进 ...
- SLAM中的EKF,UKF,PF原理简介
这是我在知乎上问题写的答案,修改了一下排版,转到博客里. 原问题: 能否简单并且易懂地介绍一下多个基于滤波方法的SLAM算法原理? 目前SLAM后端都开始用优化的方法来做,题主想要了解一下之前基于 ...
- ping: sendto: Network is unreachable
在我的板子上ping路由上的IP的时候可以ping通,但是ping外网的IP的时候提示"ping: sendto: Network is unreachable" 后来使用rout ...
- CSS之边框覆盖
今天想做一个淘宝导航来练练手,遇到了边框覆盖的问题.如下图: li的红色边框盖不住该灰色边框.后来问经验人士告诉我,这种边框覆盖是会出现无法100%保证正常的情况,遂得到如下3中解决方案: 1.以后遇 ...
- C++ string 用法详解
/////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者 ...
- [C++] memset 和sizeof 的使用注意
因为使用C++写小题目时经常需要清除数组,这里记录下Memset函数的sizeof运算符的使用注意. memset的特点是:将给定地址后连续的内存(包括给定地址),逐个byte初始化为参数中指明的值. ...