Educational Codeforces Round 30 B C

来源:互联网 发布:防蹭网软件苹果版 编辑:程序博客网 时间:2024/06/05 18:42

B. Balanced Substring
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2… sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

You have to determine the length of the longest balanced substring of s.

Input
The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.

Output
If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

Examples
input
8
11010111
output
4
input
3
111
output
0
Note
In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

In the second example it’s impossible to find a non-empty balanced substring.

题意:如果一个字符串中0的数量和1的数量相等,就说这个字符串师平衡串,然后给你一个字符串让你求出他的最长平衡子串。
题解:前缀和,开一个数组a,如果在字符串中遇到0则在数组a的相应位置计为-1,如果在字符串中遇到1则在数组a的相应位置计为1,然后求数组a的前缀和ans,再瞎搞一下,详情请看代码。

#include <bits/stdc++.h>using namespace std;map<int,int> yes;char str[123456];int a[123456];int main(){    yes.clear();    int n;    scanf("%d",&n);    scanf("%s",str);    for(int i=0;i<n;i++){        if(str[i]=='0') a[i+1] = -1;        else a[i+1] = 1;    }    int ans=0;    int l=0;    for(int i=1;i<=n;i++){        ans+=a[i];        if(ans==0) l= max(l,i);        if(!yes[ans]) yes[ans]=i;        else l=max(l,i-yes[ans]);    }    printf("%d\n",l);    return 0;}

C. Strange Game On Matrix
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan is playing a strange game.

He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:

Initially Ivan’s score is 0;
In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1’s in the column, this column is skipped;
Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1’s among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn’t want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.

Input
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100).

Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.

Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.

Examples
input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
output
4 1
input
3 2 1
1 0
0 1
0 0
output
2 0
Note
In the first example Ivan will replace the element a1, 2.

题意:给你一个矩阵,这个矩阵中只包含0,1,你可以将任何数量的1替换成0,然后让你在每一列中从第一个1开始(如果你把第一个1换成0了的话,第二个1就成了第一个1,以此类推)找使得接下来min(k,n-i+1)(i是指第一个1在第i行)连续的数中1的个数最多,输出这些1的个数的中和,和把1替换成0的最小次数。
暴力:

#include <bits/stdc++.h>using namespace std;int a[123][123];int gsd(int pos,int index,int k,int n){    int s=0;    for(int i=pos,h=0;i<=n&&h<min(k,n-pos+1);i++,h++){        s+=a[i][index];    }    return s;}int main(){    int n,m,k;    scanf("%d %d %d",&n,&m,&k);    for(int i=1;i<=n;i++){        for(int j=1;j<=m;j++){            scanf("%d",&a[i][j]);        }    }    int s=0;    int q=0;    for(int i=1;i<=m;i++){        int h=0;        int Max=0;        int ans=0;        for(int j=1;j<=n;j++){            if(a[j][i]==1){                h++;                int c=gsd(j,i,k,n);                if(c>Max){                    Max=c;                    ans=h-1;                }            }        }        s+=Max;        q+=ans;    }    cout<<s<<" "<<q<<endl;    return 0;}