A. Amr and Music
来源:互联网 发布:java调用ffmpeg命令行 编辑:程序博客网 时间:2024/05/21 23:34
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 learni-th instrument. Being busy, Amr dedicatedk 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 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.
题意:
可以理解为:一个人有很多个知识点要学习,对于每个知识点,学习需要一定的时间,但是自己的时间有限。
问如何在有限的时间内,学习到尽可能多的知识点。(即要求学习知识点的个数最多)
分析:
很明显的贪心题目,对于知识点的学习,如果想要学习的个数最多,那么一定要从小的开始学习。
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 __int64struct 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;}
- A. Amr and Music
- A. Amr and Music
- Codeforces 507A Amr and Music
- CodeForces 507A Amr and Music
- codeforces 507A Amr and Music
- Amr and Music
- Amr and Music
- codeforces--507A--A. Amr and Music--507B--B. Amr and Pins
- A Amr and Music( Codeforces Round #287 (Div. 2))
- coderforce 507A Amr and Music(贪心)
- Codeforces Round #287 (Div. 2) A. Amr and Music
- AMR nb and wb
- AMR nb and wb
- AMR nb and wb
- Amr and Pins
- B. Amr and Pins
- B. Amr and Pins
- codeforcesC. Amr and Chemistry
- Linux下安装Oracle11G详细过程
- 【go语言从零单排1】在mac环境下配置go,Hello world!
- VPN代理:VPN技术的现状与发展前景
- Python 初学记录 day1
- Python 根据"高了"或"低了"猜数字
- A. Amr and Music
- 不同服务器上的Java项目文件同步 解决方案(socket 、http)
- 数据库的日期操作
- CF507E——Breaking Good(BFS,DP)
- 在SQL中修改某字段的部分值
- Objective-C中的instancetype和id关键字
- Mybatis 基于Java的持久层框架(一)
- OpenCV Java学习笔记(一)--Mat矩阵的数据类型转换
- coderforces 507A 贪心简单