2017多校第6场1008

比赛的时候 一直用二分然后n^2去判断,无限tle。。。。

看了别人写的代码,自己还是太蠢。。。看到求最大就一直去想二分了。tle后也一直没有换想法。。。

这题可以用尺取发法写,枚举对称轴(注意分奇偶,这点在比赛的时候想到了,然而时间复杂度不够优秀)。把对称轴两边的区间的每个对应元素的查的绝对值,存起来,然后尺取去判断,真解法真是好。

代码看了http://blog.csdn.net/f_zyj/article/details/77064212 真的很好理解。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;

const int MAXN = 5555;
const int MAXM = MAXN >> 1;

int n, m;
int cnt, ans;
int tmp[MAXM];
char s[MAXN];

void check()
{
    int dis = 0, dis2 = 0, x1 = 0, x2 = 0;
    while (x2 < cnt)
    {
        dis2 += tmp[x2++];
        while (dis2 > m)
        {
            dis2 -= tmp[x1++];
        }
        dis = max(dis, x2 - x1);
    }
    ans = max(ans, dis);
}

void solve()
{
    n = (int)strlen(s + 1);
    ans = 0;
    for (int i = 1; i <= n; i++)
    {
        cnt = 0;
        int x1 = i - 1;
        int x2 = i + 1;
        while (x1 > 0 && x2 <= n)
        {
            tmp[cnt++] = abs(s[x1--] - s[x2++]);
        }
        check();
    }
    for (int i = 1; i <= n; i++)
    {
        cnt = 0;
        int x1 = i;
        int x2 = i + 1;
        while (x1 > 0 && x2 <= n)
        {
            tmp[cnt++] = abs(s[x1--] - s[x2++]);
        }
        check();
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%s",&m, s + 1);

        solve();

        printf("%d\n", ans);
    }

    return  0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值