【DP】【P5080】 Tweetuzki 爱序列

Description

Tweetuzki 有一个长度为 \(n\) 的序列 \(a_1~,~a_2~,~\dots~,a_n\)

他希望找出一个最大的 \(k\),满足在原序列中存在一些数 \(b_1~,~b_2~,~\dots~,b_n\) (可打散在原序列中的顺序),满足 \(\forall~i~\in~[1,k)~,~b_i~\div~3~=~b~_{i+1}\)(这时 \(b_i\) 必须能够被 \(3\) 整除)或 \(b_i~\times~2~=~b_{i+1}\)。并输出这个序列。

Input

第一行一个正整数 \(n\) 代表序列长度

下面一行 \(n\) 个数代表这个序列

Output

第一行输出最大的 \(k\)

第二行输出任意一个可行的方案

Hint

\(n~\leq~10^5~,~1~\leq~a_i~\leq~3~\times~10^{18}\)

Solution

考虑DP。

\(f_i\) 是以 \(i\) 这个数结尾的最长 ans,于是 \(f_i~=~\max(f_{i\div3}~,~f_{i\times2})~+~1\) 。其中若从第一项转移过来则必须 \(3|x\),能从一个数转移当且仅当这个数在序列中出现过。

考虑这么做为什么是可以的,会产生可不可以的问题因为一个转移位置比 \(i\) 大,一个转移位置比 \(i\) 小,为什么转移不会存在环。

反证法,考虑转移图存在环的情况,则有

\[x~\times~(\frac{1}{3})^a~\times~2^b~=~x \]

\[\Rightarrow~(\frac{1}{3})^a~\times~2^b~=~1~,~a,b~\in~Z^+ \]

然而这个方程一定是无解的。考虑分母是 \(3^a\),恒为一个奇数,分子是 \(2^b\),恒为一个偶数。一个偶数和一个奇数约分永不可能得 \(1\)。于是方程无解,于是转移图不存在环。证毕。

下一个问题是按照什么顺序更新DP,dzy神仙是按照幂次的不降序更新的,rqy神仙是按照幂次分类更新的,反正这俩我都不会,于是怒写一发记忆化搜索乱搞一波,管他什么顺序更新(雾

考虑 \(n\) 特别小,\(O(n\log n)\)挂上一大串常数都没问题,于是直接用map存DP值,再用map存是否出现,最后再用map存转移方向即可。

Code

#include <map>
#include <vector>
#include <cstdio>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long

typedef long long int ll;

namespace IPT {
	const int L = 1000000;
	char buf[L], *front=buf, *end=buf;
	char GetChar() {
		if (front == end) {
			end = buf + fread(front = buf, 1, L, stdin);
			if (front == end) return -1;
		}
		return *(front++);
	}
}

template <typename T>
inline void qr(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
	while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
	if (lst == '-') x = -x;
}

template <typename T>
inline void ReadDb(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
	while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
	if (ch == '.') {
		ch = IPT::GetChar();
		double base = 1;
		while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
	}
	if (lst == '-') x = -x;
}

namespace OPT {
	char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
	if (x < 0) {x = -x, putchar('-');}
	rg int top=0;
	do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
	while (top) putchar(OPT::buf[top--]);
	if (pt) putchar(aft);
}

const int maxn = 100010;

int n;
ll ans;
ll MU[maxn];
std::map<ll,int>frog;
std::map<ll,bool>oc;
std::map<ll, ll>pre;

ll dfs(ll);
void print(ll);

int main() {
	freopen("1.in", "r", stdin);
	qr(n);
	for (rg int i = 1; i <= n; ++i) qr(MU[i]);
	for (rg int i = 1; i <= n; ++i) oc[MU[i]] = true;
	for (rg int i = 1; i <= n; ++i) {
		frog[MU[i]] = dfs(MU[i]);
		if (frog[ans] < frog[MU[i]]) ans = MU[i];
	}
	qw(frog[ans], '\n', true);
	print(ans);
}

ll dfs(ll x) {
	if (frog[x]) return frog[x];
	ll tp = 1, ttp = 0;
	if (!(x % 3)) {
		if (oc[x / 3]) tp = dfs(x / 3) + 1, ttp = x / 3;
	}
	if (oc[x << 1]) {
		ll qwq = dfs(x << 1) + 1;
		if (tp < qwq) tp = qwq, ttp = x << 1;
	}
	pre[x] = ttp;
	return frog[x] = tp;
}

void print(ll x) {
	if (!x) return;
	qw(x, ' ', true);
	print(pre[x]);
}
posted @ 2018-12-09 16:09  一扶苏一  阅读(250)  评论(0编辑  收藏  举报