Submission #89663

#TimeUsernameProblemLanguageResultExecution timeMemory
89663antimirageLongest beautiful sequence (IZhO17_subsequence)C++17
0 / 100
24 ms17164 KiB
#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb emplace_back
#define all(s) s.begin(), s.end()

using namespace std;

const int N = 1e5 + 5;

int dp[11][1025][1025], p[N], n, ar[N], k[N], ans, ind[11][1025][1025], last;

vector <int> path;

main()
{
	cin >> n;
	if (n == 2){
		puts("1");
		puts("1");
		return 0;
	}
	for (int i = 1; i <= n; i++)
		scanf("%d", &ar[i]);
		
	for (int i = 1; i <= n; i++)
		scanf("%d", &k[i]);
		
	for (int i = 1; i <= n; i++)
	{
		int pref = (ar[i] >> 10), suf = ar[i] % 1024, res = 0;
		
		for (int j = 0; j < 1024; j++)
		{
			if ( __builtin_popcount( j & suf ) > k[i] ) continue;
			if ( dp[ k[i] - __builtin_popcount( j & suf ) ][pref][j] > res )
			{
				res = dp[ k[i] - __builtin_popcount( j & suf ) ][pref][j];
				p[i] = ind[ k[i] - __builtin_popcount( j & suf ) ][pref][j];
			}
		}
		res++;
		
		if (res > ans){
			ans = res;
			last = i;
		}
		
		for (int j = 0; j < 1024; j++){
			if (res > dp[__builtin_popcount(pref & j) ][j][suf])
			{
				dp[__builtin_popcount(pref & j) ][j][suf] = res;
				ind[__builtin_popcount(pref & j) ][j][suf] = i;
			}
		}
	}
	cout << ans << endl;
	
	while (ans){
		path.push_back( last );
		last = p[last];
		ans--;
	}
	reverse(all(path));
	
	for (auto it : path)
		printf("%d ", it);
}
/**
4
1 2 3 4
10 0 1 0

2
8 9
20 0

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

Compilation message (stderr)

subsequence.cpp:17:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
subsequence.cpp: In function 'int main()':
subsequence.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &ar[i]);
   ~~~~~^~~~~~~~~~~~~~
subsequence.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &k[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...