首页 > 其他分享 >Trick

Trick

时间:2023-04-15 12:00:45浏览次数:45  
标签:5005 Codeforces long Trick Problem dp mod

Trick

Do we really need to visit all the states?

Sometimes, the naive dp solution to a problem might take too long and too much memory. However, sometimes it is worth noting that most of the states can be ignored because they will never be reached and this can reduce your time complexity and memory complexity.

有时,问题的简单dp解决方案可能会占用太长时间和太多内存。然而,有时值得注意的是,大多数状态可以被忽略,因为它们永远不会被到达,这可以降低您的时间复杂性和内存复杂性。

Problem - C - Codeforces

Change the object to dp

更改dp的对象,选择小的dp

Problem - C - Codeforces

Problem - E - Codeforces

Open and Close Interval Trick

Problem - F - Codeforces

Problem - D - Codeforces

Problem - E - Codeforces

"Connected Component" DP

P5999 [CEOI2016] kangaroo

#include<bits/stdc++.h>
using namespace std;
long long n, m, k, i, j, f[2005][2005];
const int mod = 1e9 + 7;
int main()
{
	cin >> n >> m >> k;
	f[0][0] = 1;
	for (i = 1; i <= n; i++)
	{
		for (j = 0; j <= n; j++)
		{
			if ((i != m) and (i != k))
			{
				(f[i][j] += f[i - 1][j + 1] * j) %= mod;
				if (j) (f[i][j] += f[i - 1][j - 1] * (j - (i > m) - (i > k))) %= mod;
			}
			else
			{
				(f[i][j] += f[i - 1][j]) %= mod;
				if (j) (f[i][j] += f[i - 1][j - 1]) %= mod;
			}
		}
	}
	cout << f[n][1] << '\n';
}

摩天大楼

#2743. 「JOI Open 2016」摩天大楼 - 题目 - LibreOJ (loj.ac)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, i, j, k, u, o, p, l, s, f[105][105][1005][3], a[105];
const int mod = 1e9 + 7;
int main()
{
	cin >> n >> m;
	for (i = 1; i <= n; i++)
		cin >> a[i];
	if (n == 1) { puts("1"); return 0; }
	sort(a + 1, a + n + 1);
	f[0][0][0][0] = 1;
	for (i=0;i<n;i++)
		for (j=0;j<=i;j++)
			for (k=0;k<=m;k++)
				for (u = 0; u <= 2; u++)
				{
					o = a[i+1] - a[i];
					if (i == 0) o = 0;
					p = f[i][j][k][u];
					l = k + o * (j * 2 - u);
					if ((l > m) or (!p))continue;
					if (j > 1) (f[i + 1][j - 1][l][u] += p * (j - 1))%=mod;
					(f[i + 1][j + 1][l][u] += p * (j + 1 - u))%=mod;
					if (u == 0)
						(f[i + 1][j + 1][l][u + 1] += p * 2)%=mod;
					if (u == 1)
						(f[i + 1][j + 1][l][u + 1] += p)%=mod;
					(f[i + 1][j][l][u] += p * (j * 2 - u))%=mod;
					if ((j)and(u == 0))
						(f[i + 1][j][l][u+1] += p * 2)%=mod;
					if ((j)and(u == 1))
						(f[i + 1][j][l][u + 1] += p)%=mod;
				}
	for (i = 0; i <= m; i++)
		s = (s + f[n][1][i][2])%mod;
	cout << s << '\n';
}

Ant Man

