GYM 100883 C.Too Many Coins(水~)
2017-03-14 14:08
232 查看
Description
n枚硬币,现在可以带其中某些面值的硬币,使得带的硬币总面值不小于m,问最少要带多种硬币
Input
第一行一整数T表示用例组数,每组用例首先输入两个整数n和m表示硬币数和需要的面值,之后n个整数a[i]表示每枚硬币的面值(1<=n,a[i]<=1e6,1<=m<=1e9)
Output
如果所有硬币带上都不够m则输出Impossible,否则输出种类最少的情况,如果有多组解则输出面值相对大的那个
Sample Input
3
10 7
1 1 1 1 1 2 2 2 5 4
10 11
1 1 1 1 1 1 1 1 1 1
10 6
1 1 1 1 1 1 2 2 2 3
Sample Output
2 5
Impossible
2
Solution
统计每种面值的硬币的单个面值和总面值,之后对总面值排序,先用总面值大的那种硬币
Code
n枚硬币,现在可以带其中某些面值的硬币,使得带的硬币总面值不小于m,问最少要带多种硬币
Input
第一行一整数T表示用例组数,每组用例首先输入两个整数n和m表示硬币数和需要的面值,之后n个整数a[i]表示每枚硬币的面值(1<=n,a[i]<=1e6,1<=m<=1e9)
Output
如果所有硬币带上都不够m则输出Impossible,否则输出种类最少的情况,如果有多组解则输出面值相对大的那个
Sample Input
3
10 7
1 1 1 1 1 2 2 2 5 4
10 11
1 1 1 1 1 1 1 1 1 1
10 6
1 1 1 1 1 1 2 2 2 3
Sample Output
2 5
Impossible
2
Solution
统计每种面值的硬币的单个面值和总面值,之后对总面值排序,先用总面值大的那种硬币
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define maxn 1111111 int T,n,m,a[maxn],res,cnt; struct node { ll v; int id; node(){}; node(ll _v,int _id) { v=_v,id=_id; } bool operator<(const node&b)const { if(v!=b.v)return v>b.v; return id>b.id; } }b[maxn]; int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); ll sum=0; memset(a,0,sizeof(a)); while(n--) { int temp; scanf("%d",&temp); sum+=1ll*temp,a[temp]++; } if(sum<m)printf("Impossible\n"); else { res=0; for(int i=1;i<=1000000;i++) if(a[i])b[res++]=node(1ll*i*a[i],i); sort(b,b+res); sum=0;cnt=0; for(int i=0;i<res;i++) { a[cnt++]=b[i].id; sum+=b[i].v; if(sum>=m)break; } sort(a,a+cnt); for(int i=0;i<cnt;i++)printf("%d%c",a[i],i==cnt-1?'\n':' '); } } return 0; }
相关文章推荐
- [MySQL FAQ]系列 -- Too many open files
- [转]socket: Too many open files (24) 的解决方法
- Too many open files 错误解决方案
- Jetty - linux -Too many open files
- Xcode6.1使用MJRefresh、SDWebImage,Too many arguments to function call, expected 0, have *
- Exception: Too many parameters were provided in this RPC request. The maximum
- mysql Error 1040 too many connection解决办法
- iOS-Bug: objc_msgSend 报错Too many arguments to function call, expected 0, have 3
- Linux中的错误:too many open files
- 解决mysql 1040错误Too many connections的方法
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- nginx报错accept4() failed (23: Too many open files in system)
- python——ubuntu下[Error 24: too many open files]的解决方法
- Too many connections
- popen类似的打开函数, 失败出现Too many open files
- tomcat 报错 Too many open files
- NHibernate and TooManyRowsAffectedException(转载)
- F-Secure SSH File Transfer遇到错误"too many authentication failures for root"
- Too many processes on hostname
- 解决org.apache.rat:apache-rat-plugin:0.8:check (default) on project hbase: Too many unapproved license