两道尺取法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两道尺取法相关的知识,希望对你有一定的参考价值。

1.咸鱼魔法记

题目链接:http://www.ifrog.cc/acm/problem/1124

题目:

DESCRIPTION

给你一个01串,我们定义这个串的咸鱼值,是最长的全1串。现在你最多可以使用K次咸鱼魔法,每次魔法,你可以使得一个位置翻转(0变成1,1变成0)。问你这个串的咸鱼值最多是多少。

INPUT
第一行两个整数N,K。表示串的长度和可以施展咸鱼魔法的次数。(N,K<=300000) 第二行N个01整数。
OUTPUT
输出答案。
SAMPLE INPUT
10 2
1 0 0 1 0 1 0 1 0 1
SAMPLE OUTPUT
5
题意:略...
题解:先最前面弄一段出来,然后后面不断尺取,过程中最大值更新即可。
技术分享图片
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int N=300000+10;
 7 int a[N];
 8 
 9 int main(){
10     int n,k;
11     scanf("%d %d",&n,&k);
12     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
13     int l=1,r=1,ans=0;
14     for(int i=1;i<=n;i++){
15         if(a[i]==0&&k==0) break;
16         if(a[i]==0) k--;
17         r++;
18     }
19     ans=max(ans,r-1);
20     for(int i=r;i<=n;i++){
21         if(a[i]==0){
22             ans=max(ans,i-l);
23             while(a[l]==1) l++;
24             l++;
25         }
26     }
27     ans=max(ans,n+1-l);
28     printf("%d\n",ans);
29     return 0;
30 }
View Code

2.An impassioned circulation of affection

题目链接:http://codeforces.com/problemset/problem/814/C

题目:

Nadeko‘s birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!

Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi‘s favourite one, and takes the length of the longest among them to be the Koyomity of the garland.

For instance, let‘s say the garland is represented by "kooomo", and Brother Koyomi‘s favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.

But problem arises as Nadeko is unsure about Brother Koyomi‘s favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.

Input

The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.

The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.

The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.

The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi‘s possible favourite colour.

Output

Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

题意:给定n长度的字符串,q次询问,每次询问给出m个字符c。问在原来字符串的基础上,改变其中一些字符,能得到的最长的全c子串的长度为多少。

题解:和上面一题的思路一样,不断尺取。时间复杂度为O(qn),3*108......左右。注意最后再更新一下答案,因为在过程中有可能更新不到答案。

技术分享图片
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 char s[2333];
 7 
 8 int main(){
 9     char c;
10     int n,q,m;
11     scanf("%d %s %d",&n,s,&q);
12 
13     while(q--){
14         int l=0,r=0,ans=0;
15         scanf("%d %c",&m,&c);
16         for(int i=0;i<n;i++){
17             if(s[i]!=c&&m==0) break;
18             if(s[i]!=c) m--;
19             r++;
20         }
21         ans=max(ans,r);
22         for(int i=r;i<n;i++){
23             if(s[i]!=c){
24                 ans=max(ans,i-l);
25                 while(s[l]==c) l++;
26                 l++;
27             }
28         }
29         ans=max(ans,n-l);
30         printf("%d\n",ans);
31     }
32 
33     return 0;
34 }
View Code

 

以上是关于两道尺取法的主要内容,如果未能解决你的问题,请参考以下文章

尺取法

尺取法

Codeforces 1156C 尺取法 / 二分

Shuffle UVA - 12174 尺取法

尺取法 poj 2566

poj3320(尺取法)