Problem-solving ideas: Give the time required to learn n instruments, and the total number of days, ask the maximum number of instruments to learn, and output the number of instruments in the original sequence (not unique)
In ascending order, to learn the most instruments, you must choose to spend the least time to learn and then use the structure to store the sequence number of the instrument in the original series.
A. AMR and MusicTime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
AMR is a young coder who likes music a lot. He always wanted to learn what to play music but he is busy coding so he got a idea.
Amr has n instruments, it takes a Sub class= "Lower-index" > i days to Learn i -th Instrument. Being busy, Amr dedicated k days to learn what to play the maximum possible number of instruments .
Amr asked for your help to distribute him free days between instruments so that he can achieve his goal.
Input
The first line contains numbers n, k (1≤ n ≤100, 0≤ k ≤10), the number of instruments and number of days respectively.
The second line contains n integers ai (1≤ a i ≤100), representing number of days required to learn the i-th instrument.
Output
In the first line output one integer m representing the maximum number of instruments AMR can learn.< /c3>
The second line output m space-separated integers:the Indices of instruments is learnt. You may output indices on any order.
If there is multiple optimal solutions output any. It is not a necessary to the use of a for studying.
Sample Test (s) input
4 10 4 3 1 2
Output
4 1 2) 3 4
Input
5 6 4 3 1 1 2
Output
3 1 3 4
Input
1 3 4
Output
0
Note
In the first Test AMR can learn all 4 instruments.
In the second Test and other possible solutions is: {2, 3, 5} or {3, 4, 5}.
In the third Test Amr doesn ' t has enough time to learn, the only presented instrument.
/*287 div2 a*/#include <iostream> #include <cstdio> #include <cstring> #include < Algorithm> using namespace std;struct node{int first;int second;} a[105];bool cmp (Node n1,node n2) { return n1 .first<n2.first; } int main () {int n,k,i,j,sum=0;scanf ("%d%d", &n,&k), for (i=1;i<=n;i++) {scanf ("%d", &a[i].first); A[i]. Second=i;} Sort (a+1,a+n+1,cmp); for (i=1;i<=n;i++) {if (sum+a[i].first<=k) sum=sum+a[i].first;elsebreak;} printf ("%d\n", i-1); for (j=1;j<i;j++) printf ("%d", A[j].second);}
A. AMR and Music