zoukankan      html  css  js  c++  java
  • CodeForces1221A2048 Game思维题

    You are playing a variation of game 2048. Initially you have a multiset ss of nn 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 ss, remove them from ss and insert the number equal to their sum into ss.

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

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

    You have to determine if you can win this game.

    You have to answer qq independent queries.

    Input

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

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

    The second line of each query contains nn integers s1,s2,,sns1,s2,…,sn (1si2291≤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 20482048 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 512512 and 512512, and ss turns into {1024,64,1024}{1024,64,1024}. Then choose 10241024 and 10241024, and ss turns into {2048,64}{2048,64} and you win.

    In the second query ss contains 20482048 initially.

     

    题意:

    给出一串数,所有的数全部都是2的次方。判断能否组成2048,里面的出现的数字可以由两个相同数字的和进行替换,比如2,2,8可以变成4,8。能输入YES,否则输出NO。

     

    思维点:

    首先要判断给的数据中有没有2048,要是存在2048就可以直接输出YES了,否则继续判断。

    2048=2^11,1024=2^10,可以发现2的0次方一直加到2的10次方的和刚好是2047,如果说累加出来的sum大于2047,也就是说大于等于2048的话即可,

    但是需要注意的是一旦遇到一个大于2048的,就不能进行累加。

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 
     6 typedef long long ll;
     7 using namespace std;
     8 
     9 ll a[110],b[110];
    10 
    11 int main()
    12 {
    13     int t;
    14     scanf("%d",&t);
    15     while(t--)
    16     {
    17         int n;
    18         scanf("%d",&n);
    19         int flag=0;
    20         for(int i=0;i<n;i++)
    21         {
    22             scanf("%lld",&a[i]);
    23             if(a[i]==2048)
    24                 flag=1;
    25         }
    26         if(flag)
    27             printf("YES\n");
    28         else
    29         {
    30             sort(a,a+n);
    31             ll sum=0;
    32             int flag=1;
    33             for(int i=0;i<n;i++)
    34             {
    35                 if(a[i]>=2048)
    36                     continue;
    37                 sum+=a[i];
    38 
    39             }
    40             if(sum>=2048)
    41                 printf("YES\n");
    42             else
    43                 printf("NO\n");
    44         }
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    GridView实用示例源码附加导出Excel功能
    在做web项目时经常遇到新闻要上一篇,下一篇
    wpf是什么
    今天发的一些文章是早以前遇到一些小问题,园友别炮轰我哦
    Excel表中的数据导入到数据库中
    WPF指南之XAML概述
    sql 替换字段内的内容及出现的问题
    const 和readonly
    SQL2008报Script failed for Server 'xxxxxx.database.windows.net' (Microsoft.SqlServer.Smo)这样的错误
    新年新气象
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11686537.html
Copyright © 2011-2022 走看看