博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ass1_1
阅读量:4637 次
发布时间:2019-06-09

本文共 3422 字,大约阅读时间需要 11 分钟。

Aha, Maoge is coming!!

Problem 1 Maoge and Coding

Description

Maoge is a administrative officer who likes coding a lot. He always wanted to learn how to code program but he was busy with administration so he got an idea.

Maoge knows n programming languages, it takes ai days to learn i-th language. Being busy, Maoge dedicated k days to learn coding.

Maoge asked for your help to distribute his free days between programming languages so that he can achieve his goal.

Input

The first line contains two numbers n, k (1≤n≤100, 0≤k≤10000), the number of programming languages 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 language.

Output

In the first line output one integer m representing the maximum number of programming languages Maoge can learn.In the second line output m space-separated integers: the indices of programming languages 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.

Sample Input 1

4 104 3 1 2

Sample Output 1

41 2 3 4

Sample Input 2

5 64 3 1 1 2

Sample Output 2

3 1 3 4

这是助教所说的练手题,乍一看以为dp大法,定睛一看是贪心。题目大意是有k天,每门语言需要一定的时间去掌握,设计出在k天学得最多门语言的策略。嘤,只考虑门数,其他不用想。嘤,还任意输出,贪心一波就可以跑路了。排个非递减序,线性扫描,o文明k。为什么可以贪心:1)贪心选择性质:一定存在某个最优解包含耗时最小的语言(简称min)。考虑一个最优解s,不包含min,如果包含,命题得证。记s中最小的解为smin,容易得到,t(smin)>=t(min),故s'=(s-smin)|min,T(s')<=T(s)并且有|s'|=|s|,故s'一定是一个最优解命题得证。2)最优子结构:最优解s包括自问题的最优解,假设最优解s包括min,则s'=s-min是T(s-min)下的最优解,否则存在s''是T(s-min)下的最优解,用s''代替s'可导出矛盾:s不是最优解。故有s=min+s'。综上,用贪心法可以求得最优解。

1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 typedef bool(*compare)(int l,int r); 9 10 bool _less(const pair
& l, const pair
& r) { return l.second < r.second; }11 12 vector
> s;13 14 int main(void) {15 int n, k;16 cin >> n >> k;17 for (int i = 0;i < n;i++) {18 int tmp;19 cin >> tmp;20 s.push_back(make_pair(i + 1, tmp));21 }22 sort(s.begin(), s.end(), _less);23 24 25 int cnt = 0;26 27 for (int i = 0;i < s.size();i++) {28 if (k - s[i].second >= 0) {29 cnt++;30 k -= s[i].second;31 }32 else break;33 }34 cout << cnt << endl;35 36 for (int i = 0;i < cnt;i++)cout << s[i].first << " ";37 cout << endl;38 39 return 0;40 }

 

再加个限制条件?如果要在语言数最大化的情况下取得尽可能多的学习时间(我就是喜欢学习),如何?贪心法失效了,考虑下dp大法?嘤,语言不重复学习,考虑一波多阶段决策。
1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 int d[105][105]; 9 void dp(int* p, const int n, const int k) {10 for (int i = 0;i <= n;i++)d[i][0] = 0;11 for (int i = 1;i <= k;i++)d[0][i] = INT_MIN;12 13 for (int i = 1;i <= n;i++)14 for (int times = 1;times <= k;times++) {15 d[i][times] = INT_MIN;//关键,区分语言数最大化下最多能花的时间,16 if (times >= p[i - 1])d[i][times] = d[i - 1][times - p[i - 1]] + 1;17 d[i][times] = max(d[i][times], d[i - 1][times]);18 }19 20 int cost_times, max_num = -1;21 for (int times = k;times > 0;times--) {22 if (d[n][times] > max_num) {23 max_num = d[n][times];24 cost_times = times;25 }26 }27 cout << max_num << " " << cost_times << endl;28 //这里只介绍想法,懒得去打印解29 }

 

 

转载于:https://www.cnblogs.com/schsb/p/8727579.html

你可能感兴趣的文章
dateTimePicker编辑状态下,取值不正确的问题
查看>>
mac 端口转发方案
查看>>
[2017.02.23] Java8 函数式编程
查看>>
loadrunner支持https协议的操作方法-经验总结
查看>>
Knowledge Point 20180305 数据在计算机中的表示
查看>>
谈谈对web标准的理解
查看>>
DIV+CSS规范命名大全集合
查看>>
求二进制中1的个数(编程之美2.1)
查看>>
hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现
查看>>
58前端内推笔试2017(含答案)
查看>>
写给自己的web开发资源
查看>>
Java学习笔记
查看>>
sprintf 和strcpy 的差别
查看>>
打表打表何谓打表?
查看>>
MPEG4与.mp4
查看>>
实验5
查看>>
git 下载 安装
查看>>
录制终端信息并回放
查看>>
JS中window.event事件使用详解
查看>>
ES6深入学习记录(一)class方法相关
查看>>