CodeForces - 616D Longest k-Good Segment (尺取法)

程序员的自我修养 一个好的程序员的自我修养需要从不同角度出发,工作能力是一个方面,对自身的定位和了解也是一个方面,程序员的工作能力修养包括需求理解、功能设计、沟通确认、代码规范、成果验证、代码重构优化等,通过自我剖析来对自身进行定位并了解自身的优缺点,如去除劣根性、端正意识形态与态度、提升自己的核心竞争力等。 自我剖析 一个成功的人,必须要先了解自己,只有了解自己是什么样的人,了解自身有哪些优缺点,了解自己更合适做什么工作,知道自己做什么才能发挥出自身的优势,这样才能使自己在工作中发光发热,在职业生涯的道路上走的更远。 阅读详情

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.

Find any longest k-good segment.

As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k.

The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a.

Output

Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right.

Example
Input
5 5
1 2 3 4 5
Output
1 5
Input
9 3
6 5 1 2 3 2 1 4 5
Output
3 7
Input
3 1
1 2 3
Output
1 1

题目大意就是给一个数组,找到最长的区间,使得不同元素的个数为k

今天才知道尺取法是什么意思,“尺”就是一个不定长的区间,然后一直取,取到取不下区间起点就向左移,还能取就区间终点向后移

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int num[1000005];
int vis[1000005];
int main()
{
    int n,k;
    int sta;
    while(~scanf("%d%d",&n,&k))
    {
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)  scanf("%d",&num[i]);
        int cnt=0;
        int maxlen=0;
        int tempsta=1;
        int i=1;
        while(i<=n&&tempsta<=n-k+1)
        {
            while(cnt<=k&&i<=n)
            {
                if(!vis[num[i]])
                   cnt++;
                vis[num[i]]++;
                i++;
            }
            i--;
            vis[num[i]]--;
            cnt--;
            if(cnt<k)
                i++;
            if(maxlen<i-tempsta)
            {
                sta=tempsta;
                maxlen=i-tempsta;
            }
            if(cnt<k)
                i--;
            vis[num[tempsta]]--;
            if(vis[num[tempsta]]==0)
                cnt--;
            tempsta++;
        }
        printf("%d %d\n",sta,sta+maxlen-1);
    }
     return 0;
}





MuJoCo 入门教程(二) 本章介绍 MuJoCo 的数学和算法基础。对于熟悉广义或关节坐标建模和仿真的读者来说,整体框架是相当标准的。因此,我们将对这些材料进行简要总结。本章的大部分篇幅用于介绍我们如何处理接触和其他约束条件。这种方法是基于我们最近的研究,也是 MuJoCo 所独有的,因此我们花时间对其进行了详细的激励和解释。更多信息可参见下面的论文,尽管本章中的一些技术思想是新的,在其他地方还没有描述过。 阅读详情

相关推荐

【SystemVerilog笔记(一)】数据类型

(一)内建数据类型 1.按照四值逻辑和二值逻辑分类 四值逻辑类型:integer、logic、reg、wire等线网类型 二值逻辑类型:byte、shortint、int、longint、bit 2.按照有符号和无符号分类 有符号类型:byte、shortint、int、longint、integer 无符号类型:bit、logic、reg、wire等线网类型 ※记忆:按位定义的变量是无符号的。 (二)软件常用类型 1.定宽数组 在这里插入代码片 ...

cc__cc__的博客 1万+

CF616D Longest k-Good Segment

题目描述 给定一个包含\(n\)个整数的序列\(a\),\(0\le a_i \le 10^6\),询问不重复数字个数\(\le k\)的最长区间的左右端点。如果有多解输出任意一组。 输入输出格式 输入格式: The first line contains two integers \(n,k\) ( \(1 \leq k \leq n \leq 5·10^{5}\)) — the number ...

weixin_30885111的博客 155

企业多部门网络隔离实战:用华为交换机VLAN+Trunk实现行政/技术部安全隔离

