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 加锁处理分析
MySQL 加锁处理分析 发表于 2013 年 12 月 13 日 由 hedengcheng 1 背景 1 1.1 MVCC:Snapshot Read vs Current Re ...
- iOS学习之自动布局
Autolayout: 最重要的两个概念: 约束:对控件位置和大小的限定条件 参照:对控件设置的约束是相对于哪一个视图而言的 自动布局的核心计算公式: obj1.property1 =(obj2.pr ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Zabbix3.0完整部署
0. 准备工作 0.1 初始化 #!/bin/sh yum clean all systemctl stop firewalld.service systemctl disable firewalld ...
- java获取照片相关属性
package test; import java.io.File; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetad ...
- iOS开发-block使用与多线程
Block Block封装了一段代码,可以在任何时候执行 Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值. 苹果官方建议尽量多用block.在多线程.异步任务.集合遍历. ...
- JS 之如何在插入元素时插在原有元素的前面而不是末尾
语法: 父级.insertBefore(新元素,被插入的元素): //在指定的元素前面加入一个新元素 父级.insertBefore(新元素,父级.children[0]); //在 ...
- PL/pgSQL的 RETURN NEXT例子
从网上找到例子: 可以说,RETURN NEXT要用在循环中: 例子一: 数据准备: CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); ...
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
本文将介绍jQuery调用基于.NET Framework 3.5的WebService返回JSON数据,另外还要介绍一下用jQuery调用WebService的参数设置及设置不当所出现的问题,还有出 ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...