1. 程式人生 > >兩道尺取法

兩道尺取法

long for each closed lose nsa ngs color other eve

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

兩道尺取法