text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
long long int M = 1e9 + 7;
long long int M2 = 998244353;
long long int M3 = 1e8;
long long int MAX = 5001;
string s = "Everything correct till here!";
void show(vector<long long int> v) {
for (long long int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << "\n";
}
void showrev(vector<long long int> v) {
for (long long int i = 0; i < v.size(); i++)
cout << v[v.size() - i - 1] << " ";
cout << "\n";
}
void showarr(long long int arr[], long long int n) {
for (long long int i = 0; i < n; i++) cout << arr[i] << " ";
cout << "\n";
}
void showpv(vector<pair<long long int, string> > v) {
for (long long int i = 0; i < v.size(); i++)
cout << v[i].first << " " << v[i].second << "\n";
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int findlcm(long long int arr[], long long int n) {
long long int ans = arr[0];
for (int i = 1; i < n; i++) ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
return ans;
}
void solve() {
long long int n;
cin >> n;
long long int a[n + 1];
for (long long int i = 1; i <= n; i++) cin >> a[i];
long long int dp[n + 1];
for (long long int i = 0; i < n + 1; i++) dp[i] = 1;
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j * j <= i; j++) {
if (i % j != 0) continue;
if (a[i] > a[j]) dp[i] = max(dp[i], dp[j] + 1);
if (a[i] > a[i / j]) dp[i] = max(dp[i], dp[i / j] + 1);
}
}
cout << *max_element(dp + 1, dp + n + 1) << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int T = 1, Q = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
unsigned long long t;
cin >> t;
for (unsigned long long i = 0; i < t; i++) {
unsigned long long a[100005], n;
cin >> n;
for (unsigned long long i = 0; i < n; i++) cin >> a[i + 1];
unsigned long long dp[100005];
for (unsigned long long i = 0; i <= n; i++) dp[i] = 1;
for (unsigned long long i = 2; i <= n; i++) {
set<unsigned long long> s;
s.insert(1);
for (unsigned long long j = 2; j * j <= i; j++) {
if (i % j == 0) {
s.insert(j);
s.insert(i / j);
}
}
for (auto k = s.begin(); k != s.end(); k++) {
if (a[i] > a[*k]) dp[i] = max(dp[i], dp[*k] + 1);
}
}
unsigned long long mxm = *max_element(dp, dp + n + 1);
cout << mxm << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int dp[100005];
long long PowerMod(long long a, long long b, long long c) {
int ans = 1;
a = a % c;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % c;
b = b / 2;
a = (a * a) % c;
}
return ans;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i] = 1;
}
int ans = 0;
long long p = PowerMod(20, 20, 3);
for (int i = 1; i <= n; i++) {
for (int j = i + i; j <= n; j += i) {
if (a[i] < a[j]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
ans = max(dp[i], ans);
}
printf("%d\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e6 + 5;
long long int n;
long long int a[N];
void solve() {
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
}
long long int dp[n + 1];
long long int res = 0;
memset(dp, 0, sizeof(dp));
for (long long int i = 1; i <= n; i++) {
long long int j;
for (j = i * 2; j <= n; j += i) {
if (a[i] < a[j]) {
dp[j] = max(dp[j], 1 + dp[i]);
}
}
res = max(res, dp[i]);
}
cout << res + 1 << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int ara[100003];
int arab[100003];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t, n;
cin >> t;
while (t--) {
cin >> n;
int maxi = 1;
for (int i = 1; i <= n; i++) {
cin >> ara[i];
arab[i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 2; (i * j) <= n; j++) {
if (ara[i * j] > ara[i]) arab[i * j] = max(arab[i * j], arab[i] + 1);
}
maxi = max(arab[i], maxi);
}
cout << maxi << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 * 9;
const long long N = 1e6 + 23;
const long long M = 3000 + 23;
const long long mod1 = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long base = 131;
long long n, m, k, ans, cnt, x, y, mid, sx = -1, sy = -1, mx, mi = INF, d, aa;
long long a[N], b[N], dp[N];
bool vis[N];
vector<long long> ve, ve1;
string s, ss;
void init(long long pos) {
for (long long i = 1; i * i <= pos; i++) {
if (pos % i == 0) {
if (pos / i == i) {
if (a[i] < a[pos]) dp[pos] = max(dp[pos], dp[i] + 1);
} else {
if (a[i] < a[pos]) dp[pos] = max(dp[pos], dp[i] + 1);
if (a[pos / i] < a[pos]) dp[pos] = max(dp[pos], dp[pos / i] + 1);
}
}
ans = max(dp[pos], ans);
}
}
map<long long, long long> mp;
void solve() {
long long pos = 0, s1 = 0, s2 = 0, z, f1 = 0;
mx = 0;
ans = 0;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
dp[i] = 1;
}
for (long long i = 2; i <= n; i++) {
init(i);
}
cout << max(ans, 1ll) << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
long long T;
for (cin >> T; T--;) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int static fast = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
vector<long long> arr(n, 0);
vector<long long> dp(n + 1, 1);
for (auto& val : arr) cin >> val;
long long ans = 1;
for (long long i = 1; i <= n / 2; i++) {
for (long long j = i * 2; j <= n; j += i) {
if (arr[j - 1] > arr[i - 1]) {
dp[j] = max(dp[j], dp[i] + 1);
ans = max(ans, dp[j]);
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long const mod = 1000000000 + 7;
long long sgn(long long a) {
if (a > 0)
return 1;
else
return -1;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v(n + 1);
vector<long long> f(n + 1, 1);
for (long long i = 1; i <= n; ++i) cin >> v[i];
for (long long i = 1; i <= n; i++) {
for (long long j = i * 2; j <= n; j += i) {
if (v[i] < v[j]) f[j] = max(f[j], f[i] + 1);
}
}
long long ans = 0;
for (long long i = 1; i <= n; ++i) ans = max(ans, f[i]);
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long arr[n + 1];
arr[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int num[n + 1];
memset(num, 0, sizeof(num));
num[1] = 1;
for (int i = 2; i <= n; i++) {
if (arr[i] > arr[1]) {
num[i] = 2;
} else {
num[i] = 1;
}
}
for (int i = 2; i <= n / 2; i++) {
for (int j = 2 * i; j <= n; j += i) {
if (arr[j] > arr[i]) {
num[j] = max(num[j], num[i] + 1);
}
}
}
int mx = 0;
for (int i = 1; i <= n; i++) {
if (num[i] > mx) {
mx = num[i];
}
}
cout << mx << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n + 1);
for (long long i = 1; i <= n; i++) cin >> a[i];
vector<long long> len(n + 1, 1);
long long ans = INT_MIN;
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= sqrt(i); j++) {
if (i % j == 0) {
if (a[i] > a[j]) len[i] = max(len[i], len[j] + 1);
if (a[i] > a[i / j]) len[i] = max(len[i], len[i / j] + 1);
}
}
ans = max(ans, len[i]);
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n + 1), dp(n + 1, 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) {
int mx = 0;
for (int j = 2; i * j <= n; j++) {
if (a[i] < a[i * j]) mx = max(mx, dp[i * j]);
}
dp[i] += mx;
}
cout << *max_element(dp.begin(), dp.end()) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<int, vector<int> > divi;
void sieve() {
for (int j = 1; j <= 100000; j++) {
for (int i = 1; i <= sqrt(j); i++) {
if (j % i == 0) {
if (j / i == i) {
divi[j].push_back(i);
} else {
divi[j].push_back(i);
divi[j].push_back(j / i);
}
}
}
}
}
long long int lis(long long int arr[], int n) {
long long int lis[n];
lis[0] = 1;
for (int i = 2; i <= n; i++) {
lis[i - 1] = 1;
for (int k = 0; k < divi[i].size(); k++) {
int j = divi[i][k];
if (arr[i - 1] > arr[j - 1] && lis[i - 1] < lis[j - 1] + 1 && i % j == 0)
lis[i - 1] = lis[j - 1] + 1;
}
}
return *max_element(lis, lis + n);
}
int main() {
sieve();
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
long long int ans = lis(arr, n);
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int z;
long long int dp[100005];
long long int dfs(long long int arr[], long long int i) {
if (i > z) return 0;
if (dp[i] != -1) return dp[i];
long long int ans = 0;
for (long long int j = i + i; j <= z; j += i) {
if (arr[i] < arr[j]) ans = max(ans, dfs(arr, j) + 1);
}
dp[i] = ans;
return ans;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
z = n;
long long int arr[n + 1];
for (long long int j = 1; j <= n; j++) {
cin >> arr[j];
dp[j] = -1;
}
long long int best = 0;
for (long long int j = 1; j <= n; j++) best = max(best, dfs(arr, j));
cout << best + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
long long t = 1, n, m, i, j, k, u, to;
cin >> t;
long long tc = t;
while (t--) {
cin >> n;
vector<long long> a(n + 1), dp(n + 1, 1);
for (i = 1; i <= n; i++) {
cin >> a[i];
}
long long ans = 1;
for (i = n; i > 0; i--) {
for (k = i; k <= n; k += i) {
if (a[i] < a[k]) {
dp[i] = max(dp[i], dp[k] + 1);
}
ans = max(ans, dp[i]);
}
}
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t_;
cin >> t_;
while (t_--) {
long long n;
cin >> n;
vector<long long> s(n);
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
vector<long long> x(n, 0);
x[0] = 1;
for (long long i = 2; i <= n; i++) {
long long best = 0;
for (long long j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (x[j - 1] > best && s[j - 1] < s[i - 1]) {
best = x[j - 1];
}
if (x[(i / j) - 1] > best && s[(i / j) - 1] < s[i - 1]) {
best = x[(i / j) - 1];
}
}
}
x[i - 1] = best + 1;
}
long long best = 0;
for (long long i = 0; i < n; i++) {
best = max(best, x[i]);
}
cout << best << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 2e3 + 10;
void sol() {
long long n;
cin >> n;
vector<long long> a(n + 1);
for (long long i = 1; i < n + 1; i++) cin >> a[i];
vector<long long> ans(n + 1, 1);
for (int i = n / 2; i >= 1; i--) {
int j = 2;
while (i * j <= n) {
if (ans[j * i] + 1 > ans[i] && a[i] < a[j * i]) ans[i] = ans[i * j] + 1;
j++;
}
}
long long mx = 0;
for (int i = 1; i <= n; i++) {
mx = max(mx, ans[i]);
}
cout << mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
sol();
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int s[100005];
int dp[100005];
int n;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int r = 0; r <= n; r++) dp[r] = 0;
int tt;
for (int j = 1; j <= n; j++) {
cin >> tt;
s[j] = tt;
}
int ans = 0;
for (int k = n; k >= 1; k--) {
int mx = 0;
for (int i = k * 2; i <= n; i = i + k) {
if (s[k] < s[i]) mx = max(mx, dp[i]);
}
dp[k] = mx + 1;
ans = max(ans, dp[k]);
}
if (n == 1) {
cout << "1"
<< "\n";
continue;
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[100005];
int dp[100005];
int main() {
int t, n, k, i, j;
scanf("%d", &t);
while (t--) {
int ans = -1;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
dp[i] = 1;
for (j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (a[i] > a[i / j]) {
dp[i] = max(dp[i / j] + 1, dp[i]);
}
if (a[i] > a[j]) {
dp[i] = max(dp[j] + 1, dp[i]);
}
}
}
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n;
vector<long long> v(n + 5), dp(n + 5);
for (long long i = 1; i <= n; i++) {
cin >> v[i];
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
dp[i] = 1;
if (i == 1) {
dp[i] = 1;
} else {
for (long long j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (v[i] > v[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
if (v[i] > v[i / j]) {
dp[i] = max(dp[i], dp[i / j] + 1);
}
}
}
}
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
int main() {
int t, n, i, j, m, s[100007], a[100007];
for (scanf("%d", &t); t--;) {
for (scanf("%d", &n), i = 0; i++ < n;) scanf("%d", a + i), s[i] = 1;
for (i = 1; i < n; i++)
for (j = i; j <= n; j += i)
if (a[j] > a[i]) s[j] = std::max(s[j], s[i] + 1);
for (i = m = 0; i++ < n;) m = std::max(m, s[i]);
printf("%d\n", m);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int const mod = 1e9 + 7;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int MIN(long long int a, long long int b) {
if (a < b) return a;
return b;
}
long long int MAX(long long int a, long long int b) {
if (a > b) return a;
return b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
bool p[1000001];
memset(p, true, sizeof p);
p[0] = p[1] = false;
p[2] = true;
for (int i = 2; i * i <= 1000001; i++) {
if (p[i]) {
for (int j = i * i; j <= 1000001; j += i) {
p[j] = false;
}
}
}
vector<int> pp;
for (int i = 2; i <= 1000001; i++) {
if (p[i]) pp.push_back(i);
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i + 1];
int dp[n + 1], ans = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = 1;
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (a[i] > a[j]) dp[i] = MAX(dp[i], dp[j] + 1);
if (a[i] > a[i / j]) dp[i] = MAX(dp[i], dp[i / j] + 1);
}
}
ans = MAX(dp[i], ans);
}
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> dp(n + 1, 1);
for (int i = n; i > 0; i--) {
int count = 2;
int j = i;
while (j * count <= n) {
if (a[i] < a[j * count]) dp[i] = max(dp[i], 1 + dp[j * count]);
count++;
}
}
int optimal = 1;
for (int i = 1; i <= n; i++) {
if (optimal < dp[i]) {
optimal = dp[i];
}
}
cout << optimal << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int v[100100];
int n;
int memo[100100];
int dp(int ind) {
if (ind == n) return 1;
int pdm = memo[ind];
if (pdm != -1) return pdm;
pdm = 1;
int j = ind;
while (j <= n) {
if (v[j] > v[ind]) pdm = max(pdm, 1 + dp(j));
j += ind;
}
memo[ind] = pdm;
return pdm;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
memo[i] = -1;
}
int mx = -1;
for (int i = 1; i <= n; i++) {
mx = max(mx, dp(i));
}
cout << mx << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int A[100005];
set<int> st[100005];
void init() {
for (int i = 2; i <= 100000; i++) {
for (int j = 2; j * j <= i; j++) {
if ((i % j) == 0) {
st[i].insert(j);
st[i].insert(i / j);
}
}
st[i].insert(1);
}
}
int d[100005];
signed main() {
int t, x, k;
init();
scanf("%d", &t);
int n;
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
d[i] = 1;
}
int ans = 1;
d[1] = 1;
for (int i = 2; i <= n; i++) {
for (auto x : st[i]) {
if (A[x] < A[i]) {
d[i] = max(d[x] + 1, d[i]);
}
}
ans = max(ans, d[i]);
}
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
ofstream g("date.out");
int t;
vector<int> diviz[100005];
int main() {
cin >> t;
for (int i = 1; i <= 100000; i++) {
for (int j = 2 * i; j <= 100000; j += i) {
diviz[j].push_back(i);
}
}
while (t--) {
int n, sol = 0;
cin >> n;
vector<int> v(n + 5, 0);
vector<int> dp(n + 5, 0);
for (int i = 1; i <= n; i++) {
cin >> v[i];
dp[i] = 1;
for (int j = 0; j < diviz[i].size(); j++) {
int val = diviz[i][j];
if (v[i] > v[val]) {
dp[i] = max(dp[i], dp[val] + 1);
}
}
sol = max(sol, dp[i]);
}
cout << sol << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MXN = (int)1e5 + 7;
const int MOD = 998244353;
const long long INF = (long long)2e18 + 7;
const int N = (int)1e3 + 7;
const int dx[] = {1, 0, 0, -1};
const int dy[] = {0, 1, -1, 0};
inline void boost() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int t, n, a[MXN], ans[MXN];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int mx = 0;
fill(ans + 1, ans + n + 1, 0);
for (int i = 1; i <= n; i++) {
vector<int> node;
for (int j = 1; j <= sqrt(i); j++) {
if (j * j == i) {
node.push_back(j);
} else if (i % j == 0) {
node.push_back(j);
node.push_back(i / j);
}
}
ans[i] = 1;
for (int j : node) {
if (a[j] < a[i]) {
ans[i] = max(ans[i], ans[j] + 1);
}
}
mx = max(mx, ans[i]);
}
cout << mx << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], f[N], n;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), f[i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = i * 2; j <= n; j += i)
if (a[i] < a[j]) f[j] = max(f[j], f[i] + 1);
ans = max(ans, f[i]);
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2;
int a[N], dp[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
fill(dp, dp + n + 2, 1);
for (int i = 1; i <= (n); i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 2 * i; j <= n; j += i) {
if (a[i] < a[j]) dp[j] = max(dp[j], dp[i] + 1);
}
}
int mas = 1;
for (int i = 1; i <= n; i++) mas = max(mas, dp[i]);
cout << mas << "\n";
}
}
|
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
using namespace std;
long long int ar[100001], dp[100001];
int main() {
long long int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> ar[i], dp[i] = 1;
int ans = 1;
for (long long int i = 2; i <= n; i++) {
int mx = 0;
for (long long int j = 1; j * j <= i; j++)
if (i % j == 0) {
int d1 = j;
int d2 = i / j;
if (ar[d1] < ar[i]) mx = max((long long int)mx, dp[d1]);
if (ar[d2] < ar[i]) mx = max((long long int)mx, dp[d2]);
}
dp[i] += mx;
ans = max((long long int)ans, dp[i]);
}
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, q;
scanf("%d", &t);
for (q = 1; q <= t; q++) {
long long int n, i;
cin >> n;
long long int ar[n + 5];
long long int vis[n + 2];
for (i = 1; i <= n; i++) {
cin >> ar[i];
vis[i] = 1;
}
long long int ans = 1, c, k;
long long int tem, tem2;
for (int i = 1; i <= n; i++) {
k = i;
for (int j = i * 2; j <= n; j = j + i) {
if (ar[i] < ar[j]) {
vis[j] = max(vis[j], vis[i] + 1);
}
}
}
for (int i = 1; i <= n; i++) {
ans = max(ans, vis[i]);
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
cin >> n;
vector<long long> nums(n);
for (auto &v : nums) cin >> v;
vector<long long> dp(n + 5, 1);
long long res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j += (i + 1)) {
if (nums[j] > nums[i]) dp[j] = max(dp[j], dp[i] + 1);
}
}
for (auto &v : dp) res = max(v, res);
cout << res << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> divisors[100002];
void solve() {
int n;
cin >> n;
vector<int> v(n + 2);
for (int i = 0; i < n; i++) {
cin >> v[i + 1];
}
v[0] = 1e9 + 3;
int ans = 1;
vector<int> dp(n + 2, 0);
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = 1;
for (int d : divisors[i]) {
if (v[i] > v[d]) {
dp[i] = max(dp[i], dp[d] + 1);
}
}
ans = max(dp[i], ans);
}
cout << ans << endl;
}
void sieve(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = i * 2; j <= n; j += i) {
divisors[j].push_back(i);
}
}
}
int main() {
int t = 1;
sieve(1e5 + 1);
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, i;
cin >> n;
long long int a[n], c1 = 0, c2 = 1, c = 2, dp[n];
for (long long int i = 1; i <= n; i++) cin >> a[i];
for (long long int i = 1; i <= n; i++) {
dp[i] = 1;
}
for (long long int i = 1; i <= (n / 2) + 1; i++) {
for (int j = i * 2; j <= n; j += i) {
if (a[i] < a[j]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
long long int ans = 0;
for (long long int i = 1; i <= n; i++) ans = max(ans, dp[i]);
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using tint = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
const tint MOD = 1e9 + 7;
const int MX = 1e5 + 5;
const int INF = 1e9;
const long double PI = acos((-1));
void NACHO(string name = "factory") {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
int main() {
NACHO();
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
vi a(n + 1);
for (int i = 0; i < int(n); i++) cin >> a[i + 1];
vi dp(n + 1, 1);
for (int i = 1; i < int(n + 1); i++) {
for (int j = 1; j * j <= i; ++j) {
if (i % j == 0) {
if (a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1);
if (a[i / j] < a[i]) dp[i] = max(dp[i], dp[i / j] + 1);
}
}
}
int maxi = 0;
for (int i = 0; i < int(n); i++) maxi = max(maxi, dp[i + 1]);
cout << maxi << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
long long int n;
long long int a[N], dp[N];
long long int rec(long long int ind) {
if (ind > n) return 0;
if (dp[ind] != -1) return dp[ind];
long long int c = 0;
for (long long int i = ind; i <= n; i += ind) {
if (a[i] > a[ind]) {
c = max(c, 1 + rec(i));
}
}
dp[ind] = c;
return c;
}
void solve() {
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
dp[i] = -1;
}
long long int mx = 1;
for (long long int i = 1; i <= n; i++) {
mx = max(mx, 1 + rec(i));
}
cout << mx << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t-- > 0) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 9;
long long power(long long x, long long y) {
if (y == 0) return 1;
long long temp = power(x, y / 2);
if (y % 2 == 1)
return temp * temp * x;
else
return temp * temp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 10), dp(n + 10);
for (int i = 1; i <= n; i++) {
dp[i] = 1;
cin >> a[i];
}
int maxi = 1;
for (int i = 1; i <= n; i++) {
for (int j = i + i; j <= n; j += i)
if (a[j] > a[i]) dp[j] = max(dp[j], dp[i] + 1);
maxi = max(maxi, dp[i]);
}
cout << maxi << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[3005];
long long loga(long long a, long long b) {
long long c = 0;
while (b) {
c++;
b /= a;
}
return c;
}
long long powe(long long a, long long b) {
long long n = 1;
while (b) {
if (b & 1) n *= a;
a *= a;
b /= 2;
}
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, k, i, j, ii, jj;
long long n, x, y, c, d, m, lo, hi, mid, a, b, p, z;
string s;
t = 1;
cin >> t;
long long T = 0;
while (t--) {
cin >> n;
long long a[n + 1], cur;
for (i = 1; i <= n; i++) cin >> a[i];
long long an[n + 1];
for (i = 1; i <= n; i++) an[i] = 1;
for (i = n; i > 0; i--) {
cur = 0;
for (j = 2 * i; j <= n; j += i) {
if (a[j] > a[i]) {
cur = max(cur, an[j]);
}
}
an[i] += cur;
}
long long ans = 1;
for (i = 1; i <= n; i++) ans = max(ans, an[i]);
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int arr[1000005], n, dp[1000005];
int rec(int x) {
int i, j, k;
if (dp[x] != -1) return dp[x];
for (i = 2;; i++) {
if (x * i > n) break;
if (arr[x * i] > arr[x]) dp[x] = max(dp[x], rec(x * i) + 1);
}
if (dp[x] < 0) dp[x] = 1;
return dp[x];
}
int main() {
int t, z;
cin >> t;
while (t--) {
memset(dp, -1, sizeof dp);
cin >> n;
int i, j, k, l, MX = 0;
for (i = 1; i <= n; i++) cin >> arr[i];
for (i = 1; i <= n; i++) {
if (dp[i] != -1)
MX = max(MX, dp[i]);
else
MX = max(MX, rec(i));
}
cout << MX << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int cases = 1;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n + 1, 0);
for (int i = 1; i <= n; i++) cin >> v[i];
int maxi = 0;
vector<int> ase(n + 1, 0);
for (int i = n; i >= 1; i--) {
ase[i]++;
int mx = 0;
for (int j = i + i; j <= n; j += i) {
if (v[j] > v[i]) mx = max(ase[j], mx);
}
ase[i] += mx;
maxi = max(ase[i], maxi);
}
cout << maxi << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
long long n;
vector<long long> v(1000002);
long long dp[1000005];
long long check(long long i) {
long long cnt = 0;
if (i > n) return 0;
if (dp[i] != -1) return dp[i];
for (long long j = 2 * i; j <= n; j += i) {
if (v[j] > v[i]) cnt = max(cnt, 1 + check(j));
}
return dp[i] = cnt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long tc = 1, i, j, k;
cin >> tc;
while (tc--) {
cin >> n;
for (long long i = 1; i <= n; i++) cin >> v[i];
long long ans = 1;
for (long long i = 1; i <= 1000004; i++) dp[i] = -1;
for (long long i = 1; i <= n; i++) {
ans = max(ans, 1 + check(i));
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n + 1];
for (long long i = 1; i <= n; i++) cin >> a[i];
long long ans[n + 9];
memset(ans, 0, sizeof(ans));
for (long long i = 1; i <= n; i++) {
ans[i] = max(ans[i], 1LL);
for (long long j = 2 * i; j <= n; j += i) {
if (a[j] > a[i]) {
ans[j] = max(ans[j], ans[i] + 1);
}
}
}
long long mx = 0;
for (int i = 1; i <= n; i++) mx = max(mx, ans[i]);
cout << mx << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int base = 100003;
const long long MM = 1ll * 1000000007 * 1000000007;
const int maxc = 2e9;
int n, a[100001], f[100001];
void Solve() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) f[i] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= sqrt(i); j++)
if (i % j == 0) {
int k = i / j;
if (a[k] < a[i]) f[i] = max(f[i], f[k] + 1);
if (a[j] < a[i]) f[i] = max(f[i], f[j] + 1);
}
}
cout << *max_element(f + 1, f + n + 1) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int test = 1;
cin >> test;
while (test--) Solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> dp(n + 1, 1);
for (int i = 1; i <= n; i++) {
for (int j = i + i; j <= n; j += i) {
if (a[i] < a[j]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int dp[100005];
int s[100005];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
dp[i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int k = 2; k * i <= n; k++) {
int j = k * i;
if (s[j] > s[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
int ans = -1;
for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
const double PI = acos(-1.0);
int a[mx], dp[mx];
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int ans = 1;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int j = 1; j <= sqrt(i); j++) {
if (i % j == 0) {
int x = j, y = i / j;
if (a[i] > a[x] && i != x) dp[i] = max(dp[i], dp[x] + 1);
if (a[i] > a[y] && i != y) dp[i] = max(dp[i], dp[y] + 1);
ans = max(ans, dp[i]);
}
}
}
for (int i = 1; i <= n; i++) dp[i] = 0;
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e+5 + 8;
int n;
vector<long long> v;
int dp[N / 2];
int solve(int i) {
if (dp[i] != -1) return dp[i];
int val = 1;
for (int j = 2 * i; j <= n; j += i) {
if (v[j] > v[i]) val = max(val, 1 + solve(j));
}
return dp[i] = val;
}
void MAIN(long long tc) {
cin >> n;
v.clear();
v.resize(n + 1);
for (int i = 1; i <= n; i++) dp[i] = -1;
for (int i = 1; i <= n; i++) cin >> v[i];
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, solve(i));
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed;
cout << setprecision(10);
int test__cases = 1;
cin >> test__cases;
for (int i = 1; i <= test__cases; i++) {
MAIN(i);
}
}
|
#include <bits/stdc++.h>
using namespace std;
void yes() { cout << "YES" << endl; }
void no() { cout << "NO" << endl; }
const int N = 5 * 1e5 + 11;
int n, k, a[N], d[N], cnt[N];
long long ans;
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cnt[i] = 0;
d[i] = 0;
}
int mx = 0;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= sqrt(i) + 1; j++) {
if (i % j == 0) {
if (a[i] > a[j]) {
cnt[j] = max(cnt[j], cnt[i] + 1);
}
if (j == 1) continue;
int df = i / j;
if (a[i] > a[df]) {
cnt[df] = max(cnt[df], cnt[i] + 1);
}
}
}
mx = max(mx, cnt[i]);
}
cout << mx + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(NULL);
;
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first)
return a.first < b.first;
else
return a.second > b.second;
}
int main() {
int t;
int n;
cin >> t;
while (t--) {
cin >> n;
pair<int, int> ar[n];
int bl[n + 1];
for (int i = 0; i < n; i++) {
cin >> ar[i].first;
ar[i].second = i + 1;
bl[i + 1] = 0;
}
sort(ar, ar + n, cmp);
int xx, x;
int ans = 0;
for (int i = 0; i < n; i++) {
x = ar[i].second;
bl[x] = 1;
xx = sqrt(x);
if (x != 1) {
for (int j = 1; j <= xx; j++) {
if (x % j == 0) {
bl[x] = max(bl[x], bl[j] + 1);
if (j != 1) bl[x] = max(bl[x], bl[x / j] + 1);
}
}
}
if (ans < bl[x]) ans = bl[x];
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long _, n, res, a[200005];
void dfs(long long now, long long cnt) {
for (long long i = 2; i <= n; i++) {
if (now * i > n) {
res = max(cnt, res);
break;
}
if (a[now * i] <= a[now]) continue;
dfs(now * i, cnt + 1);
}
}
int main() {
for (scanf("%lld", &_); _; _--) {
scanf("%lld", &n);
res = 1;
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 1; i <= n; i++) dfs(i, 1);
printf("%lld\n", res);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
int s[n + 1];
for (int i = 1; i < n + 1; i++) {
cin >> s[i];
}
int f[n + 1];
for (int i = 0; i < n + 1; i++) {
f[i] = 1;
}
for (int i = 1; i < n + 1; i++) {
for (int j = 2 * i; j <= n; j += i) {
if (s[j] > s[i]) {
f[j] = max(f[j], f[i] + 1);
}
}
}
int mx = 0;
for (int i = 1; i < n + 1; i++) {
mx = max(mx, f[i]);
}
cout << mx << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long lcm(long long a, long long b);
long long gcd(long long a, long long b);
long long smallestDivisor(long long n) {
if (n % 2 == 0) return 2;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tc = 1;
cin >> tc;
while (tc--) {
long long n, i, j, k;
cin >> n;
long long a[n];
for (i = 1; i <= n; i++) {
cin >> a[i];
}
long long dp[n + 1];
for (i = 0; i <= n; i++) {
dp[i] = 1;
}
for (i = 1; i <= n; i++) {
long long mx = 0;
long long x = sqrt(i);
for (j = 1; j <= x; j++) {
if (i % j == 0) {
if (a[i] > a[j]) {
mx = max(mx, dp[j]);
}
if (a[i] > a[i / j]) {
mx = max(mx, dp[i / j]);
}
}
}
dp[i] += mx;
}
long long ans = 0;
for (i = 0; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
|
#include <bits/stdc++.h>
using namespace std;
int t, n, a[100005], dp[100005];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
dp[i] = 1;
}
int maxn = 1;
for (int i = n / 2; i >= 1; i--) {
for (int j = i; j <= n; j += i) {
if (a[j] > a[i]) dp[i] = max(dp[i], dp[j] + 1);
}
maxn = max(dp[i], maxn);
}
printf("%d\n", maxn);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using vb = vector<bool>;
const ll mod = 1e9 + 7;
template <typename T>
void take_input(vector<T> &a, int size) {
T tmp;
for (int i = 0; i < size; i++) {
cin >> tmp;
a.push_back(tmp);
}
}
template <typename T>
void print_vector(vector<T> v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
void add_edge(vector<vi> &a, int u, int v) { a[u].push_back(v); }
void print_graph(vector<vector<int> > &a) {
for (int i = 0; i < 20; i++) {
cout << i << " : ";
for (int x : a[i]) {
cout << x << " ";
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<vector<int> > factors;
factors.resize(1e5 + 1);
for (int i = 1; i <= 1e5; i++) {
for (int j = 1; j <= sqrt(i); j++) {
if (j == 1)
factors[i].push_back(j);
else if (i % j == 0) {
factors[i].push_back(j);
factors[i].push_back(i / j);
}
}
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vi a;
a.push_back(0);
take_input<int>(a, n);
vi dp;
for (int i = 0; i < n + 1; i++) dp.push_back(1);
for (int i = 1; i <= n; i++) {
for (int j : factors[i]) {
if (a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1);
}
}
int ans = 1;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long n;
cin >> n;
long long s[n + 1];
for (long long i = 1; i <= n; ++i) {
cin >> s[i];
}
vector<long long> ans(n + 1, 1);
for (long long i = 1; i <= n; ++i) {
for (long long j = i * 2; j <= n; j += i) {
if (s[j] > s[i]) ans[j] = max(ans[j], ans[i] + 1);
}
}
long long a = 0;
for (long long i = 1; i <= n; ++i) {
a = max(a, ans[i]);
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::vector<int> v(n), b(n, 1);
for (int& x : v) std::cin >> x;
for (int i = 0; i < n; ++i) {
for (int j = (i + 1) * 2 - 1; j < n; j += i + 1)
if (v[j] > v[i]) b[j] = std::max(b[j], b[i] + 1);
}
std::cout << *std::max_element(b.begin(), b.end()) << '\n';
}
}
|
#include <bits/stdc++.h>
const int MAXN = 1e5 + 1;
int t, n;
int arr[MAXN];
int brr[MAXN];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
arr[i + 1] = a;
}
for (int i = 0; i <= n; i++) brr[i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; (j * j) <= i; j++) {
if (i % j == 0) {
if (arr[j] < arr[i]) {
brr[i] = std::max(brr[i], brr[j] + 1);
}
if (arr[i / j] < arr[i]) {
brr[i] = std::max(brr[i], brr[i / j] + 1);
}
}
}
}
printf("%d\n", *std::max_element(brr, brr + n + 1));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int tt, n, ans;
cin >> tt;
while (tt--) {
cin >> n;
vector<int> v(n + 1), cnt(n + 1, 1);
for (int i = 1; i <= n; i++) cin >> v[i];
ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + i; j <= n; j += i) {
if (v[i] < v[j]) cnt[j] = max(cnt[j], cnt[i] + 1);
}
ans = max(ans, cnt[i]);
}
cout << ans << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int binary_expo(long long int base, long long int pw) {
long long int res = 1;
while (pw) {
if (pw % 2) {
res *= base;
res %= 1000000007;
pw -= 1;
} else {
base *= base;
base %= 1000000007;
pw /= 2;
}
}
return res;
}
long long int mod_inv(long long int k) {
return binary_expo(k, 1000000007 - 2);
}
int t, n;
vector<long long int> s(100005, 0);
long long int dp[100005];
int main() {
cin >> t;
while (t--) {
cin >> n;
s.clear();
for (int i = 1; i <= n; i++) dp[i] = 1;
for (int i = 1; i <= n; i++) cin >> s[i];
long long int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 2 * i; j <= n; j += i) {
if (s[j] > s[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ar[n + 1];
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
int dp[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] = 0;
}
for (int i = n / 2; i >= 1; i--) {
for (int j = 2 * i; j <= n; j += i) {
if (ar[j] > ar[i]) {
dp[i] = max(dp[j] + 1, dp[i]);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, t, a[200200], c[200200], z = 1;
vector<long long> v;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
c[i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = i + i; j <= n; j += i) {
if (a[j] > a[i]) {
c[j] = max(c[j], c[i] + 1);
}
}
}
for (int i = 1; i <= n; i++) {
z = max(z, c[i]);
}
cout << z << endl;
z = 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int t;
int n, ans[N], mx, a[N];
void solve() {
mx = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans[i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (a[i] > a[j]) ans[i] = max(ans[i], ans[j]);
if (a[i] > a[i / j]) ans[i] = max(ans[i], ans[i / j]);
}
}
ans[i]++;
mx = max(mx, ans[i]);
}
cout << mx << endl;
}
int main() {
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n + 1];
for (long long i = 1; i <= n; i++) cin >> a[i];
long long dp[n + 1];
for (long long i = 0; i < n + 1; i++) dp[i] = 1;
for (long long i = 1; i <= n / 2; i++) {
for (long long j = 2 * i; j <= n; j += i) {
if (a[j] > a[i]) dp[j] = max(dp[j], dp[i] + 1);
}
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> divs[100001];
void init() {
for (int num = 2; num <= 100000; num++) {
int limit = sqrt(num);
divs[num].push_back(1);
for (int div = 2; div <= limit; div++) {
if (num % div == 0) {
divs[num].push_back(div);
divs[num].push_back(num / div);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
init();
while (t--) {
int n;
cin >> n;
long long arr[n + 1];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int cum[n + 1];
int maxim = 0;
for (int i = 1; i <= n; i++) {
cum[i] = 1;
int mx = 0;
for (auto div : divs[i]) {
if (arr[i] > arr[div]) {
mx = max(mx, cum[div]);
}
}
cum[i] += mx;
maxim = max(maxim, cum[i]);
}
cout << maxim << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> factor[100001];
void sieve() {
for (int i = 1; i <= 100000; i++) {
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
factor[i].push_back(j);
factor[i].push_back(i / j);
}
}
}
}
int main() {
sieve();
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int a[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int dp[n + 1];
fill(dp, dp + n + 1, 1);
dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < factor[i].size(); j++) {
int s = factor[i][j];
if (a[i] > a[s] && i > s) dp[i] = max(dp[i], dp[s] + 1);
}
}
printf("%d\n", *max_element(dp + 1, dp + n + 1));
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100002];
int mx, ar[100002], dp[100002];
int main() {
for (int i = 1; i <= 100000; i++) {
for (int j = i; j <= 100000; j += i) v[j].push_back(i);
}
int t;
cin >> t;
while (t--) {
int n, ans = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
dp[i] = 1;
}
for (int i = 2; i <= n; i++) {
int mx = 0;
for (int j = 0; j < v[i].size() - 1; j++) {
int id = v[i][j];
if (id >= i) continue;
if (ar[i] > ar[id]) mx = max(mx, dp[id]);
}
dp[i] += mx;
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[100001];
long long dp[100001];
long long f(long long x) {
if (dp[x] != -1) return dp[x];
long long ans = 0;
for (long long i = 1; i * i <= x; i++) {
if (x % i == 0) {
if (a[x / i] < a[x]) ans = max(ans, 1 + f(x / i));
if (a[i] < a[x]) ans = max(ans, 1 + f(i));
}
}
return dp[x] = ans;
}
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
memset(dp, -1, sizeof(dp));
a[0] = 0;
for (long long i = 1; i < n + 1; i++) {
cin >> a[i];
}
long long ans = 0;
for (long long i = 1; i <= n; i++) ans = max(f(i), ans);
cout << 1 + ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int sizes[100010];
vector<long long int> szg[100010];
long long int answers[100010];
int main() {
int t;
long long int n, k, i, j;
cin >> t;
while (t--) {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> sizes[i];
szg[i].clear();
answers[i] = 0;
}
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j += i) {
if (sizes[j] > sizes[i]) {
szg[i].push_back(j);
}
}
}
long long int ans = 0;
for (i = n; i >= 1; i--) {
answers[i] = 1;
long long int mx = 0;
for (long long int y : szg[i]) {
if (answers[y] > mx) mx = answers[y];
}
answers[i] += mx;
if (answers[i] > ans) ans = answers[i];
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, s[100005], ans[100005], ma = 1;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int j = 0; j < 100005; j++) ans[j] = 1;
for (int j = 0; j < n; j++) cin >> s[j];
for (int j = n / 2; j > 0; j--) {
for (int k = j * 2; k <= n; k += j) {
if (s[k - 1] > s[j - 1]) {
ans[j - 1] = max(ans[j - 1], ans[k - 1] + 1);
}
}
ma = max(ma, ans[j - 1]);
}
cout << ma << '\n';
ma = 1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long vis[1000][1000] = {};
long long power(long long x, unsigned long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
long long Remainder(string str, long long R) {
long long len = str.length();
long long Num, Rem = 0;
for (long long i = 0; i < len; i++) {
Num = Rem * 10 + (str[i] - '0');
Rem = Num % R;
}
return Rem;
}
string longDivision(string number, long long divisor) {
string ans;
long long idx = 0;
long long temp = number[idx] - '0';
while (temp < divisor) temp = temp * 10 + (number[++idx] - '0');
while (number.size() > idx) {
ans += (temp / divisor) + '0';
temp = (temp % divisor) * 10 + number[++idx] - '0';
}
if (ans.length() == 0) return "0";
return ans;
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
signed main() {
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long s[n + 1];
long long d[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> s[i];
d[i] = 1;
}
for (long long i = 1; i <= n; i++) {
for (long long j = 2; i * j <= n; j++) {
if (s[i * j] > s[i]) d[i * j] = max(d[i * j], d[i] + 1);
}
}
long long z = 1;
for (long long i = 1; i <= n; i++) z = max(z, d[i]);
cout << z << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, a[100005];
vector<int> v[100005];
int val[100005];
int k[100005];
int main() {
for (int i = 2; i <= 100000; i++) {
v[i].push_back(1);
for (int j = i + i; j <= 100000; j += i) {
v[j].push_back(i);
}
}
scanf("%d", &t);
while (t--) {
memset(k, 0, sizeof(k));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = n; i >= 1; i--) {
if (k[i] == 0) k[i] = 1;
int p = v[i].size();
for (int j = 0; j < p; j++) {
if (a[i] > a[v[i][j]]) k[v[i][j]] = max(k[v[i][j]], k[i] + 1);
}
}
int mx = 0;
for (int i = 1; i <= n; i++) {
mx = max(mx, k[i]);
}
printf("%d\n", mx);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e9 + 7, M = 2e5 + 1;
void solve() {
int n, k;
cin >> n;
int j, m = 1;
vector<long long> a(n + 1, 0);
for (int i = (0); i < (n); i++) cin >> a[i];
vector<int> dp(n + 1, 1);
for (int i = (1); i < (n); i++) {
for (int j = (2 * i); j < (n + 1); j += (i)) {
if (a[j - 1] > a[i - 1]) dp[j] = max(dp[j], dp[i] + 1);
}
}
for (auto i : dp) {
m = max(m, i);
}
cout << m << endl;
}
int main() {
int t = 1, h = 0;
cin >> t;
while (t--) {
h++;
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void TESTCASE() {
long long int n;
cin >> n;
vector<long long int> s(n + 1);
for (long long int i = 1; i <= n; i++) cin >> s[i];
vector<long long int> dp(n + 1, 1);
for (long long int i = 1; i <= n; i++) {
for (long long int j = 2 * i; j <= n; j += i) {
if (s[j] > s[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
long long int res = *max_element(dp.begin() + 1, dp.end());
cout << res << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long int TEST;
cin >> TEST;
while (TEST--) {
TESTCASE();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T, n, s[100010], dist[100010], ans;
bool pr[100010];
vector<int> v;
void sieve() {
memset(pr, true, sizeof(pr));
v.clear();
pr[1] = false;
for (int i = 2; i <= 100000; i++) {
if (pr[i]) {
v.push_back(i);
for (int j = i; j <= 100000 / i; j++) {
pr[i * j] = false;
}
}
}
}
int main() {
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
dist[i] = 1;
}
ans = 1;
for (int i = 1; i <= n; i++) {
for (int x = 2; x * i <= n; x++) {
int j = x * i;
if (s[j] > s[i]) {
dist[j] = max(dist[j], dist[i] + 1);
}
}
ans = max(ans, dist[i]);
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7;
long long power(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = ((res) * (x));
res %= N;
y = y >> 1;
x = ((x) * (x));
}
return res;
}
long long powmod(long long a, long long b, long long n) {
long long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y) % n;
}
y = (y * y) % n;
b /= 2;
}
return x % n;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> w(n + 1);
w.push_back(0);
for (long long i = 1; i <= n; i++) cin >> w[i];
vector<long long> d(n + 1, 0);
for (long long i = 1; i <= n; i++) {
d[i] = 1;
long long m = 0;
for (long long j = 1; j <= sqrtl(i); j++) {
if (i % j == 0 && w[i] > w[j]) m = max(d[j], m);
if (i % j == 0 && w[i] > w[i / j]) m = max(d[i / j], m);
}
d[i] += m;
}
long long maxm = 0;
for (auto x : d) maxm = max(maxm, x);
cout << maxm << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int T, n;
int f[N], a[N];
vector<int> v[N];
int main() {
scanf("%d", &T);
for (register int i = 1; i <= 1e5; ++i)
for (register int j = i; j <= 1e5; j += i) v[j].push_back(i);
while (T--) {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (register int i = 1; i <= n; ++i) f[i] = 0;
f[1] = 1;
for (register int i = 2; i <= n; ++i) {
f[i] = 1;
int size = v[i].size() - 1;
for (register int j = 0; j <= size; ++j)
if (a[v[i][j]] < a[i]) f[i] = max(f[i], f[v[i][j]] + 1);
}
int ans = 0;
for (register int i = 1; i <= n; ++i) ans = max(ans, f[i]);
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
string s[2];
int a[maxn];
int n;
int main() {
cin >> n;
cin >> s[0] >> s[1];
for (int i = 1; i <= n; ++i) {
a[i] = a[i - 1];
if (s[0][i - 1] == '1' && s[1][i - 1] == '0') a[i]++;
if (s[0][i - 1] == '0' && s[1][i - 1] == '1') a[i]--;
}
int mx = 0, mn = 0;
for (int i = 1; i <= n; ++i) mx = max(mx, a[i]), mn = min(mn, a[i]);
if (a[n] == 0)
cout << mx - mn;
else
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, a[1000005], b[1000005], c[2], cnt, p[1000005];
int t[1000005 * 4][2];
void maketree(int x, int l, int r) {
if (l == r) {
t[x][0] = t[x][1] = 0, t[x][p[l]] = l;
return;
}
int mid = (l + r) >> 1;
maketree(x << 1, l, mid), maketree(x << 1 ^ 1, mid + 1, r);
t[x][0] = (t[x << 1][0]) ? t[x << 1][0] : t[x << 1 ^ 1][0];
t[x][1] = (t[x << 1][1]) ? t[x << 1][1] : t[x << 1 ^ 1][1];
}
void change(int x, int l, int r, int v) {
if (l == r) {
t[x][0] = t[x][1] = 0;
return;
}
int mid = (l + r) >> 1;
if (v <= mid)
change(x << 1, l, mid, v);
else
change(x << 1 ^ 1, mid + 1, r, v);
t[x][0] = (t[x << 1][0]) ? t[x << 1][0] : t[x << 1 ^ 1][0];
t[x][1] = (t[x << 1][1]) ? t[x << 1][1] : t[x << 1 ^ 1][1];
}
int find(int x, int l, int r, int L, int R, int v) {
if (l > R || r < L) return 0;
if (L <= l && r <= R) return t[x][v];
int mid = (l + r) >> 1, tmp = find(x << 1, l, mid, L, R, v);
if (tmp)
return tmp;
else
return find(x << 1 ^ 1, mid + 1, r, L, R, v);
}
int main() {
scanf("%d", &n);
char ch = getchar();
while (ch < '0' || ch > '1') ch = getchar();
for (i = 1; i <= n; i++) a[i] = ch - '0', ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
for (i = 1; i <= n; i++) b[i] = ch - '0', ch = getchar();
for (i = 1; i <= n; i++)
if (a[i] != b[i]) p[++cnt] = a[i] > b[i];
int res = 0, ans = 0;
for (i = 1; i <= cnt; i++) res += p[i] ? 1 : -1;
if (res != 0) printf("-1"), exit(0);
if (!cnt) printf("0"), exit(0);
maketree(1, 1, cnt);
res = cnt;
while (res) {
ans++;
int x = find(1, 1, cnt, 1, cnt, 0), y = find(1, 1, cnt, 1, cnt, 1), v,
now = max(x, y);
change(1, 1, cnt, x), change(1, 1, cnt, y), res -= 2;
if (x > y)
v = 1;
else
v = 0;
if (!res) break;
x = find(1, 1, cnt, now + 1, cnt, v);
while (x) {
y = find(1, 1, cnt, x + 1, cnt, v ^ 1);
if (y) {
change(1, 1, cnt, x), change(1, 1, cnt, y), res -= 2;
now = y;
if (!res) break;
x = find(1, 1, cnt, now + 1, cnt, v);
} else
break;
}
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s, t;
cin >> n >> s >> t;
vector<int> v;
int one = 0, zero = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] != t[i]) {
if (s[i] == '1') {
v.push_back(0);
zero++;
} else {
v.push_back(1);
one++;
}
}
}
if (one != zero) {
cout << "-1\n";
return;
}
int ua = 0, ub = 0, mxa = 0, mxb = 0;
for (int i = 0; i < v.size(); i++) {
if (!v[i]) {
if (ub == 0)
ua++;
else
ub--;
} else {
if (ua == 0)
ub++;
else
ua--;
}
mxa = max(mxa, ua);
mxb = max(mxb, ub);
}
cout << mxa + mxb;
return;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, sum, ma, mi, ans;
char s1[maxn], s2[maxn];
int main() {
scanf("%d %s %s", &n, s1 + 1, s2 + 1);
for (int i = 1; i <= n; i++) {
sum += s1[i] - '0';
sum -= s2[i] - '0';
ma = max(ma, sum);
mi = min(mi, sum);
}
if (sum != 0) {
ans = -1;
} else {
ans = ma - mi;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = (long long int)1e9 + 7;
long long int powmod(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b = b >> 1;
}
return res % MOD;
}
long long int kad(vector<long long int> &a) {
if (a.size() == 0) {
return 0;
}
long long int ans = 0;
long long int temp = 0;
for (long long int i = 0; i < a.size(); i++) {
temp += a[i];
if (temp < 0) {
temp = 0;
}
ans = max(ans, temp);
}
return ans;
}
void solve(long long int testnum) {
long long int n;
cin >> n;
string s, t;
cin >> s;
cin >> t;
vector<long long int> pcs, pcb;
long long int cnt = 0;
for (long long int i = 0; i < n; i++) {
if (s[i] == '0' && t[i] == '1') {
pcs.push_back(1);
pcb.push_back(-1);
cnt++;
} else if (s[i] == '1' && t[i] == '0') {
pcs.push_back(-1);
pcb.push_back(1);
cnt--;
}
}
if (cnt != 0) {
cout << "-1\n";
return;
}
if (pcs.size() == 0) {
cout << "0\n";
return;
}
long long int ans = max(kad(pcs), kad(pcb));
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
for (long long int i = 1; i <= t; i++) {
solve(i);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int ll_MAX = 1e18 + 5;
const long long int MOD = 1e9 + 7;
long long int fast_expo(long long int x, long long int p) {
if (p == 0)
return 1;
else if (p % 2 == 0) {
long long int t = fast_expo(x, p / 2) % MOD;
return (t * t) % MOD;
} else
return (x * (fast_expo(x, p - 1)) % MOD) % MOD;
}
long long int gcd(long long int a, long long int b) {
if (a == 0 || b == 0)
return a + b;
else
return gcd(b % a, a);
}
bool check(long long int arr[], long long int s, long long int e,
long long int val, long long int num) {
long long int cnt = 0;
for (long long int i = s; i <= e; ++i) {
if (arr[i] <= val) {
cnt++;
i++;
}
}
if (cnt >= num) return true;
return false;
}
int main() {
long long int t_c;
t_c = 1;
for (long long int t_i = 0; t_i < t_c; ++t_i) {
long long int n;
cin >> n;
string s, t;
cin >> s >> t;
long long int cnt = 0;
for (auto i : s) {
if (i == '0') cnt++;
}
for (auto i : t) {
if (i == '0') cnt--;
}
if (cnt != 0) {
cout << -1;
return 0;
}
string str;
for (long long int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
str.push_back(s[i]);
}
}
if (str.size() == 0) {
cout << 0;
return 0;
}
long long int curr0 = 0, curr1 = 0;
vector<long long int> vect;
for (auto i : str) {
if (i == '0') {
curr0++;
if (curr1 != 0) vect.push_back(curr1);
curr1 = 0;
} else {
curr1++;
if (curr0 != 0) vect.push_back(-curr0);
curr0 = 0;
}
}
if (curr1) vect.push_back(curr1);
if (curr0) vect.push_back(-curr0);
if (vect.size() == 0) {
cout << 0 << "\n";
return 0;
}
long long int ans = 0;
vector<long long int> vect2;
while (1) {
ans++;
if (vect[vect.size() - 1] * vect[0] < 0) {
for (long long int i = 0; i < vect.size(); ++i) {
if (vect[i] > 1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] - 1);
} else
vect2.push_back(vect[i] - 1);
} else if (vect[i] < -1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] + 1);
} else
vect2.push_back(vect[i] + 1);
}
}
} else {
for (long long int i = 0; i < vect.size() - 1; ++i) {
if (vect[i] > 1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] - 1);
} else
vect2.push_back(vect[i] - 1);
} else if (vect[i] < -1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] + 1);
} else
vect2.push_back(vect[i] + 1);
}
}
if (vect[vect.size() - 1] > 0) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[vect.size() - 1]);
} else
vect2.push_back(vect[vect.size() - 1]);
} else if (vect[vect.size() - 1] < 0) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[vect.size() - 1]);
} else
vect2.push_back(vect[vect.size() - 1]);
}
}
if (vect2.size() == 0) break;
vect = vect2;
vect2.clear();
}
cout << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B, typename C>
struct triple {
A X;
B Y;
C Z;
triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {}
};
template <typename A, typename B, typename C>
triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) {
return triple<A, B, C>(a, b, c);
}
template <typename A, typename B, typename C>
bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) {
if (a.X != b.X) return a.X < b.X;
if (a.Y != b.Y) return a.Y < b.Y;
return a.Z < b.Z;
}
template <typename T, typename SS>
ostream& operator<<(ostream& ofs, const pair<T, SS>& p) {
ofs << "( " << p.first << " , " << p.second << " )";
return ofs;
}
template <typename T>
void print(T a) {
for (auto i : a) cout << i << ' ';
cout << '\n';
}
template <typename T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <typename T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
signed main() {
srand(time(NULL));
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
string s, t;
cin >> s >> t;
vector<long long> a(n);
for (long long i = 0; i < (n); i++)
if (s[i] != t[i]) a[i] = (s[i] == '1' ? 1 : -1);
long long z = 0;
long long ans = 0, now = 0;
for (long long i = 0; i < (n); i++) {
now += a[i];
ans = max(ans, now);
now = max(now, 0LL);
}
z = max(z, ans);
for (long long i = 0; i < (n); i++) a[i] = -a[i];
now = ans = 0;
for (long long i = 0; i < (n); i++) {
now += a[i];
ans = max(ans, now);
now = max(now, 0LL);
}
z = max(z, ans);
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if (s != t)
cout << "-1\n";
else
cout << z << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = 0;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int mod = 1e9 + 7;
const int maxn = 1e6 + 100;
int T, n, m, q, k;
char ss[maxn << 1], tt[maxn << 1];
int main() {
n = read();
int nu1 = 0, nu2 = 0;
scanf("%s", ss);
scanf("%s", tt);
for (int i = 0; i < n; i++) {
if (ss[i] == '1') nu1++;
if (tt[i] == '1') nu2++;
ss[i + n] = ss[i];
tt[i + n] = tt[i];
}
if (nu1 != nu2) {
printf("-1\n");
return 0;
}
int ans = 0, ji = 0, mx = 0, mn = 0;
for (int i = 0; i < n * 2; i++) {
if (ss[i] == '1' && tt[i] == '0')
ji++;
else if (ss[i] == '0' && tt[i] == '1')
ji--;
mx = max(mx, ji);
mn = min(mn, ji);
}
cout << mx - mn << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int mod_pow(int num, int power) {
int test;
for (test = 1; power; power >>= 1) {
if (power & 1) test = (test * num) % 1000000007;
num = (num * num) % 1000000007;
}
return test;
}
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
if (negative) number *= -1;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return (a.first.second < b.first.second);
}
vector<int> con[3000005];
int A[3000005], B[3000005];
int N, K;
void solve(int test_num) {
cin >> N;
string str;
cin >> str;
int su = 0;
for (int i = 0; i < N; i++) {
A[i] = (str[i] - '0');
su += A[i];
}
cin >> str;
for (int i = 0; i < N; i++) {
B[i] = (str[i] - '0');
su -= B[i];
}
if (su != 0) {
cout << "-1" << endl;
return;
}
vector<int> vc;
for (int i = 0; i < N; i++) {
if (A[i] == 0 && B[i] == 1) vc.push_back(-1);
if (A[i] == 1 && B[i] == 0) vc.push_back(1);
}
if (vc.size() == 0) {
cout << 0 << endl;
return;
}
int ans = 0;
int cu = 0;
for (int i = 0; i < vc.size(); i++) {
cu += vc[i];
cu = max(cu, 0);
ans = max(ans, cu);
}
cu = 0;
for (int i = 0; i < vc.size(); i++) {
cu += -1 * vc[i];
cu = max(cu, 0);
ans = max(ans, cu);
}
cout << ans << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
T = 1;
for (int i = 0; i < T; i++) {
solve(i + 1);
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
void setIO(const string &name = "") {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (name.length()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
template <typename T>
void read(vector<T> &a) {
for (auto &x : a) cin >> x;
}
template <typename T>
void read(vector<T> &a, long long n) {
a.resize(n);
for (auto &x : a) cin >> x;
}
template <typename T>
void print(vector<T> &a) {
for (const auto &x : a) cout << x << ' ';
cout << '\n';
}
void MOD(long long &x) {
x %= 1000000007;
if (x < 0) x += 1000000007;
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long n;
string s, t;
vector<char> disp;
int32_t main() {
setIO();
cin >> n;
cin >> s >> t;
long long nums0 = 0, nums1 = 0;
long long numt0 = 0, numt1 = 0;
for (auto(i) = 0; i < n; i++) {
if (s[i] != t[i]) {
disp.emplace_back(s[i]);
if (s[i] == '1') {
nums1++;
numt0++;
} else {
nums0++;
numt1++;
}
}
}
if (nums0 != numt0 or nums1 != numt1) {
cout << "-1\n";
return 0;
}
if (disp.empty()) {
cout << "0\n";
return 0;
}
long long ans = 0;
long long end0 = 0, end1 = 0;
for (auto x : disp) {
if (x == '0') {
if (end1 == 0) {
ans++;
} else {
end1--;
}
end0++;
} else {
if (end0 == 0) {
ans++;
} else {
end0--;
}
end1++;
}
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1) {
cerr << name << " = " << arg1 << " ]"
<< "\n";
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " = " << arg1 << ",";
debug_out(comma + 1, args...);
}
const long long mod = 1e9 + 7;
const long long N = 1e6 + 5, M = 2e5 + 5;
long long n;
string s, t;
void Solve_main() {
cin >> n >> s >> t;
if (count((s).begin(), (s).end(), '1') !=
count((t).begin(), (t).end(), '1')) {
cout << -1 << '\n';
return;
}
if (s == t) {
cout << 0 << '\n';
return;
}
long long mx = -2e6, mn = 2e6;
long long curr = 0;
for (long long i = (0); i < (n); ++i) {
if (s[i] != t[i]) {
if (s[i] == '1')
curr++;
else
curr--;
mx = max(mx, curr);
mn = min(mn, curr);
}
}
long long res = abs(mx) + abs(mn);
cout << res << '\n';
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
long long tc = 1;
for (long long i = 1; i <= tc; i++) {
Solve_main();
}
cerr << "[time:" << 1.0 * clock() / CLOCKS_PER_SEC << "s] ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> fact(500005);
long long int modInverse(long long int a, long long int m) {
long long int mom = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m;
long long int top = m;
m = a % m;
a = top;
top = y;
y = x - q * y;
x = top;
}
if (x < 0) x += mom;
return x % 1000000007;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res % p;
}
vector<long long int> getAllFactors(long long int n) {
vector<long long int> v;
for (long long int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
v.push_back(i);
else {
v.push_back(i);
v.push_back(n / i);
}
}
}
return v;
}
inline bool isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' ||
c == 'E' || c == 'I' || c == 'O' || c == 'U')
return true;
else
return false;
}
inline bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); }
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
inline void preComputeFactorial() {
fact[0] = 1;
for (long long int i = 1; i <= 500005; i++)
fact[i] = (fact[i - 1] % 1000000007 * i % 1000000007) % 1000000007;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
if (b == 0) return a;
if (a == b) return a;
if (a > b) return gcd(a - b, b);
return gcd(a, b - a);
}
long long int computeXOR(long long int n) {
if (n % 4 == 0) return n;
if (n % 4 == 1) return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
long long int setBitNumber(long long int n) {
long long int k = (long long int)(log2(n));
return (long long int)(pow(2, k));
}
long long int task(long long int n, string s, string t) {
if (s == t) return 0;
if (count(s.begin(), s.end(), '1') == 1) return 1;
set<long long int> alpha, beta, gamma;
for (long long int i = 0; i <= n - 1; i++) {
if (s[i] != t[i]) {
if (s[i] == '0')
alpha.insert(i);
else
beta.insert(i);
gamma.insert(i);
}
}
long long int result = 0;
while (alpha.size() && beta.size()) {
long long int id = *gamma.begin(), flag = 0, e;
vector<long long int> hole;
hole.push_back(id);
if (s[id] == '0') {
while (true) {
if (flag == 0) {
if (beta.upper_bound(id) == beta.end()) break;
e = *beta.upper_bound(id);
hole.push_back(e);
id = e;
} else {
if (alpha.upper_bound(id) == alpha.end()) break;
e = *alpha.upper_bound(id);
hole.push_back(e);
id = e;
}
flag ^= 1;
}
if (s[hole[0]] == s[hole.back()]) hole.pop_back();
for (auto itr : hole) {
if (s[itr] == '0') {
alpha.erase(itr);
gamma.erase(itr);
} else {
beta.erase(itr);
gamma.erase(itr);
}
}
} else {
flag = 1;
while (true) {
if (flag == 0) {
if (beta.upper_bound(id) == beta.end()) break;
e = *beta.upper_bound(id);
hole.push_back(e);
id = e;
} else {
if (alpha.upper_bound(id) == alpha.end()) break;
e = *alpha.upper_bound(id);
hole.push_back(e);
id = e;
}
flag ^= 1;
}
if (s[hole[0]] == s[hole.back()]) hole.pop_back();
for (auto itr : hole) {
if (s[itr] == '0') {
alpha.erase(itr);
gamma.erase(itr);
} else {
beta.erase(itr);
gamma.erase(itr);
}
}
}
result += 1;
}
return result;
}
void a2oJ() {}
void hackerRank() {}
void hackerEarth() {}
void spoj() {}
void codeForces() {}
void codeChef() {
long long int n;
cin >> n;
string s, t;
cin >> s;
cin >> t;
long long int zero = 0;
long long int one = 0;
for (long long int i = 0; i <= n - 1; i++) {
if (s[i] == '0')
zero++;
else
one++;
}
for (long long int i = 0; i <= n - 1; i++) {
if (t[i] == '0')
zero--;
else
one--;
}
long long int answer = -1;
if (zero == 0 && one == 0) {
answer = task(n, s, t);
}
cout << answer;
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int testcase;
testcase = 1;
while (testcase--) {
codeForces();
codeChef();
spoj();
hackerEarth();
hackerRank();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, k;
string s, t;
int arr[N];
int get(int x) {
int curr = 0, mx = 0;
for (int i = 1; i <= n; i++) {
curr += x * arr[i];
mx = max(mx, curr);
if (curr < 0) curr = 0;
}
return mx;
}
void test_case() {
cin >> n >> s >> t;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (s[i - 1] != t[i - 1]) {
if (s[i - 1] == '1')
arr[i] = -1;
else
arr[i] = 1;
}
sum += arr[i];
}
if (sum != 0) {
cout << -1;
return;
}
int ans = max(get(1), get(-1));
cout << ans;
cout << "\n";
return;
}
void solve() {
int t;
t = 1;
for (int nr = 1; nr <= t; nr++) {
test_case();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double getTime() { return clock() / (double)CLOCKS_PER_SEC; }
void read(){};
template <typename T, typename... Args>
void read(T& a, Args&... args) {
cin >> a;
read(args...);
}
void print(){};
template <typename T, typename... Args>
void print(T a, Args... args) {
cout << a << " \n"[sizeof...(args) == 0];
print(args...);
}
void solve() {
int n;
read(n);
string s, t;
cin >> s >> t;
int p = count(s.begin(), s.end(), '1');
int q = count(t.begin(), t.end(), '1');
if (p != q) {
print(-1);
return;
}
set<int> idx[2];
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
idx[s[i] - '0'].insert(i);
}
}
idx[0].insert(n), idx[1].insert(n);
int moves = 0;
while (*idx[0].begin() != n or *idx[1].begin() != n) {
int cur = 0;
if (*idx[0].begin() > *idx[1].begin()) cur = 1;
int val = 0;
while (true) {
auto it = idx[cur].lower_bound(val);
val = *it;
if (val == n) break;
idx[cur].erase(it);
cur ^= 1;
}
moves++;
}
print(moves);
}
int main() {
int t;
t = 1;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void rd(T &l) {
l = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
l = l * 10 + (ch ^ '0');
ch = getchar();
}
l = (f ? l : -l);
}
char s[1000005], t[1000005];
int main() {
int n, ans1 = 0, ans2 = 0;
rd(n);
scanf("%s", s + 1);
scanf("%s", t + 1);
for (int i = 1; i <= n; ++i) {
if (s[i] == '0') ++ans1;
if (t[i] == '0') ++ans2;
}
if (ans1 != ans2) {
puts("-1");
return 0;
}
ans1 = ans2 = 0;
for (int i = 1; i <= n; ++i)
if (s[i] != t[i]) {
if (s[i] == '0') {
if (!ans1)
++ans2;
else
--ans1, ++ans2;
} else {
if (!ans2)
++ans1;
else
--ans2, ++ans1;
}
}
printf("%d\n", ans1 + ans2);
return 0;
}
|
#include <bits/stdc++.h>
int read() {
int ans = 0, c, f = 1;
while (!isdigit(c = getchar()))
if (c == '-') f *= -1;
do ans = ans * 10 + c - '0';
while (isdigit(c = getchar()));
return ans * f;
}
const int N = 1000050;
int n;
char s[N], t[N];
int main() {
scanf("%d%s%s", &n, s, t);
int p = 0, q = 0;
int a = 0, b = 0, m = 0;
for (int i = 0; i < n; ++i)
if (s[i] != t[i]) {
if (s[i] == '0') {
++p;
++a;
if (b) --b, ++m;
} else {
++q;
++b;
if (a) --a, ++m;
}
}
if (p != q)
printf("%d\n", -1);
else
printf("%d\n", p + q - m);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
int num[2][2];
int zero[MAX + 5], one[MAX + 5];
char s[MAX + 5], t[MAX + 5];
int main() {
int n;
scanf("%d", &n);
getchar();
scanf("%s", s + 1);
scanf("%s", t + 1);
for (int i = n; i > 0; i--) {
if (s[i] == '1' && t[i] == '0') one[i] = 1;
one[i] += one[i + 1];
if (s[i] == '0' && t[i] == '1') zero[i] = 1;
zero[i] += zero[i + 1];
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1' && t[i] == '0') {
if (num[0][1] > 0) {
num[0][1]--;
num[1][1]++;
} else if (num[0][0] > 0 && zero[i] >= num[1][0] + 1) {
num[0][0]--;
num[1][0]++;
} else
num[1][0]++;
}
if (s[i] == '0' && t[i] == '1') {
if (num[1][0] > 0) {
num[1][0]--;
num[0][0]++;
} else if (num[1][1] > 0 && one[i] >= num[0][1] + 1) {
num[1][1]--;
num[0][1]++;
} else
num[0][1]++;
}
}
if (num[0][1] || num[1][0])
printf("-1\n");
else
printf("%d\n", num[1][1] + num[0][0]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Max = 1e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
long long n, cum[Max + 10];
long long cal(long long m) {
long long ans = 0, i;
for (i = 1; i <= n; i++) ans += min(cum[i] % m, m - (cum[i] % m));
return ans;
}
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
;
long long i, j, m, p, a, sum = 0, k, t, b, c, d, cnt = 0, q, l, r;
string str, ptr;
bool flag = true;
cin >> n;
cin >> str >> ptr;
a = 0, b = 0;
vector<long long> v;
long long o = 0, z = 0;
long long ans = 0;
for (i = 0; i < n; i++) {
a += (str[i] == '1');
b += (ptr[i] == '1');
if (str[i] == ptr[i]) continue;
if (str[i] == '0' && ptr[i] == '1')
z++, o--;
else {
o++;
z--;
}
z = max(z, 0ll);
o = max(o, 0ll);
ans = max(ans, abs(z - o));
}
if (a == b) {
cout << ans << endl;
} else
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
string s, t;
int arr[N];
int n;
int helper(int x) {
int cur_sum = 0;
int max_sum = 0;
for (int i = 1; i <= n; i++) {
cur_sum += x * arr[i];
max_sum = max(max_sum, cur_sum);
if (cur_sum < 0) {
cur_sum = 0;
}
}
return max_sum;
}
int main() {
cin >> n >> s >> t;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (s[i - 1] != t[i - 1]) {
if (s[i - 1] == '1') {
arr[i] = -1;
} else {
arr[i] = 1;
}
}
sum += arr[i];
}
if (sum != 0) {
cout << -1;
return 0;
} else {
int res = max(helper(1), helper(-1));
cout << res;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
inline long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
inline long long int lcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
inline long long int add(long long int a, long long int b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
inline long long int multi(long long int a, long long int b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
inline long long int sub(long long int a, long long int b) {
a %= MOD;
b %= MOD;
a -= b;
if (a < 0) a += MOD;
return a;
}
inline long long int power(long long int a, long long int b) {
a %= MOD;
long long int res = 1;
while (b > 0) {
if (b & 1) {
res = multi(res, a);
}
a = multi(a, a);
b >>= 1;
}
return res;
}
bool isPrime(long long int x) {
if (x <= 4 || x % 2 == 0 || x % 3 == 0) {
return x == 2 || x == 3;
}
for (long long int i = 5; i * i <= x; i += 6) {
if (x % i == 0 || x % (i + 2) == 0) {
return 0;
}
}
return 1;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
string s, t;
cin >> s >> t;
long long int c1 = 0, c2 = 0;
vector<long long int> res;
long long int ans = 0;
bool flag = 0;
for (long long int i = 0; i < n; i++) {
if (s[i] == '1') c1++;
if (t[i] == '1') c2++;
}
if (c1 != c2) {
cout << -1;
return 0;
}
long long int pos = 0, neg = 0;
for (long long int i = 0; i < n; i++) {
if (s[i] == '1' && t[i] == '0') {
if (neg > 0) {
pos++;
neg--;
} else
pos++;
} else if (s[i] == '0' && t[i] == '1') {
if (pos > 0) {
neg++;
pos--;
} else
neg++;
}
}
cout << neg + pos;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
string s, t;
void solve() {
cin >> n >> s >> t;
deque<int> d{0};
int st = 0, n0 = 0, n1 = 0;
for (int i = (0); i < (n); ++i)
if (s[i] != t[i]) {
if (s[i] == '0')
++n0;
else
++n1;
if (st == s[i] - '0') {
++d.back();
} else {
st ^= 1;
d.push_back(1);
}
}
if (n0 != n1) {
cout << "-1\n";
return;
}
if (d.front() == 0) d.pop_front();
if (int((d).size()) & 1) {
d.front() += d.back();
d.pop_back();
}
int ans = 0;
while (int((d).size())) {
int m = *min_element((d).begin(), (d).end());
for (auto& x : (d)) x -= m;
ans += m;
deque<int> dt = {0};
for (int i = (0); i < (int((d).size())); ++i) {
if (d[i])
dt.push_back(d[i]);
else if (++i < int((d).size()))
dt.back() += d[i];
}
if (dt.front() == 0) dt.pop_front();
d = dt;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int n, ans;
string s, t;
set<int> st0, st1;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s >> t;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
if (s[i] == '1')
st1.insert(i);
else
st0.insert(i);
}
}
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if (s != t) return cout << -1, 0;
while (!st0.empty()) {
ans++;
if (*st0.begin() < *st1.begin()) {
int last = -1;
while (1) {
auto it = st0.lower_bound(last);
if (it == st0.end()) break;
auto it2 = st1.lower_bound(*it);
if (it2 == st1.end()) break;
last = *it2;
st0.erase(it);
st1.erase(it2);
}
continue;
}
int last = -1;
while (1) {
auto it = st1.lower_bound(last);
if (it == st1.end()) break;
auto it2 = st0.lower_bound(*it);
if (it2 == st0.end()) break;
last = *it2;
st0.erase(it2);
st1.erase(it);
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 9;
long long power(long long x, long long y) {
if (y == 0) return 1;
long long temp = power(x, y / 2);
if (y % 2 == 1)
return temp * temp * x;
else
return temp * temp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s, t;
cin >> n >> s >> t;
int c1 = 0, c2 = 0;
for (char c : s) c1 += (c == '1');
for (char c : t) c2 += (c == '1');
if (c1 != c2) {
cout << "-1\n";
return 0;
}
vector<int> p;
for (int i = 0; i < n; i++)
if (s[i] != t[i]) p.push_back((s[i] == '0'));
int ans = 0;
c1 = 0, c2 = 0;
for (auto i : p) {
if (i == 0) {
if (c1)
c1--, c2++;
else
ans++, c2++;
} else {
if (c2)
c2--, c1++;
else
ans++, c1++;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long fexp(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
b /= 2;
a = a * a % 1000000007;
}
return ans;
}
long long inverse(long long a, long long p) { return fexp(a, p - 2); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long T = 1;
long long t = 0;
while (t++ < T) {
long long n;
string s, t;
cin >> n;
cin >> s >> t;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
if (s[i] == t[i])
a[i] = 0;
else if (s[i] == '1')
a[i] = 1;
else
a[i] = -1;
}
long long sum = 0;
for (long long i = 0; i < n; i++) sum += a[i];
if (sum != 0) {
cout << -1;
continue;
}
long long maxi = -1;
sum = 0;
for (long long i = 0; i < n; i++) {
sum += a[i];
if (sum < 0) sum = 0;
maxi = max(maxi, sum);
}
long long mini = 0;
sum = 0;
for (long long i = 0; i < n; i++) {
sum += a[i];
if (sum > 0) sum = 0;
mini = min(mini, sum);
}
long long ans = max(maxi, -mini);
cout << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int i, j, n, m, k;
string S, T;
string s;
void nhap() {
cin >> n >> S >> T;
S = " " + S;
T = " " + T;
}
bool xet(string A, string B) {
int dem[3];
dem[0] = dem[1] = 0;
for (int i = int(1); i <= int(n); i++) dem[A[i] - '0']++;
for (int i = int(1); i <= int(n); i++) dem[B[i] - '0']--;
if (!dem[0] && !dem[1]) return true;
return false;
}
void chuyen(string A, string B) {
s = " ";
for (int i = int(1); i <= int(n); i++)
if (A[i] != B[i]) s += A[i];
}
void xuli() {
if (!xet(S, T)) {
cout << -1 << "\n";
return;
}
chuyen(S, T);
int minn = 0, maxx = 0, ct = 0;
for (int i = int(1); i <= int(s.size() - 1); i++) {
if (s[i] == '1')
ct++;
else
ct--;
minn = min(minn, ct);
maxx = max(maxx, ct);
}
cout << maxx - minn << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
nhap();
xuli();
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.