Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.
Amr has n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments.
Amr asked for your help to distribute his free days between instruments so that he can achieve his goal.
The first line contains two numbers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ 10 000), the number of instruments and number of days respectively.
The second line contains n integers ai (1 ≤ ai ≤ 100), representing number of days required to learn the i-th instrument.
In the first line output one integer m representing the maximum number of instruments Amr can learn.
In the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order.
if there are multiple optimal solutions output any. It is not necessary to use all days for studying.
4 10 4 3 1 2
4 1 2 3 4
5 6 4 3 1 1 2
3 1 3 4
1 3 4
0
In the first test Amr can learn all 4 instruments.
In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.
In the third test Amr doesn't have enough time to learn the only presented instrument.
题意:
可以理解为:一个人有很多个知识点要学习,对于每个知识点,学习需要一定的时间,但是自己的时间有限。
问如何在有限的时间内,学习到尽可能多的知识点。(即要求学习知识点的个数最多)
分析:
很明显的贪心题目,对于知识点的学习,如果想要学习的个数最多,那么一定要从小的开始学习。
AC:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <math.h>
using namespace std;
#define N 10010
#define LL __int64
struct LNode{
int x,y;
}f[N];
int cmd(LNode x,LNode y){
return x.x<y.x;
}
int ans[N];int len=0;
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
int n,k;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++){
scanf("%d",&f[i].x);
f[i].y=i;
}
len =0;
sort(f,f+n,cmd);
int num=0;int t=0;
while(k-f[t].x>=0){
num++;
k-=f[t].x;
ans[len++]=f[t].y+1;
t++;
if(t==n) break;
}
printf("%d\n",num);
for(int i=0;i<len;i++){
if(i==0) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}printf("\n");
return 0;
}