zoukankan      html  css  js  c++  java
  • 洛谷P1638逛画展

    传送门啦

    只需记录满足条件的一个区间的初始端点 $ (head, tail) $ ,不断删掉左端点 $ head $ ,不断更新右端点 $ tail $ ;

    开一个 $ vis[] $ 记录一下每幅画出现的次数,

    删除左端点时,判断一下左端点对应的这幅画是否在 $ [ head+1, tail ] $ 区间里存在,即 $ vis[head]>0 $ ;

    反之去更新,去寻找被删掉的点对应的下一幅画,更新 (tail)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define re register
    using namespace std ;
    const int maxn = 1e6 + 5 ;
    const int maxm = 2005 ;
    
    inline int read () {
    	int f = 1 , x = 0 ;
    	char ch = getchar() ;
    	while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ; ch = getchar () ;}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar ();}
    	return x * f ;
    }
    
    int n , m , a[maxn] , A , B;
    int head = 1 , tail ;
    int vis[maxm] , tot ;
    struct Node {
    	int val , pos ;
    }q[maxn] ;
    
    int main () {
    	n = read () ; m = read () ;
    	for(re int i = 1 ; i <= n ; ++ i)  a[i] = read () ;
    	for(re int i = 1 ; i <= n ; ++ i) {
    		++tail ;
    		if(vis[a[i]] == false)  ++tot ;
    		++vis[a[i]] ;
    		if(tot == m) {
    			A = head ; B = tail ; 
    			break ;
    		}
    	}
    	while(1){
            int x = a[head] , l = head + 1 , r = tail ;
            if(vis[x] > 0)  --vis[x] ;
            if(vis[x] == 0) {
                --tot ;
                for(re int i = r + 1 ; i <= n ; ++ i){    
                    ++tail ;
                    if(vis[a[i]] == 0) { 
    					vis[a[i]] = 1 ; 
    					++tot ; 
    					break ; 
    				}
                    else ++vis[a[i]];
                }r = tail ;
            }
    		if(r - l < B - A && tot == m) {
    			A = l , B = r ;
    		}
            ++head ;
            if(head >= n - m + 1) break;
        }
        printf("%d %d " , A , B) ;
        return 0 ;
    }
    
  • 相关阅读:
    javascript模块化进阶
    javascript模块化基础
    css架构探索
    javascript函数基础概念 (补充完结)
    聊聊圣杯布局
    javascript函数基础概念
    yum提示This system is not registered with RHN.RHN support will be disabled.
    Linux分区和挂载硬盘
    Thunderbird扩展
    yum install nginx
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/10415105.html
Copyright © 2011-2022 走看看