SCNU省选校赛第二场B题题解
今晚的校赛又告一段落啦,终于“开斋”了!
AC了两题,还算是满意的,英语还是硬伤。
来看题目吧!
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.
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).
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.
4 2
1 2 2 3
1 2
8 3
1 1 2 2 3 3 4 5
2 5
7 4
4 7 7 4 7 4 7
-1 -1
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题题解的更多相关文章
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
- Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)
Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...
- 2015 多校赛 第二场 1006 (hdu 5305)
Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...
- 2015 多校赛 第二场 1004 hdu(5303)
Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...
- 2015 多校赛 第二场 1002 (hdu 5301)
Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...
- [NOI.AC]NOI2019省选模拟赛 第二场
传送门 Solution A. 一共有\(T\)组数据 每次询问你\([l,r]\)中有多少个数能被他的所有数位整除(如果数位中含有\(0\)忽略掉) 数位dp,咕咕咕 B. 题面略 考虑一个个只有两 ...
- 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)
参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...
- 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型
题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...
- 训练赛第二场E题 Cottage Village
题目大意:在一条X轴上,有若干个正方形,并且保证这些正方形的中心都在X轴上,然后输入n个正方形的中心的X坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...
随机推荐
- [原]ubuntu下制作ubuntu源
ubuntu下可以用debmirror来下载ubuntu的所有源: 配置ubuntu12.04_mirror.sh ########################################## ...
- 【转】终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件
感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...
- PC-经典之“运行里面的密密”
msconfig.exe 你自己往里面输入这个字母就可以看到了,试试看,还有,我这里有一些可以在"运行"栏里输入的命令,一并给你: 以下为Windows操作系统的常用运行命令,执行 ...
- EF搜索数据自动将表名变复数问题
原因这个是自己生成的需要在model加Table 其他博主写了aweier2011
- ajaxfileUpload ajax 上传图片使用
前台html: <div class="b-mg15 img-text" room_id="<?= $items['id'] ?>"> ...
- Unity4向上(Unity5)兼容PlayerPrefs的数据存储
好久没有写项目.开发相关的内容了,刚好最近在做项目的更新时,遇到一个比较有意思的坑就随手记录一下. 因为项目的上一上线版本是由Unity5.3发的包,而最新的项目来不及同步更新到5.3版本发包测试,所 ...
- 哪里有比较全的hadoop视频教程
robby老师讲了套hadoop视频,讲的比的深入浅出,内容很丰富,把网盘下载地址提供给大家一下: 视频下载啦很大,有图有真相: 1,Hadoop介绍,HDFS和MapReduce工作原理:http: ...
- OpenCV2马拉松第22圈——Hough变换直线检測原理与实现
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...
- Linux环境进程间通信
http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html http://bbs.chinaunix.net/forum.ph ...
- 在其它路径新建cocos2d-x项目
打开vs2010程序,然后选择“文件—新建—项目”,如图 改了一下位置,放在D:\Program Files\cocos2d-x\ ,确定 然后点下一步 这个程序不需要物理引擎,所以可以把上面红色的圈 ...