您的位置:首页 > 其它

codeforces 253D 单调队列 好题

2012-12-12 16:49 344 查看
View Code

#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 lenth must > 1 in this problem.
ans += cnt[map[i][r]-'a']-1;
}
}
printf("%I64d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: