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 ...
随机推荐
- MYSQL event_scheduler
一.概述 事件调度器是在 MySQL 5.1 中新增的另一个特色功能,可以作为定时任务调度器,取代部分原先只能用操作系统任务调度器才能完成的定时功>能.例如,Linux 中的 crontabe ...
- 查看Linux里某文件的前面/后面几行中的某一行
如,我想看/etc/profile文件的前5行里的第5行. 则, head -5 /etc/profile | tail -1 管道|啊,很简单,就是把左边命令的结果,作为右边的输入. 如, ...
- notepad++汉字突然横过来了
修改notepad++,汉字突然横过来了,如图, 百度了一下,原来是因为选择的字体"@微软雅黑"前面的@符号惹的祸,改成"微软雅黑"就没事了.
- Spring MVC SimpleUrlHandlerMapping example
In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, w ...
- 转载 JQuery中attr属性和JQuery.data()学习
转载原地址: http://www.cnblogs.com/yeminglong/p/5405745.html 用html直接data-key来存放,key必须全部小写. <div data-m ...
- HDU 3664 Permutation Counting (DP)
题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...
- UVa 11971 Polygon (数学,转化)
题意:一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率. 析:这个题,很明显和 n 是没有任何关系的,因为无论 n 是多少那切多少段都可以,只与切多少段有 ...
- c++出错记录
错误1如下: vector<vector<int>> m_vc; error: '&' cannot appear in a constant-expression ...
- MFC实现数独(2)
主要功能描述: 运行程序后对话框会显示一个9x9的待输入数独区域,并提供随机生成数独和生成数独按钮,生成数独按钮后会创建数独并随机显示其中一个至数独区域,随机生成数独会从已生成的数独中随机获取一个并显 ...
- corpus academic writing
http://micusp.elicorpora.info/ http://corpus.byu.edu/coca/ http://rcpce.engl.polyu.edu.hk/RACorpus/