2048 Game(分治法)

You are playing a variation of game 2048. Initially you have a multiset s of n integers. Every integer in this multiset is a power of two.

You may perform any number (possibly, zero) operations with this multiset.

During each operation you choose two equal integers from s, remove them from s and insert the number equal to their sum into s.

For example, if s={1,2,1,1,4,2,2} and you choose integers 2 and 2, then the multiset becomes {1,1,1,4,4,2}.

You win if the number 2048 belongs to your multiset. For example, if s={1024,512,512,4} you can win as follows: choose 512 and 512, your multiset turns into {1024,1024,4}. Then choose 1024 and 1024, your multiset turns into {2048,4} and you win.

You have to determine if you can win this game.

You have to answer q independent queries.

Input
The first line contains one integer q (1≤q≤100) – the number of queries.

The first line of each query contains one integer n (1≤n≤100) — the number of elements in multiset.

The second line of each query contains n integers s1,s2,…,sn (1≤si≤229) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two.

Output
For each query print YES if it is possible to obtain the number 2048 in your multiset, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example
Input
6
4
1024 512 64 512
1
2048
3
64 512 2
2
4096 4
7
2048 2 2048 2048 2048 2048 2048
2
2048 4096
Output
YES
YES
NO
NO
YES
YES
Note
In the first query you can win as follows: choose 512 and 512, and s turns into {1024,64,1024}. Then choose 1024 and 1024, and s turns into {2048,64} and you win.

In the second query s contains 2048 initially.

思路:

方法一:

刚开始不知道怎么写,问了别人才知道用分治法,我不知道分治法是什么,就csdn,原来就是把问题划分为相互独立的子问题进行处理;比如:
把2048分成1024 1024;然后看集合中有几个1024;(分治法必定和递归联系起来)
三种情况:

  • 1个1024,把1024分解成512 512,然后判断集合中有几个512
  • 2个及以上1024,则符合条件;
  • 0个1024,就把两个1024分成4个512;

然后对每种情况再对512进行分解,也是三种情况;
具体的代码中解释;

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

using namespace std;
const int maxx=1e5+7;
int a[maxx];
map<int,int> mp;
bool dfs(int a,int b,int cot)//b代表a是从哪个数分解来的;cot代表坝上一个数分解后的数的个数减去集合中这个数出现的次数
{
    if(a==0) return 0;
    if(mp[a]>=cot*b/a)//如果某次分解后的数在集合中出现的次数大于组成这个分解数所需的数的数量
    {
        mp[a]-=cot*b/a;
        return 1;
    }
    else
    {
        int temp=mp[a];
        mp[a]=0;
        return dfs(a/2,a,cot*b/a-temp);//每次都分解上一级,并且用b记录上一级,传递给下一次dfs
    }

}
int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        memset(a,0,sizeof a);
        mp.clear();
        int flag=0;
        int n;
        cin >>n;
        for(int i=0;i<n;i++)
        {
            cin >>a[i];
            if(a[i]==2048)
            {
                flag=1;
            }
            mp[a[i]]++;
        }
        if(flag)
        {
            printf("YES\n");
            continue;
        }
        int f=dfs(2048,2048,1);
        if(f)
        {
            printf("YES\n");
        }
        else printf("NO\n");
    }
    return 0;
}


方法二

和方法一的思想大差不差,只不过代码实现的时候简单了许多;
刚开始就是查找集合中有没有2048,没有的话,分成两个1024,再次查找;如果只有一个1024,那么分解1024,查找512;

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

using namespace std;
const int maxx=1e5+7;
int a[maxx];
map<int,int> mp;
int dfs(int x,int k)
{
    if(x<1) return 0;
    if(mp[x]>=k)
        return 1;
    return dfs(x/2,k*2-mp[x]*2);//每次都折半,并且传递下一次需要查找的数的数量;
}
int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        mp.clear();
        int n;
        cin >>n;
        for(int i=0;i<n;i++)
        {
            cin >>a[i];
            mp[a[i]]++;
        }
        int flag=dfs(2048,1);
        if(flag)
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值