제출 #507149

#제출 시각아이디문제언어결과실행 시간메모리
507149abc864197532Longest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
4398 ms48040 KiB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l)
		cout << *l << " \n"[l + 1 == r];
}
#define test(x...) abc("[" + string(#x) + "]", x);
const int N = 200000;

int bucket[1 << 10][11][1 << 10], popcnt[1 << 10];

int main () {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector <int> a(n), k(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0; i < n; ++i)
		cin >> k[i];
	for (int s = 1; s < 1 << 10; ++s)
		popcnt[s] = popcnt[s ^ (s & (-s))] + 1;
	vector <int> dp(n, 1), bck(n, -1);
	auto upd = [&](int &i, int j) {
		if (i == -1)
			i = j;
		else if (dp[i] < dp[j])
			i = j;
	};
	for (int s = 0; s < 1 << 10; ++s) for (int i = 0; i < 10; ++i) for (int t = 0; t < 1 << 10; ++t)
		bucket[s][i][t] = -1;
	for (int i = 0; i < n; ++i) {
		for (int s = 0; s < 1 << 10; ++s) for (int j = 0; j < 10; ++j) {
			if (popcnt[s & (a[i] >> 10)] != k[i] - j)
				continue;
			int x = bucket[a[i] & 1023][j][s];
			if (x != -1 && dp[i] < dp[x] + 1)
				dp[i] = dp[x] + 1, bck[i] = x;
		}
		for (int s = 0; s < 1 << 10; ++s) {
			upd(bucket[s][popcnt[s & a[i]]][a[i] >> 10], i);
		}
	}
	int mx = max_element(all(dp)) - dp.begin();
	cout << dp[mx] << endl;
	vector <int> ans;
	while (mx != -1) {
		ans.pb(mx + 1);
		mx = bck[mx];
	}
	reverse(all(ans));
	printv(all(ans));
	for (int i = 1; i < ans.size(); ++i) {
		if (__builtin_popcount(a[ans[i - 1] - 1] & a[ans[i] - 1]) != k[ans[i] - 1])
			cout << "fail\n" << i << endl;
	}
}

/*
5
5 3 5 3 5
10 1 20 1 20

4
1 2 3 4
10 0 1 0
*/

컴파일 시 표준 에러 (stderr) 메시지

subsequence.cpp: In function 'int main()':
subsequence.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |  for (int i = 1; i < ans.size(); ++i) {
      |                  ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...