Trick

Posted ShadowAA

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Trick相关的知识,希望对你有一定的参考价值。

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\';

实用Trick

实用Trick

开栈(跑大样例必备)

-Wl,--stack=134217728

手动开O2(能快一倍?)

#pragma GCC optimize(2)

五十行优化(非必须)

#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

快读快输(卡常必备)

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template <typename T>
void write(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) write(x / 10);
    putchar('0' + x % 10);
}

超级快读快输

static char buf[100000],*p1=buf,*p2=buf;
#define gc p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
inline int read()
{
    register int x=0;register char a=gc;
    while(a<'0'||a>'9')a=gc;
    while(a>='0'&&a<='9')x=(x<<1)+(x<<3)+(a^48),a=gc;
    return x;
}
char pbuf[10000000] , *pp = pbuf;
inline void write(int x)
{
    static int sta[35];
    register int top = 0;
    if(!x)sta[++top]=0;
    while(x) sta[++top] = x % 10 , x /= 10;
    while(top) *pp ++ = sta[top -- ] ^ '0';
}

notice: 使用快速输出时应按照如下规则:

for (; t != ed; ++t) write(*t), *pp ++ = ' ';
fwrite(pbuf , 1 , pp - pbuf , stdout);

以上是关于Trick的主要内容,如果未能解决你的问题,请参考以下文章

一个小 Trick

CV知识学习early stopregularationfine-tuning and some other trick to be known

trick合集

Trick

[Mathematics][Fundamentals of Complex Analysis][Small Trick] The Trick on drawing the picture of sin

深度学习刷SOTA的一堆trick