codeforces 253D 单调队列 好题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char map[404][404];
int a[404][404]; 
int n, m, k, sum;
__int64 ans;
int cnt[33]; 
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int i, j, l, r;
    scanf("%d%d%d", &n, &m, &k);
    for(i = 1; i <= n; i++)
    {
        scanf("%s", map[i]+1);
        for(j = 1; j <= m; j++)
            a[i][j] = (map[i][j] == 'a') + a[i-1][j];
    }
    for(i = 1; i < n; i++)
        for(j = i+1; j <= n; j++)
        {
            memset(cnt, 0, sizeof(cnt));
            sum = 0; l = 1;
            for(r = 1; r <= m; r++)
            {
                sum += a[j][r] - a[i-1][r];
                if(map[i][r] == map[j][r]) 
                    cnt[map[i][r]-'a']++;
                while( sum > k && l <= r)  // deque
                {
                    sum -= a[j][l] - a[i-1][l];
                    if(map[i][l] == map[j][l])
                        cnt[map[i][l]-'a']--;
                    l++;
                }
                if(l < r && map[i][r] == map[j][r]) // pay attention "l < r", the length must > 1 in this problem.
                    ans += cnt[map[i][r]-'a']-1; 
            }
        }
    printf("%I64d\n", ans);
    return 0;
}
 


你可能感兴趣的:(codeforces 253D 单调队列 好题)