这题的数据范围吓得我很慌。

分析:

这道题蒟蒻本来想用背包的,但是发现m太大,一写肯定炸,然后看到数据范围表示成了2632^{63},马上想到了可以二进制转化一下,然后又写炸了(我太弱了 ),只能换成如下思路,

代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
long long n,m,sum,ans,a[2000001];      //都打成ll保险!        
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;++i)
    scanf("%lld",&a[i]);
    a[n+1]=m;                   
    sort(a+1,a+1+n+1);            
    if(a[1]!=1)//排序后没有1?骗谁,连1都表示不出来了,没答案!                 
    {
        printf("No answer!!!\n");   
        return 0;     
    }
    for(int i=1;i<=n;++i)
    {
        while(sum<a[i+1]-1) //贪心的思想        
        {
            sum+=a[i];
            ans++;           
            if(sum>=m)
            {
                printf("%lld\n",ans);  
                return 0;
            }
        }   
    }
    printf("%lld\n",ans+1);最后别忘了+1,
    return 0;
}

相关文章:

  • 2021-12-12
  • 2021-11-15
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2020-10-10
  • 2021-10-21
  • 2021-07-11
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2021-11-06
  • 2022-02-28
  • 2021-10-10
  • 2022-03-03
  • 2021-07-22
相关资源
相似解决方案