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坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...
随机推荐
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- HTML对JSON的操作
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- eclipse 不能切换输入法
按了Alt+Shift键?再按一次把EN切换成CN,然后再Ctrl+Shift就可以切换输入法
- android 控件花屏问题
发现自己的手机上某个界面出现了花屏,某些控件背景被拉伸过多遮住了其他控件,很难看.这种现象高概率出现,分析了下发现:一旦发生这种现象,必然 会打印下面这种log,google了下,这种log应该是硬件 ...
- Spring AOP切面
在软件开发中,分布于应用多出的功能被称为和横切关注点. 通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(可是往往直接嵌入到应用的业务逻辑中).将这些横切关注点与业务逻辑相分离正是面向切面编成( ...
- mongdb使用场景
你期望一个更高的写负载 默认情况下,对比事务安全,MongoDB更关注高的插入速度.如果你需要加载大量低价值的业务数据,那么MongoDB将很适合你的用例.但是必须避免在要求高事务安全的情景下使用Mo ...
- Ubuntu设置目录的读写权限(Linux命令chmod 777 dirName)
更改文件所有者 sudo chown system_username /location_of_files_or_folders 更改文件的权限 鼠标右按钮点击文件/目录 -> 属性 权限 分页 ...
- crash recovery
2016-07-02 17:56:07 5772 [Note] InnoDB: Database was not shutdown normally!2016-07-02 17:56:07 5772 ...
- systemtap 列出所有linux 内核模块与相关函数2
[root@localhost src]# uname -aLinux localhost.localdomain 2.6.32 #1 SMP Sun Sep 20 18:58:21 PDT 2015 ...
- MyJFrame(文本)界面的建立
import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout ...