博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
16级第三周寒假作业F题
阅读量:4538 次
发布时间:2019-06-08

本文共 2612 字,大约阅读时间需要 8 分钟。

Sliding Window

TimeLimit:12000MS  MemoryLimit:65536K
64-bit integer IO format:
%lld
Problem Description

Case Time Limit: 5000MS

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:

The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

本题数据极其巨大,可能要进行输出优化,否则会超时,具体代码可以参考下面

void Out(int a)    //输出一个整型

{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

Input
The input consists of two lines. The first line contains two integers
n and
k which are the lengths of the array and the sliding window. There are
n integers in the second line.
Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

SampleInput
8 31 3 -1 -3 5 3 6 7
SampleOutput
-1 -3 -3 -3 3 33 3 5 5 6 7 思路:首先这道题很坑,很坑,很坑;然后进入主题,我是用的单调队列,对于初始状态,我们可以加入k-1个元素,然后每次加一个元素,进行弹出和查询的操作。因为要处理最大值和最小值,所以我们就创建两条队列, 分别用来处理最大值和最小值,这里只需要说明一下最小值的队列就可以了,最大值队列同理。 假如当前我们要加入一个新的元素x,我们只需要将x与队尾的元素进行比较,如果我们发现x小于队尾的元素,那么显然队尾的那个元素是没有用的,可以直接删除。这个原理就是对于当前加入的x,接下来的所有询问中,有 队员元素在的询问一定有x在,也就是说无论如何队尾元素都不会是最小值,我们可以直接删除。 我们就可以发现这个优先队列的性质就是前面的数字总是小于后面的数字,所以查询的时候,最小的数字就是队首的元素; 接下来还有一个删除的操作,这个更加简单,我们只要判断队首的元素与当前加入的这个数的位置,看看差值是不是大于或等于k就可以决定这个数是不是需要删除了。 然后就是我的输入输出的优化,因为数据较大,优化一下免得超时; 具体的看我的代码;
1 int read(int &x) 2 { 3     char c=getchar(); 4     int f=1;x=0; 5     while(c<'0'||c>'9') 6     { 7         if(c=='-') 8             f=-f; 9         c=getchar();10     }11     while(c>='0'&&c<='9')12     {13         x=x*10+c-'0';14         c=getchar();15     }16     x*=f;17 }
 
void Out(int a)    //输出一个整型{    if(a<0)    {          putchar('-');          a=-a;    }    if(a>9)    Out(a/10);    putchar(a%10+'0');}
1 for(int i=1;i<=n;i++) 2     { 3         read(a[i]); 4         while(a[q1[T1]]>=a[i]&&T1>=H1) 5             T1--; 6         while (a[q2[T2]]<=a[i]&&T2>=H2) 7             T2--; 8         q1[++T1]=q2[++T2]=i; 9         if(q1[H1]+k<=i)10             H1++;11         if (q2[H2]+k<=i)12             H2++;13         ans1[i]=a[q1[H1]];14         ans2[i]=a[q2[H2]];15     }

 

 

转载于:https://www.cnblogs.com/tijie/p/6358849.html

你可能感兴趣的文章
input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法
查看>>
【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)...
查看>>
mysql 5.6 参数详解
查看>>
求旋转数组的最小元素
查看>>
Gson解析Json数组
查看>>
Lintcode: Fast Power
查看>>
Pocket Gem OA: Log Parser
查看>>
枚举也能直接转换为对应的数值输出
查看>>
angularjs1-7,供应商
查看>>
BitSet
查看>>
Spring常用注解,自动扫描装配Bean
查看>>
(转载)深入理解WeakHashmap
查看>>
JAVA中的数组
查看>>
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>