本文详细介绍了如何利用华为交换机的VLAN划分与Trunk链路技术,实现企业行政与技术部门间的网络逻辑隔离。通过具体的配置案例,阐述了Access与Trunk端口的工作原理、单交换机与多交换机环境下的部署策略,并延伸至三层网关部署与ACL访问控制,为企业构建安全、高效的核心网络提供实战指导。

p4q5r6s7t的博客 577

CF616

比赛考到的知识点: 字符串, 数组, 比较大小, 搜索, 桶计数, 尺取法, 数论, 分块

chenboshuai的博客 322

【已验证】基于STM32F407与AD9833的幅频特性测试装置设计与实现

本文详细介绍了基于STM32F407微控制器和AD9833 DDS芯片的低成本幅频特性测试装置设计与实现方法。通过扫频原理,该装置能自动生成正弦波信号,测量滤波器等电路的输出幅度变化,并在LCD屏幕或示波器上绘制幅频特性曲线。文章提供了从硬件选型、CubeMX工程配置、关键驱动代码编写到数据处理与可视化的完整教程,帮助电子爱好者与工程师快速搭建实用的滤波器测试工具。

weixin_35649491的博客 119

服务器被黑?快速检测和识别系统中的恶意进程

在管理服务器时,检测和识别系统中的恶意进程至关重要。本文介绍了使用 `ps`、`top`、`htop` 等系统监控工具,分析日志文件,以及使用 `chkrootkit` 和 `rkhunter` 等安全工具的方法。通过这些步骤,可以有效地检测和处理恶意进程,确保系统的安全和稳定。

_midnight的博客 1412

Codeforces 616D Longest k-Good Segment(双指针)

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.

ZAQ的博客 1251

Windows远程桌面开发之九-虚拟显示器(Windows 10 Indirect Display 虚拟显示器驱动开发)

by fanxiushu 2019-06-24 转载或引用请注明原始作者。 这里与远程桌面关系不是太大,但这个部分是xdisp_virt远程控制程序的实现多显示器桌面扩展的子功能,因此也归为远程桌面开发一类。 这篇文章与之前发布的...

fanxiushu的专栏 2万+

CodeForces - 616D Longest k-Good Segment

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find...

~ 636

PX4从放弃到精通(三):使用qt编译PX4

1 下载px4源码 2在Firmware所在目录下执行 mkdir Firmware-build cd Firmware-build cmake …/Firmware -G “CodeBlocks - Unix Makefiles” -DCONFIG=nuttx_px4_fmu-v5_default 第三步最后的px4_fmu-v5_default根据自己的情况修改 执行完后如图 打开QT 点击打开工程 选择源码的CmakeLists.txt文件 选择imported Kit 打开项目那一栏,构建目

超维空间科技的博客 1万+

YOLO26改进策略【Neck】| arXiv 2025 分层多头注意力HMHA 子空间拆分提多样 + 通道重排降冗余,强化目标检测精度

HMHA(Hierarchical Multi-Head Attention,分层多头注意力)旨在解决传统多头注意力(MHA)在图像恢复任务中的核心缺陷——特征冗余。传统MHA将通道均匀拆分给各个头,导致不同头学习的子空间信息相似,进而出现多头关注同一区域、忽略退化区域修复的问题,限制了模型的特征表达能力。HMHA通过“分层子空间拆分+通道重排序”策略,让每个头学习差异化、互补的上下文特征,从根源上缓解冗余,提升图像恢复任务中对复杂退化场景的特征捕捉能力。

Limiiiing的博客 208

CodeForces - 616D Longest k-Good Segment 尺取

题意: 给定长度为n的序列,找一个最长的区间,使得其中不同的数字的个数不大于K 思路: 尺取 #include #include #include #include #include #include #include #include #include #include #include #define PI acos(-1.0) #define in freopen("

床前明月光 311

语言模型推理能力与计算表达力深度解析

