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坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...
随机推荐
- HW3.25
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW2.16
import java.util.Scanner; public class Solution { public static void main(String[] args) { final int ...
- Codeforces182D - Common Divisors(KMP)
题目大意 如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数 题解 如果它们有公共因子,那么显然它们的最小公共因子肯定是 ...
- (android 源码下开发应用程序) 如何在 Android 各 level ( 包含 user space 與 kernel space ) 使用dump call stack的方法
http://janbarry0914.blogspot.com/2014/07/androiddump-call-stack.html dump call stack [文章重點] 了解 Andro ...
- php连mssql中文乱码问题
我在将一个aspx+mssql的系统做成php+mssql的系统时,感觉架构大不一样,aspx多是aspx页面+aspx.cs后台协同开发,多用可视化空间开发,而php我则选用了smarty模板,感觉 ...
- [转帖] 安装Eclipse插件长时间卡在 calculating requirements and dependencies
把"Contact all update sites during install to find required software"前面的勾去掉,然后点击下一步,这样之后问题迎 ...
- Dreamweaver中清除php代码中多余空行的方法
使用DW自带的搜索功能,利用正则表达式 使用正则表达式搜索:\r\n\s*\r\n即可搜到代码中的空行,再用回车符\n替换即可消除代码中的多余空行
- webstrom热键[持续更新]
1.Ctrl+ Shift + A -- 为了加快寻找菜单命令或工具栏操作,你并不需要看菜单.只有按Ctrl+ Shift + A(说明|查找操作主菜单上),并开始输入动作的名称. . 2.Ctr ...
- [译]信仰是怎样毁掉程序猿的How religion destroys programmers
作者原文地址 作者John Sonmez 英文水平不够高,翻译不太准确. 翻译地址:译文 尽管文章是13年的,可是这段时间恰好看到.net开源核心之后,各种java和.net掐架. 语言之争有些牵涉到 ...
- MYSQL----myownstars(102)
http://blog.itpub.net/15480802/cid-84815-list-1/