CF Amr and Music (贪心)
1 second
256 megabytes
standard input
standard output
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 结构体排序,每次选最小。
#include <bits/stdc++.h>
using namespace std; struct Node
{
int day;
int num;
}; int comp(const void * a,const void * b);
int main(void)
{
int n,k;
int sum;
Node s[];
int ans[];
int box; scanf("%d%d",&n,&k);
for(int i = ;i < n;i ++)
{
scanf("%d",&s[i].day);
s[i].num = i + ;
}
qsort(s,n,sizeof(Node),comp); sum = box = ;
for(int i = ;i < n;i ++,box ++)
{
sum += s[i].day;
if(sum > k)
break;
ans[box] = s[i].num;
}
printf("%d\n",box);
for(int i = ;i < box;i ++)
{
printf("%d",ans[i]);
if(i + < box)
printf(" ");
}
if(box)puts(""); return ;
} int comp(const void * a,const void * b)
{
return (*(Node *)a).day - (*(Node *)b).day;
}
CF Amr and Music (贪心)的更多相关文章
- CF 115B Lawnmower(贪心)
题目链接: 传送门 Lawnmower time limit per test:2 second memory limit per test:256 megabytes Description ...
- CF Soldier and Badges (贪心)
Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- CF Amr and Pins (数学)
Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CF Anya and Ghosts (贪心)
Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- cf 853 A planning [贪心]
题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*t ...
- CF 1082E Increasing Frequency(贪心)
传送门 解题思路 贪心.对于一段区间中,可以将这段区间中相同的元素同时变成\(c\),但要付出的代价是区间中等于\(c\)的数的个数,设\(sum[i]\)表示等于\(c\)数字的前缀和,Max[i] ...
- cf 12C Fruits(贪心【简单数学】)
题意: m个水果,n个价格.每种水果只有一个价格. 问如果给每种水果分配价格,使得买的m个水果总价格最小.最大. 输出最小值和最大值. 思路: 贪心. 代码: bool cmp(int a,int b ...
- CF 161B Discounts(贪心)
题目链接: 传送门 Discounts time limit per test:3 second memory limit per test:256 megabytes Description ...
- CF 268E Playlist(贪心)
题目链接: 传送门 Playlist time limit per test:1 second memory limit per test:256 megabytes Description ...
随机推荐
- JSF 2 textbox example
In JSF, you can use the <h:inputText /> tag to render a HTML input of type="text", t ...
- redis神器
redis是内存型数据库,数据保存在内存中,通过tcp直接存取,优势是速度快,并发高,缺点是数据类型有限,查询功能不强,一般用作缓存. redis具有持久化机制,可以定期将内存中的数据持久化到硬盘上. ...
- 任务分发系统gearman
1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...
- libevent的使用方法--回显服务器的简单实例
#include <event.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...
- 改变Oracle数据库连接端口
Oracle数据库默认数据库监听与连接端口是1521, 但是有时候项目中需要更改默认端口 这样做很多时候客户要求,基于安全考虑. 以Oracle 11g 为例, 更改Listener的端口大致 需要以 ...
- 用6个字符写出任意的Javascript代码
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用6个字符写出任意的Javascript代码.
- Weblogic安装
1.下载wls_121200.jar, 2.输入cnd打开命令提示 3.f:进入F盘 4.Java -version 验证是否配置Java环境, 5.echo %path% 查看环境变量 6.set ...
- jqGrid 学习
jqGrid 学习: 一.下载需要的jqGrid包:http://www.trirand.com/blog/?page_id=6 二.下载JQuery UI:http://jqueryui.com/d ...
- asp.net中使用swfupload上传大文件
转载:http://www.cnblogs.com/niunan/archive/2012/01/12/2320705.html 花了一天多时间研究出来的,其实也就是网上下别人的代码然后再自己修修改改 ...
- Oracle_11g_R2安装详解_for_Windows_7
Oracle 11g R2安装全攻略 - For Windows 7 图文教程 1.下载Oracle 11g R2的Windows版本,官方下载地址如下: http://download.oracle ...