二分

题意:

你的朋友提议玩一个游戏:将写有数字的n个纸片放入口袋中,你可以从口袋中抽取4次纸片,每次记下纸片上的数字后都将其放回口袋中。如果这4个数字的和是m,就是你赢,否则就是你的朋友赢。你挑战了好几回,结果一次也没赢过,于是怒而撕破口袋,取出所有纸片,检查自己是否真的有赢的可能性。请你编写一个程序,判断当纸片上所写的数字是k1, k2, …, kn时,是否存在抽取4次和为m的方案。如果存在,输出Yes;否则,输出No。(1<=n<=1000,1<=m<=1e8,1<=k<=1e8)

解法1:暴力

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

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;

int n, m, k[MAXN];

void solve()
{
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            for(int c = 0; c < n; c++)
            {
                for(int d = 0; d < n; d++)
                {
                    if(k[a] + k[b] + k[c] + k[d] == m)
                    {
                        flag = true;break;
                    }
                }
            }
        }
    }
    if(flag) puts("yes");
    else puts("no");
}


int main()
{
    freopen("input.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        scanf("%d", &m);
        for(int i = 0; i < n; i++)
            scanf("%d", &k[i]);
        solve();
    }
    return 0;
}

解法2:稍加优化

//@auther yangZongJun
/********************************************//**
Date  : 2015
题目来源:
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;

int n, m, k[MAXN];
/*
void solve()
{
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            for(int c = 0; c < n; c++)
            {
                for(int d = 0; d < n; d++)
                {
                    if(k[a] + k[b] + k[c] + k[d] == m)
                    {
                        flag = true;break;
                    }
                }
            }
        }
    }
    if(flag) puts("yes");
    else puts("no");
}
*/

bool binary_search(int x)
{
    int l = 0, r = n;
    while(r - l >= 1)
    {
        int mid = (r + l) / 2;
        if(k[mid] == x) return true;
        else if(k[mid] < x) l = mid + 1;
        else r = mid;
    }
    return false;
}

void solve()
{
    sort(k, k+n);
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            for(int c = 0; c < n; c++)
            {
                if(binary_search(m - k[a] - k[b] - k[c]))
                    flag = true;
            }
        }
    }
    if(flag) puts("yes");
    else puts("no");
}

int main()
{
    freopen("input.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        scanf("%d", &m);
        for(int i = 0; i < n; i++)
            scanf("%d", &k[i]);
        solve();
    }
    return 0;
}

解法三:最优

//@auther yangZongJun
/********************************************//**
Date  : 2015
题目来源:
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1005;
const int INF = 2100000000;

int n, m, k[MAXN];
int kk[MAXN*MAXN];
/*
void solve()
{
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            for(int c = 0; c < n; c++)
            {
                for(int d = 0; d < n; d++)
                {
                    if(k[a] + k[b] + k[c] + k[d] == m)
                    {
                        flag = true;break;
                    }
                }
            }
        }
    }
    if(flag) puts("yes");
    else puts("no");
}
*/
/*
bool binary_search(int x)
{
    int l = 0, r = n;
    while(r - l >= 1)
    {
        int mid = (r + l) / 2;
        if(k[mid] == x) return true;
        else if(k[mid] < x) l = mid + 1;
        else r = mid;
    }
    return false;
}

void solve()
{
    sort(k, k+n);
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            for(int c = 0; c < n; c++)
            {
                if(binary_search(m - k[a] - k[b] - k[c]))
                    flag = true;
            }
        }
    }
    if(flag) puts("yes");
    else puts("no");
}
*/

bool binary_search(int x)
{
    int l = 0, r = n*n;
    while(r - l >= 1)
    {
        int mid = (r + l) / 2;
        if(kk[mid] == x) return true;
        else if(kk[mid] < x) l = mid + 1;
        else r = mid;
    }
    return false;
}

void solve()
{
    for(int c = 0; c < n; c++)
    {
        for(int d = 0; d < n; d++)
        {
            kk[c*n + d] = k[c] +k[d];
        }
    }
    sort(kk, kk + n*n);
    bool flag = false;
    for(int a = 0; a < n; a++)
    {
        for(int b = 0; b < n; b++)
        {
            if(binary_search(m - k[a] - k[b]))
                flag = true;
        }
    }
    if(flag) puts("yes");
    else puts("no");
}
int main()
{
    freopen("input.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        scanf("%d", &m);
        for(int i = 0; i < n; i++)
            scanf("%d", &k[i]);
        solve();
    }
    return 0;
}

参见这里
http://blog.csdn.net/wcc526/article/details/13093661

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值