码迷,mamicode.com
首页 > 其他好文 > 详细

尼克的任务

时间:2018-10-27 01:26:47      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:www   algorithm   mes   tchar   \n   休息   结束   bool   while   

传送门

这道题虽然很基础但是也不是很好想……

我们一开始会想,用dp[i]表示前i分钟最大休息时间,之后你就完全不知道怎么转移了。

阻碍你转移的关键问题在于,你不知道后面会发生什么,但是如果从后面倒着DP的话就可以了。因为这样的话,你后面的过程相当于是已知的。我们只要在记录一下以这个点为开始的工作有多少,之后直接从那个工作的结束转移过来即可。如果没工作,直接从上一个位置转移即可。

看一下代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar(‘\n‘)

using namespace std;
typedef long long ll;
const int M = 20005;
const int INF = 1000000009;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < 0 || ch > 9)
    {
    if(ch == -) op = -1;
    ch = getchar();
    }
    while(ch >= 0 && ch <= 9)
    {
    ans *= 10;
    ans += ch - 0;
    ch = getchar();
    }
    return ans * op;
}

struct mis
{
    int p,t;
    bool operator < (const mis &g) const
    {
        return p > g.p;
    }
}a[M];

int n,k,sum[M],dp[M],cnt = 1;

int main()
{
    n = read(),k = read();
    rep(i,1,k) a[i].p = read(),a[i].t = read(),sum[a[i].p]++;
    sort(a+1,a+1+k);
    per(i,n,1)
    {
    if(!sum[i]) dp[i] = dp[i+1] + 1;
    else
    {
        rep(j,1,sum[i]) dp[i] = max(dp[i + a[cnt].t],dp[i]),cnt++;
    }
    }
    printf("%d\n",dp[1]);
    return 0;
}

 

尼克的任务

标签:www   algorithm   mes   tchar   \n   休息   结束   bool   while   

原文地址:https://www.cnblogs.com/captain1/p/9859353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!