Problem - 704B - Codeforces

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, s, e, i, j, k, q, a[5005], b[5005], c[5005], d[5005], x[5005], f[5005][5005];
int main()
{
	cin >> n >> s >> e;
	for (i = 1; i <= n; i++)
		cin >> x[i];
	for (i = 1; i <= n; i++)
		cin >> a[i];
	for (i = 1; i <= n; i++)
		cin >> b[i];
	for (i = 1; i <= n; i++)
		cin >> c[i];
	for (i = 1; i <= n; i++)
		cin >> d[i];
	for (i = 0; i <= n; i++)
		for (j = 0; j <= n; j++)
			f[i][j] = 1e18;
	f[0][0] = 0;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j <= n; j++)
		{
			if ((j == 1) and (k == 2))
				f[i][j] = 1e18;
			if (f[i][j] < 1e18)
			{
				if (i + 1 == s)
				{
					if (j < n) f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + d[i + 1] - x[i + 1]);
					if (j) f[i + 1][j] = min(f[i + 1][j], f[i][j] + c[i + 1] + x[i + 1]);
				}
				else
					if (i + 1 == e)
					{
						if (j < n) f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + b[i + 1] - x[i + 1]);
						if (j) f[i + 1][j] = min(f[i + 1][j], f[i][j] + a[i + 1] + x[i + 1]);
					}
					else
					{
						if (j < n) f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + b[i + 1] + d[i + 1] - x[i + 1] - x[i + 1]);
						if (j > 1) f[i + 1][j - 1] = min(f[i + 1][j - 1], f[i][j] + a[i + 1] + c[i + 1] + x[i + 1] + x[i + 1]);
						if (j)
						{
							if ((e > i + 1) or (j > 1)) f[i + 1][j] = min(f[i + 1][j], f[i][j] + a[i + 1] + d[i + 1]);
							if ((s > i + 1) or (j > 1)) f[i + 1][j] = min(f[i + 1][j], f[i][j] + c[i + 1] + b[i + 1]);
						}
					}
			}
		}
		k = k + ((i + 1 == s) or (i + 1 == e));
	}
	cout << f[n][1] << '\n';
}

标签:5005,Codeforces,long,Trick,Problem,dp,mod
From: https://www.cnblogs.com/ShadowAA/p/17320829.html

相关文章

  • CF429D Tricky Function 题解 分治/平面最近点对
    题目链接:http://codeforces.com/problemset/problem/429/D题目大意:给定一个长度为\(n\)的数列\(a_1,a_2,\ldots,a_n\)。用\(s\)表示\(a\)的前缀和数组,即\(s_......
  • The Barton-Nackman Trick 介绍
    问题说明什么是TheBarton-NackmanTrick?1994年,Barton和Nackman提出的一种模板编程技巧,由于当时模板函数重载和命名空间等机制不完善的背景下提出的一种技巧,现在通常配合CRT......
  • 常见trick总结
    记录一些做题时遇到的有价值的trick。CF1717E\[a+b=n\]\[\gcd(a,b)=\gcd(a,a+b)=\gcd(a,n)=\varphi(n)\]P2619二分\(\Delta\),每条白边加上\(\Delta\)求\(\te......
  • Trick:最小环
    求无负环无向图至少具有三个节点的最小简单环,考虑最小的简单环\(x_1x_2x_3\cdotsx_nx_1,n\ge3\),不妨令\(x_n=max\{x_i\}\),那么显然有\(x_1x_2\cdotsx_{n-1}\)是只经......
  • 常见 Trick
    二维凸包的点数在随机情况下是\(O(\logn)\)的。树的高度在随机情况下是\(O(\logn)\)的。要求最大值最小值的时候,有三个方向:min-max容斥,二分,直接求。条件很复杂......
  • pairwise损失_triplet损失_提升精排模型的trick
    多标签importtorchimporttorch.nnasnnimporttorch.optimasoptimclassRankModel(nn.Module):def__init__(self,num_features):super(RankMode......
  • A. Amazing Trick
    A.AmazingTrick思路对于p数组,每个数只有两个禁止的位置不能是自己,并且a[pi]也不是。也就是每个点限制有两个位置不能放,可行的种类有很多,但是模拟起来又较复杂,所以采......
  • 开发者进阶必备的9个Tips & Tricks!
    优秀的开发人员市场前景是十分广阔的,但想找到一份理想的工作,仅有代码知识是不够的。优秀的工程师应该是一个终身学习者、问题的创造性解决者,着迷于整个软件世界。要成为一......
  • BJGK Che's Trick
    神秘把读题里“有效的信息”圈出来,务必要体现在答案里亚硫磷亚硝;氟乙碳氢硫;次氯氢氰硅;苯酚碳氢铝过滤完,固体蘸着液体,一定要洗涤。电泳,交替,可以吸附正负离子。......
  • Slope Trick
    原理若一个函数满足:连续分段线性凸性则可以使用SlopeTrick来快速维护。我们发现我们可以仅通过记录转折点,转折点处斜率变化,以及一侧的直线即可维护出整个函数。......