Submission #486682

#TimeUsernameProblemLanguageResultExecution timeMemory
486682my04Longest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
149 ms6708 KiB
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define sz(x) (int)(x).size()
#define S second
#define F first
#define all(x) (x).begin(), (x).end()

using namespace std;
using ll = long long;

void setIO(string name = "") {
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);

	if (sz(name)) {
		freopen((name + ".in").c_str(), "r", stdin);
		freopen((name + ".out").c_str(), "w", stdout);
	}
}

const int inf = 1e9;
const ll INF = 1e18;            
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;

int n;
int a[MAXN], k[MAXN];
int dp[MAXN], par[MAXN];
int last[MAXN];
set<pair<int, int>> s[305];

void solve() {
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	
		dp[i] = 1;
		par[i] = -1;
	}

	for (int i = 1; i <= n; i++) {
		cin >> k[i];
	}

	if (n <= 5000) {
	
		for (int i = 1; i <= n; i++) {
			for (int j = i + 1; j <= n; j++) {
				int x = (a[i] & a[j]);
			                     
				if (__builtin_popcount(x) == k[j]) {
					if (dp[j] < dp[i] + 1) {
						dp[j] = dp[i] + 1;
						par[j] = i;
					}
				}
			}
		}

	}


	
	else {
		s[a[1]].insert(mp(1, 1));

		for (int i = 2; i <= n; i++) {
			for (int x = 0; x <= 256; x++) {
				if (s[x].empty() || __builtin_popcount(x & a[i]) != k[i]) continue;

				int ind = (*s[x].rbegin()).S;			
		
				if (dp[i] < dp[ind] + 1) {
					dp[i] = dp[ind] + 1;
					par[i] = ind;
				}		
			}

			s[a[i]].insert(mp(dp[i], i));
		}
	}

	int x = 1;
	for (int i = 2; i <= n; i++) {
		if (dp[i] > dp[x]) x = i;
	}

	cout << dp[x] << '\n';

	vector<int> path;

	while (x != -1) {
		path.pb(x);
		x = par[x];
	}

	reverse(all(path));

	for (int i : path) {
		cout << i << ' ';
	}
}                   	

main() {
	setIO();
	
	int tt = 1;
	// cin >> tt;
	while (tt--) {
		solve();
	}
	
	return 0;
}

Compilation message (stderr)

subsequence.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  108 | main() {
      | ^~~~
subsequence.cpp: In function 'void setIO(std::string)':
subsequence.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen((name + ".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subsequence.cpp:20:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |   freopen((name + ".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...