洛谷P2001 硬币的面值 题解

mac2022-06-30  102

题目链接:https://www.luogu.org/problemnew/show/P2001

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

分析:

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

代码:

#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; }

转载于:https://www.cnblogs.com/vercont/p/10210038.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)