map和结构体的简单运用Gym - 100883C

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值