当前位置: 首页 > news >正文

CF580B Kefa and Company(滑动窗口)

题目描述

Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n−1 .

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

输入格式

The first line contains the integer n ( 1<=n<=100000 ) — the number of flats in the house.

The second line contains the row s with the length n , it consists of uppercase and lowercase letters of English alphabet, the i -th letter equals the type of Pokemon, which is in the flat number i .

输出格式

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.

隐藏翻译

题意翻译

给出一个长度为n的序列,求出长度最小的序列that包含序列中所有的字母(区别大小写)。详见样例

输入输出样例

输入 #1复制

3
AaA

输出 #1复制

2

输入 #2复制

7
bcAAcbc

输出 #2复制

3

输入 #3复制

6
aaBCCe

输出 #3复制

5

说明/提示

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2 .

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6 .

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6 .


#include<bits/stdc++.h>
#define int long long
#define ll long long
#define N 100005
#define INF INT64_MAX
#define MINF 0x3f
#define eps 1e-9
using namespace std;
int n,x,m,k,t,ans=0,vis[N],c[N],minn=INF,maxn=-INF,flag=false;
int a[N],b[N],t1[N],t2[N],sum[N];
map<int,int> mp;  //map 数组作映射,记录前缀和相同的时候
struct node{    //因为要对a数组进行排序,所以用结构体排序
  int a,b;
}no[N];

bool cmp(node p,node q)
{
  return p.a<q.a;
}

void solve()
{
  cin>>n>>m;
  for(int i=1;i<=n;i++) {
    cin>>no[i].a>>no[i].b; 
  }
sort(no+1,no+n+1,cmp);
  for(int i=1;i<=n;i++) 
  {
    sum[i]=sum[i-1]+no[i].b;   //前缀和计算b的前i项和
  }
  int r=1;
  for(int l=1;l<=n;l++)         //滑动窗口,缩小左边界
  {
    while(r<=n&&no[r].a-no[l].a<m) //当右边界不越界,且ai差距不超过m的时候
    {
      ans=max(ans,sum[r]-sum[l-1]);  //就更新ans的长度,寻找的在此范围内的最大ans
      r++;                   //找完以后右边界继续右移
    }
  }

}
signed main()
{
  
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
     solve();
    return 0; 
 }
 

http://www.dtcms.com/a/107216.html

相关文章:

  • Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
  • 尚语翻译图册翻译|专业图册翻译|北京专业翻译公司推荐|专业文件翻译报价
  • 开发体育赛事直播系统主播认证功能技术实现方案
  • 体重秤PCBA电路方案组成结构
  • 【CSS3】05-定位 + 修饰属性
  • XSS漏洞的分类解释和演示实验
  • dfs(基于BFS的dfs)力扣37.解数独力扣79.单词搜索力扣1219.黄金矿工力扣980.不同路径III
  • linux shell 删除空行(remove empty lines)
  • 12-leveldb优化读性能的方法
  • agent报告
  • Linux命令-inotifywait
  • 继承与组合
  • LlamaIndex实现RAG增强:融合检索(Fusion Retrieval)与混合检索(Hybrid Search)
  • [ 取证 ]取证学习,工具使用, 赛题复现
  • Day 6
  • 在Android中创建DialogFragment
  • 【光电集成电路技术】基于单片机的音乐梦幻灯与USB转接器设计,电子琴硬件组成及仿真电路实现
  • 【Java 优选算法】二分算法(下)
  • OpenGL进阶系列20 - OpenGL SuperBible - bindlesstex 例子学习
  • Flutter求助贴
  • 失效模式分析(FMEA)、控制计划(CP)、流程图(Flowchart)的无缝衔接与高效管理--全星FMEA软件系统
  • 《自然-方法》2024年度技术:空间蛋白质组学(spatial proteomics)
  • PyArrow 核心技术与应用:高效数据处理与跨生态集成实践
  • C语言常见3种排序
  • 利用vmware快速安装一个可以使用的centos7系统
  • QML弹窗
  • H(e^j\omega) H(\omega)
  • 计算机网络复习 吉林大学
  • PH热榜 | 2025-04-02
  • OSI每一层的SAP和CEP分别是什么