1117: Ready to declare

时间限制: 1 Sec  内存限制: 128 MB

提交: 358  解决: 41

[提交][状态][

pid=1117" style="color:rgb(26,92,200); text-decoration:none">讨论版]

题目描写叙述

Finally, you find the most good-looking girl...
You are going to write a letter to her. But you are not convident to be better than other boys. So you think you need a good chance...
You have known that these days, she will meet N boys at all(N<=100000). The ith boy has a handsome value V[i](0<V[i]<10000). She will meet K(K<=N) boys at the same time, and when the first boy leave, the next boy join them. It is to say that she will meet
first K boys in the list at first, then the first boy leave ,the k+1-th boy join. So she will meet boys N-K+1 times.
You must want to join she and them when these boys are not handsome, so you need to calculate each meet's the least handsome value and the most.
here is a sample when N=8,K=3
boys                least     most
[5 3 2] 1 1 10 2 3 25
5 [3 2 1] 1 10 2 3 13
5 3 [2 1 1] 10 2 3 12
5 3 2 [1 1 10] 2 3 110
5 3 2 1 [1 10 2] 3 110
5 3 2 1 1 [10 2 3] 210

输入

There are several test cases, each case contains two lines.
The first line is two number ,N K.
The second line has N number, the handsome value.

输出

Each case output two lines.The first line is each meet's min handsome value, the second line is each meet's max handsome value.

例子输入

8 3
5 3 2 1 1 10 2 3

例子输出

2 1 1 1 1 2
5 3 2 10 10 10

提示

来源

尽管正解是单调队列。但是我用的是两个栈实现的队列水过的。存下代码

/*************************************************************************
> File Name: Euler.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年10月30日 星期四 11时19分11秒
************************************************************************/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
int stack1[maxn],stack2[maxn],max2[maxn],max1,min2[maxn],min1;
int top1,top2;
void init(){
top1=top2=0;
max1=max2[top2]=0;
min1=min2[top2]=maxn;
}
int ans1[maxn],ans2[maxn];
void deque(){ if(top2>0){
stack2[--top2];
return ;
}
while(top1>0){
stack2[top2]=stack1[--top1];
max2[top2]=max(top2>0?max2[top2-1]:0,stack2[top2]);
min2[top2]=min(top2>0?min2[top2-1]:maxn,stack2[top2]);
top2++;
}
max1=0;
min1=maxn;
--top2;
}
int get_max_min(int x){
if(x)return max(max1,top2>0?max2[top2-1]:0);
return min(min1,top2>0?min2[top2-1]:maxn);
}
void push(int x){
max1=max(x,max1);
min1=min(x,min1);
stack1[top1++]=x;
}
int main()
{
int n,k,x;
while(~scanf("%d%d",&n,&k)){
init();
int t=0,cnt=0;
for(int i=1;i<=n;i++){
scanf("%d",&x);
push(x);
++cnt;
if(cnt==k)
{
ans1[t]=get_max_min(0);
//cout<<"sldl"<<endl;
ans2[t++]=get_max_min(1);
deque();
cnt--;
}
}
for(int i=0;i<t;i++)printf("%d%c",ans1[i],i==t-1?'\n':' ');
for(int i=0;i<t;i++)printf("%d%c",ans2[i],i==t-1?'\n':' ');
}
return 0;
}

补上单调队列版

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn =1e5 + 10;
struct Queue
{
int value;
int index;
};
Queue min_que[maxn],max_que[maxn];
int n,k,num[maxn],front,rear;
int delete_rear_inc(int f,int r,int d)
{
int mid;
while(f<=r){
mid=(f+r)/2;
if(min_que[mid].value==d) return mid;
if(min_que[mid].value>d) r=mid-1;
else f=mid+1;
}
return f;
}
int delete_rear_dec(int f,int r,int d)
{
int mid;
while(f<=r){
mid=(f+r)/2;
if(max_que[mid].value==d) return mid;
if(max_que[mid].value>d) f=mid+1;
else r=mid-1;
}
return f;
}
int main()
{ while(~scanf("%d%d",&n,&k)){ for(int i=1;i<=n;i++)scanf("%d",&num[i]); min_que[1].value=num[1];
front=rear=min_que[1].index=1; for(int i=2;i<=k;i++) {
rear=delete_rear_inc(front,rear,num[i]);
min_que[rear].value=num[i];
min_que[rear].index=i;
} printf("%d",min_que[1].value);
for(int i=k+1;i<=n;i++) {
rear=delete_rear_inc(front,rear,num[i]);
min_que[rear].value=num[i];
min_que[rear].index=i;
if(i-min_que[front].index>=k) front++;
printf(" %d",min_que[front].value);
if(i==n)puts("");
} max_que[1].value=num[1];
front=rear=max_que[1].index=1; for(int i=2;i<=k;i++) {
rear=delete_rear_dec(front,rear,num[i]);
max_que[rear].value=num[i];
max_que[rear].index=i;
} printf("%d",max_que[1].value);
for(int i=k+1;i<=n;i++) {
rear=delete_rear_dec(front,rear,num[i]);
max_que[rear].value=num[i];
max_que[rear].index=i;
if(i-max_que[front].index>=k) front++;
printf(" %d",max_que[front].value);
if(i==n)puts("");
}
}
return 0;
}

NEUOJ 1117: Ready to declare(单调队列)的更多相关文章

  1. 洛谷P3975 跳房子 [DP,单调队列优化,二分答案]

    题目传送门 跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一 ...

  2. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  3. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  4. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  5. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  6. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  7. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  8. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  9. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

随机推荐

  1. 【C#学习笔记】写文件

    using System; using System.IO; namespace ConsoleApplication { class Program { static void Main(strin ...

  2. Live555中RTP包的打包与发送过程分析

    这里主要分析一下,live555中关于RTP打包发送的部分.在处理完PLAY命令之后,就开始发送RTP数据包了(其实在发送PLAY命令的response包之前,就会发送一个RTP包,这里传输就已经开始 ...

  3. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

  4. 30道Linux面试题

    1.linux如何挂在windows下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1234 ...

  5. 安装服务Memcached+Nginx+Php linux下安装

    Memcached安装 1.      源码安装libevent(下载地址:http://monkey.org/~provos/libevent/) 2.      源码安装memcached(下载地 ...

  6. Java中的面向接口编程

    面向接口编程是很多软件架构设计理论都倡导的编程方式,学习Java自然少不了这一部分,下面是我在学习过程中整理出来的关于如何在Java中实现面向接口编程的知识.分享出来,有不对之处还请大家指正. 接口体 ...

  7. Android中FragmentPagerAdapter对Fragment的缓存(二)

    上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...

  8. Ejabberd源码解析前奏--管理

    一.ejabberdctl 使用ejabberdctl命令行管理脚本,你可以执行ejabberdctl命令和一些普通的ejabberd命令(后面会详细解说).这意味着你可以在一个本地或远程ejabbe ...

  9. [Everyday Mathematics]20150116

    设 $\al_n\geq 0$ 且 $\dps{\vlm{n}\al_n=0}$, 试求 $$\bex \vlm{n}\frac{1}{n}\sum_{k=1}^n \ln\sex{\frac{k}{ ...

  10. 浅析基层检察院派驻乡镇检察室的健康发展 z

    时间:2011-03-22 10:08 作者:祝志方 新闻来源:正义网 一.前言 在我国,基层检察院派驻乡镇检察室的发展经过了一个曲折发展的历程,上世纪80年代,随着经济社会的发展,一批乡镇检察室应运 ...