集训第四周(高效算法设计)K题 (滑窗问题)
UVA 11572 唯一的雪花
题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新
方法一:
#include"iostream"
#include"set"
#include"cstring"
#include"cstdio"
#include"algorithm"
using namespace std;
const int maxn=1000000+10;
set<int>book;
int a[maxn];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,t,sum=0,ans=0;
cin>>n;
book.clear();
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int r=0,l=0;
for(;r<n;)
{
while(!book.count(a[r])&&r<n)
{
book.insert(a[r]);
r++;
}
ans=max(ans,r-l);
book.erase(a[l]);
l++;
}
cout<<ans<<endl;
}
return 0;
}
方法二:
#include"iostream"
#include"set"
#include"cstring"
#include"cstdio"
#include"algorithm"
using namespace std;
const int maxn=1000000+10;
int pos[maxn];
int a[maxn];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,t,sum=0,ans=0;
cin>>n;
memset(pos,-1,sizeof(pos));
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int r=0,l=0;
a[n]=a[n-1];
for(int i=0;i<=n;i++)
{
if(pos[a[i]]>=l)
{
ans=max(ans,i-l);
l=pos[a[i]]+1;
}
pos[a[i]]=i;
}
cout<<ans<<endl;
}
return 0;
}
刘汝佳紫书上还提到过滑动窗口的最小值问题,其中使用到了单调队列,贴个地址去学习学习:
http://m.blog.csdn.net/blog/lx417147512/24916441
#include <stdio.h>
#define MAX 1000001
int n,k;
int pre1,pre2,lst1,lst2,len_max,len_min; // 两个队列的头指针与尾指针,最大值的下标,最小值的下表
int num[MAX],Increase[MAX],Decrease[MAX],Max[MAX],Min[MAX]; // Num存数据,递增与递减队列,最大值,最小值的数组。
// 递减序列的压入操作
void in_min(int i)
{
while( pre1<=lst1 && num[ Decrease[lst1] ]<num[i] ) //一直到寻找到比当前待插入值更大的位置,这是维护一个递减序列
--lst1;
Decrease[++lst1]=i;
// 如果大于等于k个数,就需要向最大值数组赋值
if( i>=k )
{
if(Decrease[pre1]<=i-k)
pre1++;
Max[len_max++]=num[ Decrease[pre1] ]; //当前滑动窗口的最大值即为递减序列的队头
}
}
// 递增序列的压入操作
void in_max(int i)
{
while( pre2<=lst2 && num[ Increase[lst2] ]>num[i] ) //一直到寻找到比当前待插入值更小的位置,这是维护一个递增序列
--lst2;
Increase[++lst2]=i;
// 如果大于等于k个数,就需要向最小值数组赋值
if( i>=k )
{
if(Increase[pre2]<=i-k)
pre2++;
Min[len_min++]=num[ Increase[pre2] ]; //当前滑动窗口的最小值即为递增序列的队头
}
}
int main()
{
int i;
while( ~scanf("%d%d",&n,&k) )
{
// 初始化
pre1=pre2=len_max=len_min=0;
lst1=lst2=-1;
// 读入数据
for(i=1;i<=n;++i)
{
scanf("%d",&num[i]);
in_max(i);
in_min(i);
}
// 输出数据
for( i=0;i<len_min-1;++i )
printf("%d ",Min[i]);
printf("%d\n",Min[len_min-1]);
for( i=0;i<len_max-1;++i )
printf("%d ",Max[i]);
printf("%d\n",Max[len_max-1]);
}
return 0;
}
集训第四周(高效算法设计)K题 (滑窗问题)的更多相关文章
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- 集训第四周(高效算法设计)C题 (二分查找优化题)
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
随机推荐
- (博弈论 高精度小数)51NOD 1185 威佐夫游戏 V2
有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...
- c语言程序设计案例教程(第2版)笔记(二)—函数、递归
零散知识点 模块化:将一个问题分解成若干个子问题的过程成为模块化. 模块化的优点:不但可以将一个复杂的问题分解成几个相对简单的问题:还可以提高程序代码的重用性. 函数:函数是构成C程序的基本单位.函数 ...
- python 匿名函数的使用(并没有那么简单)
以下为几种匿名函数的使用方式:x=[(lambda x:x**2)(x) for x in range(10)]print(x)y=[x**2 for x in range(10)]print(y)i ...
- /bin,/sbin,/usr/sbin,/usr/bin 目录之简单区别
/bin,/sbin,/usr/sbin,/usr/bin 目录 这些目录都是存放命令的,首先区别下/sbin和/bin: 从命令功能来看,/sbin 下的命令属于基本的系统命令,如shutdown, ...
- .net 发送邮件验证码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Ma ...
- CCF|跳一跳
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...
- Bmob使用心得
1.在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址,示例如下:(注意文字说明部分): allprojects { repositories { jcente ...
- Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
高阶函数Higher-orderfunction 变量可以指向函数 >>> abs #abs(-10)是函数调用,而abs是函数本身 <built-in function ab ...
- QPushButton注册事件过滤器后按钮消失
版权声明:本文为博主原创文章,转载需要注明出处. RT,代码如下: ui.btn_set->installEventFilter(this); bool MousrHoverTest::even ...
- 怎样在nexus 中 搜索到远程maven仓库中的jar 文件
怎样在nexus 中 搜索到远程maven仓库中的jar 文件 url: http://www.oschina.net/question/95712_21999 点击Administration菜单下 ...