【羅谷】P9242 [Blue Bridge Cup 2023 B県] ソリティアシーケンスの解答

【羅谷】P9242 [Blue Bridge Cup 2023 B県] ソリティアシーケンスの解答

トピックポータル

一連の考え

まず、数値の最初の桁と最後の桁に注意するだけでよく、これはfi \mathit{f}_{i}として定義されます。f私は数字iiの場合iで終わる最長のソリティア シーケンスの長さ各数値の最初の桁をaaa、最後の数字はbbbの場合、伝達方程式が成り立ちます:fb = max ⁡ ( fb , fa + 1 ) \mathit{f}_{b}=\max(\mathit{f}_{b},\mathit{f}_{ a} +1)fb=最大( fbf+1 )

最後にf 0 \mathit{f}_{0}でf0, f 1 … f 9 \mathit{f}_{1} \dots \mathit{f}_{9}f1f9最大限の回答を取得しますan s、答えはn − ansn − ansnです _

コード

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
    
    
	inline int read() {
    
    
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
    
    
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
    
    
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int n, t, a[200005], b[200005], f[10];
int main() {
    
    
	int t = 1;
	while(t --) {
    
    
		n = read();
		for (int i = 1; i <= n; i ++) {
    
    
			int x;
			cin >> x;
			b[i] = x % 10;
			string s = to_string(x);
			a[i] = s[0] - '0';
		}
		for (int i = 1; i <= n; i ++) {
    
    
			f[b[i]] = max(f[b[i]], f[a[i]] + 1);
		}
		int ans = 0;
		for (int i = 0; i <= 9; i ++) {
    
    
			ans = max(ans, f[i]);
		}
		write(n - ans);
	}
	return 0;
}

おすすめ

転載: blog.csdn.net/ZH_qaq/article/details/130656963