语言模型的推理能力本质上是一种知识迁移与泛化应用的过程,其核心在于Transformer架构中的自注意力机制和前馈网络的协同作用。计算表达力作为衡量模型能力的关键指标,涉及函数逼近、组合泛化和上下文理解等多个维度。随着模型规模的扩大,涌现能力现象揭示了参数规模与推理能力之间的非线性关系。在实际应用中,通过提示工程和微调策略可以有效激活和优化模型的推理表现。当前研究正探索模块化架构和世界模型整合等方向,以突破长程推理和因果关系建模等前沿挑战。本文深入探讨了语言模型推理泛化的技术原理与实践方法,为理解计算表达力

weixin_42521032的博客 341

Longest k-Good Segment CodeForces - 616D(尺取法)

The array a with n integers is given. Let’s call the sequence of one or more consecutive elements in a segment. Also let’s call the segment k-good if it contains no more than k different values. Find ...

starlet_kiss的博客 261

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

codeforces 616D Longest k-Good Segment 尺取法

传送门:Longest k-Good Segment题目大意输入n和k 然后第二行输入n个整数值 最后输出含有k个不同整数最长连续的子序列的两个下标解题思路尺取法 如果每次不同整数的数量大于k的话就减去前面的那一个AC代码#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace

woshinannan的专栏 468

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

codeforces_616D. Longest k-Good Segment(尺取法)

D. Longest k-Good Segment time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The array a with n integers is g

凳子 803

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Longest k-Good Segment CodeForces - 616D (尺取法)

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.Find an

Hanylch 408

Longest k-Good Segment CodeForces - 616D ( 尺取法)

Longest k-Good Segment   The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more tha...

SEVENY_的博客 395

CodeForces - 616D D. Longest k-Good Segment(尺取法)

题目链接: CodeForces - 616D 这是一个尺取法的问题,涉及到双指针,第一次写到第六个点就wa了,查了一下资料发现有几个容易忽略的点 双指针思路没有什么问题,需要注意每个数字可能出现不止一次,所以标记的数据不能是bool而应该是int,每出现一个新出现的数字,计数器cnt++,否则对应的尺取区间里面对应的st[a[j]]++. 当cnt > k应该移动 j,每移动一次,对应的st[a[j]]–,j++, 只有当st[a[j]] == 0的时候cnt–,才满足题目要求,然后每次更新一

Wen_Chen_的博客 368

Codeforces 616D Longest k-Good Segment【尺取法】

D. Longest k-Good Segment time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The array a with n integers is given. Let'

mengxiang000000 458

CodeForces - 616D Longest k-Good Segment (尺取法)

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Fi

LLL_yx的博客 337

A - Longest k-Good Segment CodeForces - 616D (尺取法)

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find...

日月 281

CodeForces - 616D Longest k-Good Segment

CodeForces - 616D Longest k-Good Segment 题意: 有包含n个数的序列a,求能找到最长的区间包含不超过k个不同的元素。 题解: 尺取法,先固定L,然后移动R,R每次移动,当超过k后,L再移动 代码: #include<bits/stdc++.h> #define debug(a,b) printf("%s = %d\n",a,b); typedef long long ll; using namespace std; inline int read(){

qq_35975367的博客 391

【CodeForces 616D】Longest k-Good Segment

题意 n个数里,找到最长的一个连续序列使里面最多k个不同的数。 分析 尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直到不同的数为k,然后更新答案。 代码 #include<cstdio> #define ll long long #define N 500005 int...

dingguayi7025的博客 162

CodeForces 616D Longest k-Good Segment(尺取)

D. Longest k-Good Segment time limit per test  1 second memory limit per test  256 megabytes input  standard input output  standard output The array a with n integers

K键盘里的青春K 572

codeforces 616D Longest k-Good Segment

原题链接:http://codeforces.com/contest/616/problem/D D. Longest k-Good Segment time limit per test 1 second memory limit per test 256 megabytes input standard inpu

messenger的博客 636
上一篇: Gym - 101485IIdentifying Map Tiles (递推)
下一篇: UVA - 11889 Benefit(数学题)
T__TSZ
博客等级 码龄10年 2粉丝 70原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值