zoukankan      html  css  js  c++  java
  • 洛谷1638(尺取)

    起手式:

    1.l = 1, r = 0;2.while循环里先用r往右试探,探到了或者到头了就停;3.然后用l往右探,不满足条件了就停;4.更新答案,然后如果发现指针没什么前途了就break出去输出答案。

    主代码:

     1 const int maxn = 1e6 + 5;
     2 int n, a[maxn], m, p, l, r;
     3 int in[2005], ansl, ansr, ans = inf;
     4 
     5 int main() {
     6     read(n), read(m);
     7     rep(i, 1, n)    read(a[i]);
     8     l = 1, r = 0, p = 0;
     9     while (true) {
    10         bool flag1 = false, flag2 = false;
    11         while (p < m && r < n) {
    12             flag1 = true;
    13             if (!in[a[++r]])    p++;
    14             in[a[r]]++;
    15         }
    16         while (p == m && l < r) {
    17             flag2 = true;
    18             if (in[a[l]] == 1)    p--;
    19             in[a[l++]]--;
    20         }
    21         if (p + flag2 == m && r - l + 1 + flag2 < ans) {
    22             ans = r - l + 1 + flag2;
    23             ansl = l - flag2, ansr = r;
    24         }
    25         if (not flag1 &&  not flag2)    break;
    26     }
    27     printf("%d %d
    ", ansl, ansr);
    28     return 0;
    29 }
  • 相关阅读:
    Tree UVA
    stringstream的使用
    Trees on the level UVA
    strchr和strstr函数
    sscanf的用法
    Dropping Balls UVA
    Boxes in a Line UVA
    Broken Keyboard (a.k.a. Beiju Text) UVA
    Matrix Chain Multiplication (堆栈)
    出栈次序
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/10514029.html
Copyright © 2011-2022 走看看