貪心初步 hdu1789 Doing Homework again

來源:互聯網
上載者:User

標籤:

一道非常經典的題目

題目地址

題目大意

給出N個作業的截至日期,和N個作業不交所扣掉的分數,要求輸出扣除分數做少的方案。

正確的策略是:

  1. 扣除分數大的先做
  2. 扣除分數相同,先截止的先做
  3. 做一件事的時候,從截止時間開始向第一天遍曆,如果當天沒有被作業佔據則標記為佔據。做這件事的日期越大越好。
  4. 如果不能滿足3的條件,則為不能完成
 1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <string.h> 5  6 #define MAX 1010 7  8 using namespace std; 9 int V[MAX];10 int E[MAX];11 bool t[MAX];12 int order[MAX];13 14 void init()15 {16     memset(t,0,MAX * sizeof(bool));17     return ;18 }19 20 bool cmpA (int a ,int b)21 {22     if(V[a] > V[b])23     {24         return 1;25     }26     else if(V[a] == V[b] && E[a] < E[b])27     {28         return 0;29     }30     else 31         return 0;32 }33 34 int main()35 {36     int T;37     while(~scanf("%d",&T))38     {39         for (int i = 0; i < T; ++i)40         {41             init();42             int N;43             int All = 0;44             int ans = 0;45             scanf("%d",&N);46             for (int j = 0; j < N; ++j)47             {48                 scanf("%d",&E[j]);49             }50             for (int j = 0; j < N; ++j)51             {52                 scanf("%d",&V[j]);53                 All = All + V[j];54                 order[j] = j;55             }56             //////////////////////////57             sort(order,order + N,cmpA);58             /////////////////////////////59             //////////////////////////////60             for (int j = 0; j < N; ++j)61             {62                 int event = order[j];63                 bool flag = 0;64                 for (int k = E[event]; k >= 1; k--)65                 {66                     if(!t[k])67                     {68                         ans = ans + V[event];69                         t[k] = 1;70                         flag = 1;71                     }72                     if(flag) break;73                 }74             }75             ///////////////////////////76             cout << All - ans << endl;77 78         }79     }80     return 0;81 }

 

貪心初步 hdu1789 Doing Homework again

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.