poj 2566

本文详细介绍了尺取法的基本概念及其在算法题中的应用。尺取法是一种用于解决特定类型问题的有效方法,通过不断调整左右端点来寻找最优解。文章通过一个具体的示例程序展示了如何使用尺取法解决问题,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


这幅图便是尺取法怎么“取”的过程了。

  整个过程分为4布:

    1.初始化左右端点

    2.不断扩大右端点,直到满足条件

    3.如果第二步中无法满足条件,则终止,否则更新结果

    4.将左端点扩大1,然后回到第二步http://images.cnitblog.com/blog/597004/201408/291224259702079.jpg


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define LL long long
using namespace std;
vector<pair<LL, LL> > ans;
void solve(LL n)
{
    ans.clear();
    LL s = 1, t = 1;
    LL sum = 0;
    while (s * s <= n)
    {
        while (t * t <= n && sum < n)
        {
            sum += t * t;
            t++;
        }
        if (sum == n)
        {
            ans.push_back(make_pair(s, t));
        }
        sum -= s * s;
        s++;
    }
    int sz = ans.size();
    printf("%d\n", sz);
    for (int i = 0; i < sz; i++)
    {
        printf("%d", ans[i].second - ans[i].first);
        for (int j = ans[i].first; j < ans[i].second; j++)
        {
            printf(" %d", j);
        }
        puts("");
    }
}
int main()
{
    LL n;
    while (~scanf("%lld", &n))
    {
        solve(n);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值