ZOJ 3768 Continuous Login

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5231

给一个数,判断这个数可以由 3组前缀和组成 或 2组前缀和组成 或 1组前缀和组成

思路: 用2分,搜索一组组成是否可能,之后用另一种二分搜2组的,之后暴力搜3组的

#include <iostream>
#include <string>
#define maxs 202020
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define mme(i,j) memset(i,j,sizeof(i))
using namespace std;
#define ll long long
ll sum[maxs];
ll pos;
ll ans[maxs];
ll l,r;
bool Binary(int n)
{
    ll re;
    l=1,r=pos-1;
    while(l<=r)
    {
        re=sum[l]+sum[r];
        if(re==n)
            return 1;
        if(re>n)
            r--;
        else
            l++;
    }
    return 0;
}
int main()
{
    int n,t;
    while(~scanf("%d",&t))
    {
        sum[1]=1;
        for(int i=2; i<=15713; i++)
        {
            sum[i]=sum[i-1]+i;
        }
        while(t--)
        {
            scanf("%d",&n);
            sum[1]=1;

            pos = lower_bound(sum,sum+15713,n)-sum;
            if(sum[pos]==n)
            {
                cout<<pos<<endl;
                continue;
            }
            if(Binary(n))
            {
               cout<<l<<" "<<r<<endl;
               continue;
            }
            for(int i=1;i<pos-1;i++)
            {
                if(Binary(n-sum[i]))
                {
                    cout<<l<<" "<<r<<" "<<i<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值