今晚的校赛又告一段落啦,终于“开斋”了!

AC了两题,还算是满意的,英语还是硬伤。

来看题目吧!

B. Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got an array a, consisting of
n integers: a1, a2, ..., an. Your task is to find a minimal
by inclusion segment [l, r]
(1 ≤ l ≤ r ≤ n) such, that among numbers
al,  al + 1,  ...,  ar there are exactly
k distinct numbers.

Segment [l, r] (1 ≤ l ≤ r ≤ n;
l, r are integers) of length
m = r - l + 1, satisfying the given property, is called
minimal by inclusion, if there is no segment
[x, y] satisfying the property and less then
m in length, such that
1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment
[l, r] doesn't have to be minimal in length among all segments, satisfying the given property.

Input

The first line contains two space-separated integers:
n and k (1 ≤ n, k ≤ 105). The second line contains
n space-separated integers
a1, a2, ..., an — elements of the array
a (1 ≤ ai ≤ 105).

Output

Print a space-separated pair of integers
l and r (1 ≤ l ≤ r ≤ n) such, that the segment
[l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.

Sample test(s)
Input
4 2
1 2 2 3
Output
1 2
Input
8 3
1 1 2 2 3 3 4 5
Output
2 5
Input
7 4
4 7 7 4 7 4 7
Output
-1 -1
Note

In the first sample among numbers
a1 and a2 there are exactly two distinct numbers.

In the second sample segment
[2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.

In the third sample there is no segment with four distinct numbers.

题目理解的难点在于min区间的意思,所谓的min是不能在这个区间找到更小的满足。

这个区间的性质就是,恰好有k个不同的数字。

比如 n=5,k=3

1 1 2 3 4

满足区间性质的有: 1 1 2 3, 1 2 3,但是前者不是最小,因为其子区间1 2 3也满足。而1 2 3为最小的,因为没有其他子区间有三个不同数了。当然 2 3 4也是最小区间。

这样就有个问题了?怎么才叫最小呢?

比如 n=6, k=3,

1 1 2 1 2 3

我们的策略就是从第一个开始找,找到区间含有k个数,马上就break掉,记下当前满足k个不同的大区间(不一定是min)

之后我们就确定了一个区间[1,end],之后从end开始找,找到区间含有k个数,马上就break掉,记下当前满足k个不同的子区间开始beg.

那么此时[beg,end]一定是最优的。

为什么呢?

我们可以这样考虑,因为区间要求是连续的,而end是第k个数,我们不能缺少这个数,所以第一次确定了end后,右边界就确定了,之后往左走,同理,在beg找到第k个(这时候我们看end是第一个数啦),我们就不能缺少beg的数,区间就不能比[beg,end]更小了,所以的出来的结果就是最优的。

我的代码:

/*******************************************************************************/
/* OS : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
* Compiler : g++ (GCC) 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
* Encoding : UTF8
* Date : 2014-04-03
* All Rights Reserved by yaolong.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
using namespace std;
int mp[100005];
int main(){ int n,k,i;
int ind;
vector<int> a; while(cin>>n>>k){
memset(mp,0,sizeof(mp));
a.clear();
a.resize(n+1);
for(i=1;i<=n;i++)
cin>>a[i];
int cnt=0; int beg=0;
for(i=1;i<=n&&cnt<k;i++){ if(mp[a[i]]==0){
mp[a[i]]=1;
cnt++;
ind=i;
}else{ } } if(cnt!=k){
cout<<-1<<" "<<-1<<endl;
}else{
memset(mp,0,sizeof(mp));
for(i=ind;i>=1&&cnt>=0;i--){
if(mp[a[i]]==0){ mp[a[i]]=1;
cnt--;
beg=i;
} }
cout<<beg<<" "<<ind<<endl; } } return 0; }

SCNU省选校赛第二场B题题解的更多相关文章

  1. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  2. Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)

    Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...

  3. 2015 多校赛 第二场 1006 (hdu 5305)

    Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...

  4. 2015 多校赛 第二场 1004 hdu(5303)

    Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...

  5. 2015 多校赛 第二场 1002 (hdu 5301)

    Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...

  6. [NOI.AC]NOI2019省选模拟赛 第二场

    传送门 Solution A. 一共有\(T\)组数据 每次询问你\([l,r]\)中有多少个数能被他的所有数位整除(如果数位中含有\(0\)忽略掉) 数位dp,咕咕咕 B. 题面略 考虑一个个只有两 ...

  7. 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)

    参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...

  8. 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型

    题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...

  9. 训练赛第二场E题 Cottage Village

    题目大意:在一条X轴上,有若干个正方形,并且保证这些正方形的中心都在X轴上,然后输入n个正方形的中心的X坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...

随机推荐

  1. HDU-3622 Bomb Game 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3622 题意:一个平面上有很多的炸弹,每个炸弹的爆炸范围是一样的,求最大的爆炸范围使得炸弹之间不相互影响 ...

  2. Win8关机 一直重启的问题 解决方案-摘自网络

    win8关机时自动重启的解决方法:关闭快递启动,默认是启动的,到控制面板 – 电源选项 – 选择电源按钮的功能 – 更改当前不可用的设置 – 关机设置 – 把勾去掉 关闭快速启动.这样就不会关机一直重 ...

  3. 简洁 Abstract Factory模式(3.1)

    镇楼 在某些书中使用了二维图说明抽象工厂模式.非常好,可是yqj2065不喜欢他们的产品族/产品等级,改成品牌/产品类型. 抽象工厂模式(abstract factory pattern)较工厂方法模 ...

  4. cocos2d-x jsb 防止触摸事件传递

    在游戏中要实现消息弹窗,让用户点击确认,其他区域产生遮罩,阻挡下层的事件被点击到,这是个很常用的功能,在cocos2d-x中,可以通过为layer添加事件代理来实现: pDirector->ge ...

  5. linux Page cache和buffer cache----- systemtap

    http://shixm.iteye.com/blog/1724718 http://blog.csdn.net/dianhuiren/article/details/7543886

  6. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...

  7. redis持久化(摘录)

    redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持两种持久化方式,一种是 Snapshotting(快照)也是默认方式,另一种是Ap ...

  8. iOS之核心动画(Core Animation)

      Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core ...

  9. Nexus搭建Maven服务器

    参考:http://blog.csdn.net/ichsonx/article/details/14642897 1. 为什么使用Nexus 如果没有私服,我们所需的所有构件都需要通过maven的中央 ...

  10. hibernate简单应用

    Hibernate 第二章 核心API 和CURD实现 核心API介绍 单表操作方法 查询hql语句 1.认识hibernate 核心思想ORM:实现数据库中的文件一一映射为java中的实体类. 使用 ...