您的位置:首页 > 其它

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: