CodeForces 507A Amr and Music
来源:互联网 发布:python 字符串格式化 编辑:程序博客网 时间:2024/05/22 07:53
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 thei-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 104 3 1 2
41 2 3 4
5 64 3 1 1 2
31 3 4
1 34
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.
#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct music{int days;int rank;}yueqi[110];int cmp(music x,music y){ return x.days<y.days; } int main(){int n,k,m,i,j,temp;int num[110];while(cin>>n>>k){temp=0;j=0;for(i=0;i<n;i++){cin>>yueqi[i].days;yueqi[i].rank=(i+1);}sort(yueqi,yueqi+n,cmp);for(i=0;i<n;i++){temp+=yueqi[i].days;if(temp<k){num[j]=yueqi[i].rank;j++;}else if(temp==k){num[j]=yueqi[i].rank;j++;break;}else{break;}}cout<<j<<endl;if(j!=0){for(i=0;i<j;i++){if(i==j-1){cout<<num[i]<<endl;}else{cout<<num[i]<<" ";}}}}return 0;}
- Codeforces 507A Amr and Music
- CodeForces 507A Amr and Music
- codeforces 507A Amr and Music
- codeforces--507A--A. Amr and Music--507B--B. Amr and Pins
- A. Amr and Music
- A. Amr and Music
- A Amr and Music( Codeforces Round #287 (Div. 2))
- Codeforces Round #287 (Div. 2) A. Amr and Music
- coderforce 507A Amr and Music(贪心)
- Amr and Music
- Amr and Music
- codeforces-#287-- 507B Amr and Pins
- Codeforces-507B-Amr and Pins
- Codeforces 507B Amr and Pins【思维】
- Codeforces 569A Music
- codeforces 569A Music
- CodeForces 569A-Music
- Codeforces 569 A Music
- HDU2846 Repository 字典树
- 客户端下载服务器视频然后播放
- 前端学习网站推荐
- 自动生成头注释文件
- 329.LeetCode Longest Increasing Path in a Matrix(hard)[DFS]
- CodeForces 507A Amr and Music
- mysql事务之未提交读(Read uncommitted)
- Android - Handler is abstract, cannot be instantiated.
- leetcode 279 : Perfect Squares
- 深克隆和浅克隆
- CodeForces 560A Currency System in Geraldion
- mysql事务之提交读(Read Committed)
- mysql事务之可重复读(Repeated Read)
- 优化mysql数据库性能的十个参数