zoukankan      html  css  js  c++  java
  • URAL1696 Salary for Robots

    题目戳这里
    最长下降子序列单调队列求法。
    (f_{i,j,k})表示考虑前(i)个数,(g_1 = j,g_2 = k)的方案数。转移:
    $$f_{i,j,k} = sum_{p = k+1}{j}f_{i-1,p,k}+sum_{p=0}kf_{i-1,j,p}$$
    二维前缀和优化。复杂度(O(NK^2))

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    
    const int maxk = 201;
    int f[2][maxk][maxk],N,K,rhl,ans;
    
    int main()
    {
    	freopen("1696.in","r",stdin);
    	freopen("1696.out","w",stdout);
    	scanf("%d %d %d",&N,&K,&rhl);
    	for (int i = 1;i <= K;++i) f[1][i][0] = 1;
    	for (int p = 2,now = 0,last = 1,inc;p <= N;++p,swap(now,last))
    	{
    		for (int i = 1;i <= K;++i)
    		{
    			for (int j = i-1;j >= 0;--j)
    			{
    				f[last][i][j] += f[last][i][j+1];
    				if (f[last][i][j] >= rhl) f[last][i][j] -= rhl;
    			}
    			for (int j = 0;j < i;++j)
    			{
    				f[last][i][j] += f[last][i-1][j];
    				if (f[last][i][j] >= rhl) f[last][i][j] -= rhl;
    			}
    		}
    		memset(f[now],0,sizeof f[now]);		
    		for (int i = 1;i <= K;++i)
    			for (int j = 0;j < i;++j)
    			{
    				f[now][i][j] = f[last][i][j]-f[last][j][j]-f[last][i][j+1]+f[last][j][j+1];
    				while (f[now][i][j] >= rhl) f[now][i][j] -= rhl;
    				while (f[now][i][j] < 0) f[now][i][j] += rhl;
    				if (j)
    				{
    					inc = f[last][i][0]-f[last][i-1][0]-f[last][i][j+1]+f[last][i-1][j+1];
    					while (inc >= rhl) inc -= rhl; while (inc < 0) inc += rhl;
    					f[now][i][j] += inc; if (f[now][i][j] >= rhl) f[now][i][j] -= rhl;
    				}
    			}
    	}
    	for (int i = 1;i <= K;++i)
    		for (int j = 0;j < i;++j)
    		{
    			ans += f[N&1][i][j];
    			if (ans >= rhl) ans -= rhl;
    		}
    	printf("%d
    ",ans+1);
    	fclose(stdin); fclose(stdout);
    	return 0;
    }
    
    
  • 相关阅读:
    意大利多洛米蒂 随手一拍都是大片
    改善恋爱关系的7个方法
    如何不花钱就能找到乐子
    蜜蜂会发现蜜没了吗?
    领导艺术:分权失败5宗罪
    如何正确地表白:勇敢不等于鲁莽
    现代社会奇葩家庭
    老板凭啥提拔你:如何得到老板赏识
    在科技行业谈待遇的四个技巧
    JS判断页面是否为浏览器当前页
  • 原文地址:https://www.cnblogs.com/mmlz/p/6403891.html
Copyright © 2011-2022 走看看