CF1110D Jongmah

The (sougoupinyin) on my laptop has been broken for several days, which drives me crazy. Then I realized I can avoid Chinese if I wanted. So here comes a new try with my (piejiao) English.

[CF1110D] Jongmah , Luogu
Calculate the maximum number of triples when using (n) numbers given, which also meets (a_i le m).
Illegal triples include (Chi(i, i + 1. i + 2)) and (Peng(i, i, i)).
$n, m le 10^6 $.

**Suppose (dp[i][j][k]) represents the maximum triples at i, having j times ([i - 1, i, i + 1]), k times ([i, i + 1, i + 2]). **
Enumerating j,k,l from 0 to 2, here comes the equation:
(chkmax(dp[i][k][l], dp[i - 1][j][k] + (cnt[i] - j - k - l) / 3 + l);) // starts with i, contributes l.
The answer is (dp[m][0][0]).

(O(n))

Summary
Individually considering every state, which was my first idea, remains a mess. The key is how to calculate the contribution created now. One possible solution is to transfer (Chi), add (Peng). Adding (Peng) refers to i, don't forget to add (l) due to (Chi) starts with i.

Finally we all realize that Chinese is so brief, compared with English's stinky and long. It also reflects why Chinese (teenagers) achieve higher grades than teenagers in EUR&USA. However, we have to use English one day.

#include<cstdio>ss
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define Debug(x) cout<<#x<<"="<<x<<endl
using namespace std;
typedef long long LL;
const int INF=1e9+7;
inline LL read(){
	register LL x=0,f=1;register char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
	return f*x;
}

const int N = 1e6 + 5;

int dp[N][3][3], cnt[N];
int n, m;

inline void chkmax(int& a, int b){ a = a > b ? a : b; }

int main(){
	n = read(), m = read();
	for(int i = 1; i <= n; ++i) ++cnt[read()];
	memset(dp, -1, sizeof dp);
	dp[0][0][0] = 0;
	for(int i = 1; i <= m; ++i)
		for(int j = 0; j < 3; ++j)
			for(int k = 0; k < 3; ++k)
				for(int l = 0; l < 3; ++l){
					if(j + k + l > cnt[i]) continue;
					chkmax(dp[i][k][l], dp[i - 1][j][k] + (cnt[i] - j - k - l) / 3 + l); // starts with i, contributes l.
				}
	printf("%d
", dp[m][0][0]);
}
原文地址:https://www.cnblogs.com/lizehon/p/11239365.html