各种TLE。。

F - 就差最后一题了,什么?不会做?请把题解看完,谢谢!
Time Limit:1000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu

Description
Recently, Losanto find an interesting Math game. The rule is simple: Tell you a number H, and you can choose some numbers from a set {a[1],a[2],……,a[n]}.If the sum of the number you choose is H, then you win. Losanto just want to know whether he can win the game.

Input
There are several cases.
In each case, there are two numbers in the first line n (the size of the set) and H. The second line has n numbers {a[1],a[2],……,a[n]}. 0

#include "iostream"
#include "cstdio"
#include "cstring"
#include <algorithm>
#include <stdlib.h>
#include <set>
using namespace std;
long long a[50];
long long n,h;
long ssum[50];  //用来存储到当前位置为止所有数的和
bool flag=false;
bool ifprint=false; //打印标记  默认状态为false--没打印

void dfs(long long sum,int pos)
{
    if(flag==true)
        return;
    if(sum==h)
    {
        if(ifprint==false)
        {
            cout<<"Yes"<<endl;
            ifprint=true;
            return;
        }
        else
            return;
    }

    if(flag||pos==n+1)
        return;
    if(ssum[n] - ssum[pos - 1] + sum < h || sum > h)
        return;
    //每个数都只有两个状态 要么取 要么不取
    dfs(sum+a[pos],pos+1);//TAKE ONE
    if(flag==true)
        return;
    dfs(sum,pos+1);// DONT TAKE THIS ONE
    return;

}


int main()
{

    while(scanf("%lld %lld",&n,&h)!=EOF)
    {
        int i=1;
        memset(ssum,0,sizeof(ssum));
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            ssum[i] = ssum[i - 1] + a[i];
        }
        flag=false;
        ifprint=false;
        dfs(0,1); //sum=0 位置 1
        if(flag==false&&ifprint==false)
            cout<<"No"<<endl;

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值