题目大意:

输入n,m,分别表示人的个数和颜色的个数,下一行输入n个数,对应每个人的颜色,最后一行输入对应每个颜色的人应有的数量;

问是否能找出一个区间,满足条件但有多余的人,输出多余的人最少的个数,如果连条件都不能满足,输出-1

基本思路:

尺取法,自己写的没有设置l,r标记,也没有用set,一直卡在样例2,之后又卡37,不知道哪里错了,找了很久,后来干脆学习别人的思路吧。

设一个l指针指向当前数列左边,从左往右扫描一遍,将当前颜色记录,

当所有颜色都得到后,进行判断,如果当前l指向的颜色大于需要的颜色,l后移一位,然后更新答案

果然尺取还是应该定义l,r标记的,这样比较好些,也比较好思考;

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<set> using namespace std; typedef long long ll;
typedef long long LL;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 200000+10;
const ll mod = 1e9+9;
set<int>q;
int col[maxn],cnt[maxn],k[maxn];
int main(){
int n,m,sum=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&col[i]);
}
for(int i=1;i<=m;i++){
scanf("%d",&k[i]);
if(k[i]){
q.insert(i);
}
sum+=k[i];
}
int l=1,ans=inf;
for(int r=1;r<=n;r++){
int c=col[r];
++cnt[c];
if(cnt[c]==k[c]){
q.erase(c);
}
if(q.empty()){
while(l<=r&&cnt[col[l]]>k[col[l]]){
--cnt[col[l]];
l++;
}
ans=min(ans,r-l+1-sum);
}
}
if(ans==inf){
ans=-1;
}
printf("%d\n",ans);
return 0;
}

  

Codeforces 958F2 Lightsabers (medium) 尺取法的更多相关文章

  1. Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法

    D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...

  2. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  3. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  4. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

  5. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  6. CodeForces 701C They Are Everywhere 尺取法

    简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...

  7. Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)

    题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Round #364 (Div. 2) C. They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. python网络编程之验证客户端链接的合法性

    六.socket的更多方法介绍 服务端套接字函数s.bind() 绑定(主机,端口号)到套接字s.listen() 开始TCP监听s.accept() b被动接收TCP客户的连接,(阻塞式)等待连接的 ...

  2. luoguP3806 【模板】点分治1

    #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define ...

  3. C# 获取路径中,文件名、目录、扩展名等

    string path = "C:\\dir1\\dir2\\foo.txt"; string str = "GetFullPath:" + Path.GetF ...

  4. CSU 1554 SG Value (multiset/priority queue 思维题)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 Description The SG value of a set (mult ...

  5. java中的本地缓存

    java中的本地缓存,工作后陆续用到,一直想写,一直无从下手,最近又涉及到这方面的问题了,梳理了一下.自己构造单例.guava.ehcache基本上涵盖了目前的大多数行为了.   为什么要有本地缓存? ...

  6. DR 项目小结

    前言 个人的项目总结, 非技术类博文. 需要补充的知识点 HTTP 协议与其内置方法 curl 指令和各选项的意义 Keystone 认证流程和各项目配置文件 [keystone_authtoken] ...

  7. Centos7配置定时重启服务器

    Crontab是一个很方便的在unix/linux系统上定时(循环)执行某个任务的程序. 用 service crond status 查看 crond服务状态,如果没有启动则 systemctl s ...

  8. Flannel - 配置

    原文地址 flannel 从 ETCD 中读取配置. 默认情况下,flannel 从 /coreos.com/network/config 中读取配置,可以使用 --etcd-prefix 覆盖. 通 ...

  9. JSP 定义行列数表单创建表格

    1.添加行数 .列数提交表单 <!doctype html> <html> <head> <title>setTable-发送表单</title& ...

  10. upc组队赛16 Winner Winner【位运算】

    Winner Winner 题目链接 题目描述 The FZU Code Carnival is a programming competetion hosted by the ACM-ICPC Tr ...