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坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...
随机推荐
- 【Mysql学习笔记】浅析mysql的binlog
最近读一份关于“数据库事务故障恢复"的技术资料,发现对mysql的binlog的认识不够清楚,查阅mysql reference manual有所收获,作为笔记,记录于此. 1. What' ...
- 恼人的Visual Studio 2010崩溃重启问题
上周时Visual Studio 2010突然出现崩溃现象.在源文件修改只要一编译,马上就崩溃,而且还不弹出任何异常窗口,严重影响软件开发工作. google了无数解决方案 试了下面这些方法: 1)h ...
- 版本控制Subversion TortoiseSVN apache VisualSVN笔记(转载)
转载于http://blog.sina.com.cn/s/blog_6b94d5680101m7ah.html Subversion(简称svn)是近年来崛起的版本管理软件,是cvs的接班人.目前,绝 ...
- uedit富文本编辑器
问题: 1: An error occurred at line: 6 in the generated java fileOnly a type can be imported. com.baidu ...
- Apache CXF框架结构和基本原理(转)
原文链接:http://blog.sina.com.cn/s/blog_6182547f01017pak.html CXF旨在为服务创建必要的基础设施,它的整体架构主要由以下几个部分组成: 1.Bus ...
- WebSerivce测试工具 Storm
STORM 是一款免费且开源的WebSerivce测试工具 它的功能: 1,测试任意语言测试 WebService 2,可动态调用webservice的方法,即使输入参数需要使用复杂的数据类型 3,节 ...
- Windows, Eclipse下开发Heritrix 3.1 (一)环境搭建
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
- iOS 推送,当接到推送消息时如何处理?
接收到通知时有两种进入的方式:1.当app未运行时(BOOL)application:(UIApplication *)application didFinishLaunchingWithOption ...