zoukankan      html  css  js  c++  java
  • 【YBTOJ】【POJ 1821】Fence

    题目大意:

    (n) 块木板从左到右排成一行,有 (m) 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次。

    (i) 个木匠要么不粉刷,要么粉刷包含木板 (S_i) 且长度不超过 (L_i) 的连续的一段木板,每粉刷一块可以得到 (P_i) 的报酬。不同工匠的 (S_i) 不同。 请问如何安排能使工匠们获得的总报酬最多。

    思路:

    (f_{i,j}) 表示前 (i) 个人涂完前 (j) 段木板的最大报酬。则有:

    [f_{i,j}=max(f_{i-1,j},f_{i,j-1},max_{j-L_ileq k leq S_i-1}{f_{i-1,k} + (j-k)P_i}) ]

    单调队列优化即可。

    代码:

    const int N = 16010, M = 110;
    
    inline ll Read()
    {
    	ll x = 0, f = 1;
    	char c = getchar();
    	while (c != '-' && (c < '0' || c > '9')) c = getchar();
    	if (c == '-') f = -f, c = getchar();
    	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
    	return x * f;
    }
    
    int n, m;
    int f[M][N];
    struct node
    {
    	int L, P, S;
    	bool operator < (const node &a) const
    	{
    		return S < a.S;
    	}
    }a[M];
    
    int q[N];
    
    int main()
    {
    //	freopen(".in", "r", stdin);
    //	freopen(".out", "w", stdout);
    	n = Read(), m = Read();
    	for (int i = 1; i <= m; i++)	
    		a[i].L = Read(), a[i].P = Read(), a[i].S = Read();
    	sort (a + 1, a + 1 + m); 
    	for (int i = 1; i <= m; i++)
    	{
    		int h = 1, t = 0;
    		for (int j = max(0, a[i].S - a[i].L); j <= a[i].S - 1; j++)
    		{
    			while (h <= t && f[i-1][q[t]] - q[t] * a[i].P <= f[i - 1][j] - j * a[i].P) t--;
    			q[++t] = j;	
    		}
    		for (int j = 1; j <= n; j++)
    		{
    			f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    			if (j >= a[i].S)
    			{
    				while (h <= t && q[h] < j - a[i].L) h++;
    				if (h <= t) f[i][j] = max(f[i][j], j * a[i].P + f[i-1][q[h]] - q[h] * a[i].P);
    			}
    		}
    	}
    	printf ("%d
    ", f[m][n]);
    	return 0;
    }
    
    
  • 相关阅读:
    [LeetCode] Meeting Rooms I & II
    [LeetCode] Graph Valid Tree
    [LeetCode] Palindrome Permutation I & II
    [LeetCode] Encode and Decode Strings
    [LeetCode] Paint Fence
    android 发送短信功能
    andrioid 分享到其它(短信,qq,微信等功能)
    android Dialog实例
    android开发之onCreate( )方法详解
    android edittext属性说明
  • 原文地址:https://www.cnblogs.com/GJY-JURUO/p/15151852.html
Copyright © 2011-2022 走看看