Submission #515497

#TimeUsernameProblemLanguageResultExecution timeMemory
515497MazaalaiLongest beautiful sequence (IZhO17_subsequence)C++17
23 / 100
96 ms1880 KiB
#include <bits/stdc++.h>
#define pb push_back
#define LINE "--------------------\n"
#define ALL(x) x.begin(),x.end()
using namespace std;
const int N = 1e5+5;
const int M = (1<<20)+5;
int n;
int nums[N], bits[N], par[N], dpPos[N], dpVal[M], pos[M];

int bitCnt(int a) {
	return bitset <20>(a).count();
}
signed main() {
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	int ans = 1;
	for (int i = 1; i <= n; i++) cin >> nums[i];
	for (int i = 1; i <= n; i++) cin >> bits[i];
	if (n <= 5000) {
		for (int i = 1; i <= n; i++) {
			for (int j = i+1; j <= n; j++) {
				if (bits[j] == bitCnt(nums[i]&nums[j]) && dpPos[j] < dpPos[i]+1) {
					dpPos[j] = dpPos[i]+1;
					par[j] = i;
				} 
			}
			if (dpPos[ans] < dpPos[i]) ans = i;
		}
		vector <int> tmp;
		while(ans != 0) {
			tmp.pb(ans);
			ans = par[ans];
		}
		cout << tmp.size() << '\n';
		reverse(ALL(tmp));
		for (auto el : tmp) cout << el << ' '; cout << '\n';
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		int curPar = 0, x = bitCnt(nums[i]);
		int res = 0;
		for (int j = 1; j < (1<<8); j++) {
			if (bitCnt(nums[i]&j) == bits[i] && dpVal[j] > dpVal[res]) res = j;
		}
		dpPos[i] = dpVal[res]+1;
		par[i] = pos[res];
		dpVal[nums[i]] = dpPos[i];
		pos[nums[i]] = i;
		if (dpPos[i] > dpPos[ans]) ans = i;
	}
	
	vector <int> tmp;
	while(ans != 0) {
		tmp.pb(ans);
		ans = par[ans];
	}
	cout << tmp.size() << "\n";
	while(!tmp.empty()) {
		cout << tmp.back() << ' ';
		tmp.pop_back();
	}
	cout << "\n";
}



























Compilation message (stderr)

subsequence.cpp: In function 'int main()':
subsequence.cpp:41:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   41 |   for (auto el : tmp) cout << el << ' '; cout << '\n';
      |   ^~~
subsequence.cpp:41:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   41 |   for (auto el : tmp) cout << el << ' '; cout << '\n';
      |                                          ^~~~
subsequence.cpp:45:7: warning: unused variable 'curPar' [-Wunused-variable]
   45 |   int curPar = 0, x = bitCnt(nums[i]);
      |       ^~~~~~
subsequence.cpp:45:19: warning: unused variable 'x' [-Wunused-variable]
   45 |   int curPar = 0, x = bitCnt(nums[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...