zoukankan      html  css  js  c++  java
  • ass1_1

    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 10
    4 3 1 2

    Sample Output 1

    4
    1 2 3 4

    Sample Input 2

    5 6
    4 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 <iostream>
     2 #include <set>
     3 #include <vector>
     4 #include <algorithm>
     5  
     6 using namespace std;
     7  
     8 typedef bool(*compare)(int l,int r);
     9  
    10 bool _less(const pair<int, int>& l, const pair<int, int>& r) { return l.second < r.second; }
    11  
    12 vector <pair<int,int> > 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 <iostream>
     2 #include <algorithm>
     3 #include <list>
     4 #include <vector>
     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 }


  • 相关阅读:
    了解自己,才能踏出成功的第一步
    21世纪非常成功心法[强烈推荐]
    先做人后做事的庸才文化
    职业生涯发展技巧20则
    21世纪非常成功心法[强烈推荐6]
    豁达者的诱惑
    20个问题:平凡的天才人格特质
    21世纪非常成功心法[强烈推荐5]
    测测你的人生会走怎样路线
    21世纪非常成功心法[强烈推荐7]
  • 原文地址:https://www.cnblogs.com/schsb/p/8727579.html
Copyright © 2011-2022 走看看