Luogu P1280 尼克的任務題解

來源:互聯網
上載者:User

標籤:put   read   putc   放心   順序   book   write   include   sim   

思路

首先我們需要知道主角可以休息的最多的時間,那麼我們可以設\(dp[i]\)為第\(i\)分鐘可以休息的最大時間那麼我們可以發現假如第\(i\)分鐘沒有主角沒有任何任務,那麼主角就可以放心休息轉移方程為\(dp[i] = dp[i + 1] + 1\)。如果主角在當前第\(i\)分鐘裡面有任務那麼在\(i \sim i + t - 1\)這段時間裡面就別想休息了(\(t\)為當前任務的時間)。所以我們可以得到轉移方程為\(dp[i] = dp[i + t]\) 最後注意一下枚舉的順序就可以了。

代碼
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int N = 1e4 + 5 ;int n, k;int book[N];int dp[N];struct STR { int ts; //任務開始的時間 int t; //任務持續的時間} Str[N];int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * w;}void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0');}int main(int argc, char const *argv[]) { n = read(), k = read(); for (register int i = 1; i <= k; ++i) { Str[i].ts = read(), Str[i].t = read(); book[Str[i].ts] = 1; //標記開始的時間 } for (register int i = n; i >= 1; --i) { //枚舉時間 if (!book[i]) { dp[i] = dp[i + 1] + 1; continue; } for (register int j = 1; j <= k; ++j) { //枚舉任務 if (Str[j].ts == i) dp[i] = max(dp[i], dp[i + Str[j].t]); } } write(dp[1]); return 0;}

Luogu P1280 尼克的任務題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.