map和结构体的简单运用Gym

来源:互联网 发布:淘宝新店的能买吗 编辑:程序博客网 时间:2024/05/18 13:07
Your friend has C coins in his pocket, each coin has a value Vi, and you know that he will not need that amount of money, he will only need M.

You want to help him not to carry all these coins, so you decided to tell him to take the coins of specific values, in a way that he will have at least the amount of money he needs.

You will tell him that it will be enough for him if he carried coins of types X1, X2, ..., Xn.

Note that if you tell him to carry coins of type Xi, he will carry all the coins with values Vi = Xi.

Input
The first line contains an integer T representing the number of test cases.

Each test case consists of two lines, the first line has two integers C (1 ≤ C ≤ 1000000) and M (1 ≤ M ≤ 109), and the other line contains C integers representing the values of the coins (1 ≤ Vi ≤ 1000000).

Output
For each test case print the types of coins which your friend must carry,if there are multiple solutions, print the solution with the minimum number of types, if there are still multiple solutions print the one which makes your friend carry more money, if there are still multiple solutions print the solution with the bigger types.

print the types in increasing order. If he doesn't have enough money print "Impossible" without the quotations.

Example
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
Output
2 5
Impossible

2


题意:t个数据。 下面给出c,m c为下一行的总数,如果让这个人带1的话,他会把1全都带上,也就带了5个1,带2的话就把2全部带上,带了3个2,保证带的 钱比m大就可以了   输出 带的几种硬币,如果有多种方案,输出带的硬币总数最少的方案,如果硬币总数也同样多,输出最大的种类的方案,

#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define INF 1E9*1E4#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)typedef long long LL;const int maxn=1e3+5;const int maxx=2e6+100;const double EPS=1e-7;const int mod=1000000007;//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt);    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1);    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}struct node{    int type,score;    LL sum;}Q[maxx];bool cmp(node a,node b){    if(a.sum==b.sum) return a.type>b.type;    return a.sum>b.sum;}int main(){    int t;    cin>>t;    while(t--)    {        int c,m;        cin>>c>>m;        me(Q);        map<int ,int >d;        for(int i=0;i<c;i++)        {            int x;            cin>>x;            d[x]++;        }        int cnt=0;        foreach(it,d)        {            Q[cnt].type=it->first;            Q[cnt].score=it->second;            Q[cnt].sum=(LL)(Q[cnt].type*Q[cnt].score);            cnt++;        }        sort(Q,Q+cnt,cmp);        LL ant=0;        vector<int >ans;        for(int i=0;i<cnt;i++)        {            ant+=Q[i].sum;            ans.push_back(Q[i].type);            if(ant>=m) break;        }        if(ant<m) puts("Impossible");        else        {            sort(ans.begin(),ans.end());            int len=ans.size();            for(int i=0;i<ans.size();i++)            {                printf("%d%c",ans[i],i==len-1?'\n':' ');            }        }    }}




0 0
原创粉丝点击