text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
const long long N = 5e5 + 5;
using namespace std;
long long MOD(long long a, long long b) {
if (a < b) a += b;
a = a % b;
return ((a + b) % b);
}
long long modpow(long long x, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = MOD(ans * x, mod);
x = MOD(x * x, mod);
n >>= 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
vector<int> v[n + 1];
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
}
vector<bool> vis(n + 1);
vis[a[n]] = true;
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
int cnt = 0;
for (auto &j : v[a[i]])
if (vis[j]) cnt++;
if (cnt + i + ans == n)
ans++;
else
vis[a[i]] = true;
}
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 n, m;
cin >> n >> m;
vector<int> p(n);
vector<set<int>> c(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
c[u - 1].emplace(v - 1);
}
vector<int> P;
P.push_back(p[n - 1]);
for (int i = n - 2; i >= 0; --i) {
bool f = true;
for (int pp : P) {
if (c[p[i]].count(pp) == 0) {
f = false;
break;
}
}
if (!f) {
P.push_back(p[i]);
}
}
cout << n - P.size();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 3e5 + 10;
int flag[N];
set<int> s[N];
int a[N];
int pos[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pos[a[i]] = i;
}
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
if (x == a[n]) continue;
if (y == a[n]) {
flag[x] = 1;
}
s[x].emplace(y);
}
int ans = n;
for (int i = n - 1; i > 0; i--) {
if (flag[a[i]]) {
int cnt = 0;
for (auto& x : s[a[i]]) {
if (pos[x] < i) continue;
if (pos[x] >= ans) continue;
cnt++;
}
if (cnt != ans - i - 1)
flag[a[i]] = 0;
else {
for (int j = i; j < ans; j++) {
int x = a[j], y = a[j + 1];
swap(a[j], a[j + 1]);
swap(pos[x], pos[y]);
}
ans--;
}
}
}
printf("%d\n", n - ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, counter;
unordered_map<int, bool> mp[300005];
vector<int> elem;
vector<int> where;
int pos[300005];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> x;
elem.push_back(x);
pos[x] = i - 1;
}
int me = elem.back();
for (int i = 1; i <= m; i++) {
cin >> x >> y;
mp[y][x] = true;
if (y == me) where.push_back(pos[x]);
}
sort(where.begin(), where.end());
for (int i = where.size() - 1; i >= 0; i--) {
int now = where[i];
while (true) {
if (mp[elem[now + 1]][elem[now]])
swap(elem[now + 1], elem[now]);
else
break;
if (elem[now] == me) {
counter++;
break;
}
now++;
}
}
cout << counter;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int M = 5e5 + 5;
set<int> g[N];
int Nastya;
int p[N];
int n, m;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
cin >> n >> m;
for (int i = (1); i <= ((n)); i += (+1)) {
cin >> p[i];
}
Nastya = p[n];
for (int i = (0); i <= ((m)-1); i += (+1)) {
int u, v;
cin >> u >> v;
g[u].insert(v);
}
for (int i = (n); i >= (1); i += (-1)) {
int j = i;
while (j < n && g[p[j]].count(p[j + 1])) {
swap(p[j], p[j + 1]);
j++;
}
}
for (int i = (1); i <= ((n)); i += (+1)) {
if (p[i] == Nastya) {
cout << n - i << '\n';
break;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> vec;
long long a[300001], p, ans = 0;
set<pair<long long, long long> > s;
int main() {
long long n, m;
scanf("%lld %lld", &n, &m);
long long i, j, ans = 0;
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
p = a[n];
for (i = 0; i < m; i++) {
long long x, y;
scanf("%lld %lld", &x, &y);
s.insert(make_pair(x, y));
}
vec.push_back(a[n]);
for (i = n - 1; i >= 1; i--) {
long long now = a[i];
int check = 1;
for (j = 0; j < vec.size(); j++) {
if (s.count(make_pair(now, vec[j])) == 0) {
check = 0;
break;
}
}
if (check) {
ans++;
} else
vec.push_back(now);
}
printf("%lld", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 5;
set<int> s1[MAXN];
set<int> s2;
int p[MAXN];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
s1[x].insert(y);
}
s2.insert(p[n]);
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
bool ok = true;
for (auto x : s2) {
if (s1[p[i]].find(x) == s1[p[i]].end()) {
ok = false;
break;
}
}
if (ok) {
ans++;
} else {
s2.insert(p[i]);
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 10;
long long n, m, a[N], p[N], tmp1, tmp2, ans, t, Q;
bool mark[N], pass[N];
vector<long long> adj[N];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = i;
}
for (long long i = 1; i <= m; i++) {
cin >> tmp1 >> tmp2;
if (tmp2 == a[n]) {
mark[tmp1] = true;
} else if (p[tmp1] < p[tmp2]) {
adj[tmp1].push_back(tmp2);
}
}
for (long long i = n - 1; i >= 1; i--) {
if (mark[a[i]] == true) {
long long c = 0, d = 0;
for (auto x : adj[a[i]]) {
if (mark[x] == true && pass[x] != true) {
d++;
} else if (mark[x] != true) {
c++;
}
}
if (c == t && Q == d) {
pass[a[i]] = true;
ans++;
} else {
Q++;
}
} else {
t++;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
struct Edge {
int v, next;
} edge[N << 1];
int head[N], esize;
inline void addedge(int x, int y) {
edge[++esize] = (Edge){y, head[x]};
head[x] = esize;
}
int n, m, ans;
int a[N];
int d[N];
inline void ins(int x) {
for (int i = head[x], vv; ~i; i = edge[i].next) {
vv = edge[i].v;
++d[vv];
}
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
addedge(y, x);
}
ans = 0;
ins(a[n]);
for (int i = n - 1; i >= 1; --i) {
if (d[a[i]] == n - i) {
--n;
++ans;
} else {
ins(a[i]);
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int input[300005];
int input2[500005][2];
int link[300005];
unordered_set<int> front[300005];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &input[i]);
for (int i = 0; i < m; i++) {
scanf("%d%d", &input2[i][0], &input2[i][1]);
front[input2[i][0]].insert(input2[i][1]);
}
for (int i = 1; i <= n; i++) link[i] = i + 1;
int ans = 0;
for (int i = n - 1; i > 0; i--) {
bool isok = true;
for (int now = link[i]; now <= n; now = link[now]) {
if (!front[input[i]].count(input[now])) {
isok = false;
break;
}
}
if (isok) {
ans++;
link[i - 1] = link[i];
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300001;
vector<int> g[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
unordered_map<int, int> need;
need[p[n - 1]]++;
int ans = 0, size = 1;
for (int i = n - 2; i > -1; i--) {
int cnt = 0;
for (auto it : g[p[i]]) {
if (need[it]) cnt++;
}
if (cnt == size)
ans++;
else
need[p[i]]++, size++;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<set<int> > g;
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n);
g.resize(n);
vector<int> seq(n);
vector<int> ind(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
ind[p[i]] = i;
}
for (int i = 0; i < m; i++) {
int from, to;
cin >> from >> to;
from--;
to--;
if (ind[to] > ind[from]) g[from].insert(to);
}
vector<bool> P(n);
P[p[n - 1]] = true;
int psz = 0;
psz++;
for (int suf = 1; suf < n; suf++) {
int count = 0;
while (!g[p[n - suf - 1]].empty()) {
int to = *g[p[n - suf - 1]].begin();
g[p[n - 1 - suf]].erase(g[p[n - 1 - suf]].begin());
if (P[to]) count++;
}
if (count < psz) {
P[p[n - suf - 1]] = true;
psz++;
}
}
cout << n - psz;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
int arr[MAXN];
vector<int> depends[MAXN];
map<int, int> to2cnt;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
for (int i = 1; i <= m; ++i) {
int pre, tail;
scanf("%d%d", &pre, &tail);
depends[tail].push_back(pre);
}
int mov = 0;
for (int i = 0; i < (int)(depends[arr[n]].size()); ++i) {
++to2cnt[depends[arr[n]][i]];
}
for (int k = n - 1; k >= 1; --k) {
if (n - k - mov == to2cnt[arr[k]]) {
++mov;
} else {
for (int i = 0; i < (int)(depends[arr[k]].size()); ++i) {
++to2cnt[depends[arr[k]][i]];
}
}
}
printf("%d\n", mov);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[400006];
int me;
set<int> st[300005];
pair<int, int> pr;
int main() {
scanf("%d%d", &n, &m);
vector<int> vec(n);
for (int i = 1; i <= n; i++) {
scanf("%d", p + i);
vec[i - 1] = p[i];
}
for (int i = 1; i <= m; i++) {
scanf("%d%d", &pr.first, &pr.second);
st[pr.first].insert(pr.second);
}
me = p[n];
vec.pop_back();
for (int i = (int)vec.size() - 1; i >= 0; i--) {
int now = vec[i];
for (int j = i + 1; j < vec.size(); j++) {
if (st[now].count(vec[j])) {
swap(vec[j], vec[j - 1]);
} else
break;
}
if (st[vec.back()].count(me)) {
vec.pop_back();
}
}
cout << n - vec.size() - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
set<int> g[300000];
vi h;
int p[300000], q[300000];
bool bad[300000];
int badnum;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> p[i];
p[i]--;
q[p[i]] = i;
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u - 1].insert(v - 1);
if (v - 1 == p[n - 1]) h.push_back(u - 1);
}
sort(h.begin(), h.end(), [](int a, int b) { return q[a] > q[b]; });
int pos = n - 1, sol = 0;
for (int x : h) {
for (int i = q[x] + 1; i < pos; ++i) bad[p[i]] = true, badnum++;
int cnt = 0;
for (int y : g[x])
if (bad[y]) cnt++;
if (cnt == badnum)
sol++;
else
bad[x] = true, badnum++;
pos = q[x];
}
cout << sol;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, ans, sz;
int p[300010], va[300010];
bool el[300010], test;
pair<int, int> a[500010];
bool pos(int t1, int t2) {
int st = 1, dr = m, mij;
while (st < dr) {
mij = (st + dr) / 2;
if (make_pair(t1, t2) <= a[mij])
dr = mij;
else
st = mij + 1;
}
return (a[st] == make_pair(t1, t2));
}
int main() {
cin.tie(0);
cout.tie(0);
std::ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= m; i++) cin >> a[i].first >> a[i].second;
sort(a + 1, a + m + 1);
for (int i = n - 1; i >= 1; i--)
if (pos(p[i], p[n])) {
test = 1;
for (int j = 1; j <= sz; j++)
if (!pos(p[i], va[j])) {
test = 0;
break;
}
if (test)
ans++;
else
va[++sz] = p[i];
} else
va[++sz] = p[i];
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int order[300010];
vector<int> canChange[300010];
int canYield[300010];
int cantOvertake[300010];
int main(void) {
int N, M, cnt;
cin >> N >> M;
cnt = 0;
for (int i = 0; i < N; i++) cin >> order[i];
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
canChange[a].push_back(b);
if (b == order[N - 1]) canYield[a] = 1;
}
for (int i = N - 2; i >= 0; i--) {
int cur = order[i];
if (canYield[cur]) {
int cnt2 = 0;
for (int j : canChange[cur]) cnt2 += cantOvertake[j];
if (cnt2 != cnt) {
cantOvertake[cur] = 1;
cnt++;
}
} else {
cantOvertake[cur] = 1;
cnt++;
}
}
cout << N - 1 - cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, s1, s2, i, ans = 0, curr, areInSide;
cin >> n >> m;
vector<int> v(n), swaps[n];
for (i = 0; i < n; i++) {
cin >> v[i];
v[i]--;
}
for (i = 0; i < m; i++) {
cin >> s1 >> s2;
s1--;
s2--;
swaps[s1].push_back(s2);
}
vector<bool> isReady(n, false);
isReady[v[n - 1]] = true;
reverse(v.begin(), v.end());
areInSide = 1;
for (i = 1; i < n; i++) {
curr = 0;
for (auto it : swaps[v[i]]) {
if (isReady[it]) curr++;
}
if (curr == areInSide) {
ans++;
} else {
areInSide++;
isReady[v[i]] = true;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T lowbit(T x) {
return x & (-x);
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T Pow(T a, T b, T p) {
T ret = 1;
a %= p;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) (ret *= a) %= p;
return ret;
}
template <class T>
inline void read(T &ret) {
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
ret = x * f;
}
const int N = 3e5 + 10;
int n, m, p[N], loc[N], cnt[N];
vector<int> in[N], out[N];
int main() {
read(n);
read(m);
for (int i = (1); i <= (n); i++) read(p[i]), loc[p[i]] = i;
for (int i = (1); i <= (m); i++) {
int x, y;
read(x);
read(y);
x = loc[x];
y = loc[y];
if (y > x) cnt[x]++, in[y].push_back(x);
}
int ans = 0;
for (int i = (n - 1); i >= (1); i--)
if (cnt[i] == n - i - ans) {
for (auto j = in[i].begin(); j != in[i].end(); j++) cnt[*j]--;
ans++;
}
printf("%d", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int p[300010];
set<int> s[300010];
int no[300010];
int cnt = 0;
int main() {
int n, m;
int na = 0;
int total = 0;
scanf("%d%d", &n, &m);
int i, j, k;
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
na = p[n - 1];
for (i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
s[u].insert(v);
}
for (i = n - 2; i >= 0; i--) {
if (s[p[i]].find(na) != s[p[i]].end()) {
for (j = 0; j < cnt; j++) {
if (s[p[i]].find(no[j]) == s[p[i]].end()) {
break;
}
}
if (j >= cnt) {
total++;
} else {
no[cnt++] = p[i];
}
} else {
no[cnt++] = p[i];
}
}
printf("%d\n", total);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 550000;
unordered_map<int, int> H[N];
vector<int> cand, adj[N];
int p[N], c[N], pos[N], vis[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (v == p[n]) cand.push_back(u);
adj[u].push_back(v);
H[u][v] = 1;
}
sort(cand.begin(), cand.end(), [](int x, int y) { return pos[x] > pos[y]; });
int ans = 0;
vector<int> stones;
int removed = 0;
for (int x : cand) {
for (int y : adj[x])
if (pos[y] > pos[x] && y != p[n] && !vis[y]) c[x]++;
int cur = pos[x];
if (n - 1 - cur - removed == c[x]) {
removed++;
vis[x] = 1;
}
}
cout << removed << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void input(bool test, int que[], int &que_n, vector<int> cch[]);
int howMuch(int que[], int que_n, vector<int> cch[]);
void core();
void input(bool test, int que[], int &que_n, vector<int> cch[]) {
FILE *fin;
if (test) {
fin = fopen("input.txt", "r");
if (fin == nullptr) {
exit(EXIT_FAILURE);
}
} else
fin = stdin;
int cch_n;
fscanf(fin, "%d%d", &que_n, &cch_n);
for (int i = 0; i != que_n; ++i) fscanf(fin, "%d", que + i);
int a, b;
for (int i = 0; i != cch_n; ++i) {
fscanf(fin, "%d%d", &a, &b);
cch[a].push_back(b);
}
}
int howMuch(int que[], int que_n, vector<int> cch[]) {
int counter = 0;
set<int> toPass;
toPass.insert(que[que_n - 1]);
for (int i = que_n - 2; i >= 0; --i) {
bool canPass = true;
auto it1 = cch[que[i]].begin();
auto it2 = toPass.begin();
while ((it1 != cch[que[i]].end()) && (it2 != toPass.end())) {
if (*it1 == *it2) {
++it1;
++it2;
} else if (*it1 < *it2) {
++it1;
} else {
break;
}
}
if (it2 != toPass.end()) canPass = false;
if (canPass) {
++counter;
} else {
toPass.insert(que[i]);
}
}
return counter;
}
int que[300005];
vector<int> cch[300005];
void core() {
int que_n;
input(false, que, que_n, cch);
for (int i = 1; i <= que_n; ++i) sort(cch[i].begin(), cch[i].end());
cout << howMuch(que, que_n, cch) << endl;
}
int main() {
core();
char notToCloseConsole;
cin >> notToCloseConsole;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int a[N], vis[N];
vector<int> v[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
for (int i = (1); i <= (m); i++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
}
vis[a[n]] = 1;
int ans = 0;
int p = 1;
for (int i = (n - 1); i >= (1); i--) {
int tmp = p;
int now = a[i];
for (int j = (0); j <= (int(v[now].size()) - 1); j++) {
int next = v[now][j];
if (vis[next]) tmp--;
}
if (tmp == 0)
ans++;
else {
vis[now] = 1;
p++;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = (int)3e5 + 17;
const int mod = (int)1e9 + 7;
int N, M, T, K;
int a[MAX_N];
set<int> S[MAX_N];
int main() {
while (~scanf("%d %d", &N, &M)) {
for (int i = 1; i <= N; ++i) {
scanf("%d", &a[i]);
S[i].clear();
}
int x, y;
for (int i = 0; i < M; ++i) {
scanf("%d %d", &x, &y);
S[x].insert(y);
}
int cur = N;
for (int i = N - 1; i >= 1; --i) {
if (S[a[i]].find(a[cur]) != S[a[i]].end()) {
int p = i;
while (p < cur - 1 && S[a[p]].find(a[p + 1]) != S[a[p]].end()) {
swap(a[p], a[p + 1]);
++p;
}
if (p == cur - 1) {
swap(a[p], a[p + 1]);
cur--;
}
}
}
printf("%d\n", N - cur);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[300005];
int P[300005];
int T[300005];
vector<int> v[300005];
int fenwick[300005];
void updt(int idx) {
while (idx <= n) {
fenwick[idx]++;
idx += (idx & (-idx));
}
}
int sum(int idx) {
int ret = 0;
while (idx > 0) {
ret += fenwick[idx];
idx -= (idx & (-idx));
}
return ret;
}
vector<int> z[300005];
int L[300005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> A[i], P[A[i]] = i;
for (int i = 1; i <= n; i++) T[i] = n - i;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (P[a] < P[b]) {
T[P[a]]--;
z[P[b]].push_back(P[a]);
}
}
for (int i = 1; i < n; i++) {
v[T[i]].push_back(i);
}
int ans = 0;
for (int idx = n - 1; idx >= 1; idx--) {
int cnt = sum(n) - sum(idx) - L[idx];
if (cnt == T[idx]) {
updt(idx);
ans++;
for (int k : z[idx]) {
L[k]++;
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
mt19937 gen(time(0));
struct Node {
int v, s;
long long k;
Node *l, *r;
Node(int k = 0) : k(k), v(gen()), s(1), l(0), r(0) {}
void pull() { s = (l ? l->s : 0) + (r ? r->s : 0) + 1; }
void *operator new(size_t);
};
Node mem[5000000];
int top = 0;
void *Node::operator new(size_t) { return mem + top++; }
Node *merge(Node *a, Node *b) {
if (!a) return b;
if (!b) return a;
if (a->v < b->v) {
a->r = merge(a->r, b);
a->pull();
return a;
} else {
b->l = merge(a, b->l);
b->pull();
return b;
}
}
void split(Node *cur, long long k, Node *&a, Node *&b) {
if (!cur) {
a = b = 0;
return;
}
if (k <= cur->k) {
b = cur;
split(cur->l, k, a, b->l);
b->pull();
} else {
a = cur;
split(cur->r, k, a->r, b);
a->pull();
}
}
Node *insert(Node *root, long long k) {
Node *tmp = new Node(k), *a, *b;
split(root, k, a, b);
return merge(merge(a, tmp), b);
}
Node *erase(Node *root, long long k) {
Node *a, *b;
split(root, k, a, root);
split(root, k + 1, root, b);
return merge(a, b);
}
vector<long long> bit;
long long sum(int x) {
long long ret = 0;
while (x) {
ret += bit[x];
x -= (x & -x);
}
return ret;
}
void inc(int x, long long v) {
while (x < bit.size()) {
bit[x] += v;
x += (x & -x);
}
}
vector<long long> arr, heap;
void build(int i, int l, int r) {
if (l == r) {
heap[i] = arr[l];
return;
}
int m = (l + r) >> 1;
build(((i << 1) + 1), l, m);
build(((i << 1) + 2), m + 1, r);
heap[i] = heap[((i << 1) + 1)] + heap[((i << 1) + 2)];
}
int p;
long long v;
void update(int i, int l, int r) {
if (l == r) {
heap[i] += v;
return;
}
int m = (l + r) >> 1;
if (p <= m)
update(((i << 1) + 1), l, m);
else
update(((i << 1) + 2), m + 1, r);
heap[i] = heap[((i << 1) + 1)] + heap[((i << 1) + 2)];
}
int ql, qr;
long long res;
void query(int i, int l, int r) {
if (ql <= l && r <= qr) {
res += heap[i];
return;
}
int m = (l + r) >> 1;
if (ql <= m) query(((i << 1) + 1), l, m);
if (qr > m) query(((i << 1) + 2), m + 1, r);
}
long long powi(long long a, long long b) {
long long ret = 1;
for (; b; b >>= 1, a = a * a % 1000000007) {
if (b & 1) ret = ret * a % 1000000007;
}
return ret;
}
long long inv(long long a) { return powi(a, 1000000007 - 2); }
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i], --arr[i];
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a - 1].push_back(b - 1);
}
vector<bool> v(n, 0);
v[arr[n - 1]] = 1;
int ans = 0, cnt;
for (int i = n - 2; i >= 0; i--) {
cnt = 0;
for (auto j : adj[arr[i]]) {
if (v[j]) cnt++;
}
if (i + cnt + ans == n - 1)
ans++;
else
v[arr[i]] = 1;
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int N, M, arr[maxn], pos[maxn], bit[maxn];
bool marked[maxn], done[maxn];
vector<int> g[maxn];
inline void update(int pos, int val) {
for (int i = pos; i <= N; i += i & -i) bit[i] += val;
}
inline int query(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & -i) res += bit[i];
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
pos[arr[i]] = i;
}
for (int i = 1; i <= M; i++) {
int a, b;
cin >> a >> b;
if (b == arr[N]) marked[a] = true;
g[a].push_back(b);
}
int res = marked[arr[N - 1]];
for (int i = N - 2; i >= 1; i--) {
if (!marked[arr[i]]) continue;
int req = N - res - i - 1, l = i + 1, r = N - res - 1;
for (int ck : g[arr[i]]) {
if (done[ck]) continue;
int pp = pos[ck] - query(pos[ck]);
if (pp >= l && pp <= r) req--;
}
if (req == 0) {
update(i, 1);
res++, done[arr[i]] = true;
}
}
cout << res << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
struct Edge {
int go, next;
} e[N];
int head[N], tot;
void add(int x, int y) {
e[++tot].go = y;
e[tot].next = head[x];
head[x] = tot;
}
int pos[N], num[N], p[N], cnt;
bool cmp(int x, int y) { return pos[x] > pos[y]; }
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
pos[t] = i;
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (pos[u] < pos[v]) {
num[u]++;
add(v, u);
if (pos[v] == n) {
p[++cnt] = u;
}
}
}
sort(p + 1, p + cnt + 1, cmp);
int ans = 0, t = 0;
for (int i = 1; i <= cnt; i++) {
int x = p[i];
if (num[x] + t == n - pos[x]) {
ans++;
t++;
for (int i = head[x]; i; i = e[i].next) {
num[e[i].go]--;
}
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn];
map<pair<int, int>, int> mp;
int main(void) {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
mp[make_pair(u, v)] = 1;
}
int last = n;
for (int i = n - 1; i >= 1; i--) {
if (mp[make_pair(a[i], a[last])]) {
int j;
for (j = i; j < last; j++) {
if (mp[make_pair(a[j], a[j + 1])])
swap(a[j], a[j + 1]);
else
break;
}
if (j == last) last--;
}
}
printf("%d\n", n - last);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};
long long poww(long long a, long long b) {
if (b == 0) return 1;
long long x = poww(a, b / 2);
x = x * x;
if (b % 2 == 1) x = x * a;
return x;
}
long long bigmod(long long a, long long b, long long c) {
if (b == 0) return 1 % c;
long long x = bigmod(a, b / 2, c);
x = x * x % c;
if (b % 2 == 1) x = x * a % c;
return x;
}
long long mod_inverse(long long a, long long mod) {
return bigmod(a, mod - 2, mod);
}
const int M = 100005 * 3;
int idx[M], a[M];
set<int> adj[M], adjj[M];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
idx[a[i]] = i;
}
for (int i = 0; i < (m); i++) {
int u, v;
cin >> u >> v;
adj[u].insert(idx[v]);
adjj[v].insert(u);
}
int res = 0;
for (int i = n - 1; i >= 1; i--) {
int x = a[i];
int cnt = n - i - res;
for (auto y : adj[x]) {
if (y > i) cnt--;
}
if (cnt == 0) {
res++;
for (auto y : adjj[x]) {
adj[y].erase(adj[y].find(idx[x]));
}
}
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300100;
int n, m;
vector<int> G[maxn];
int vis[maxn];
int pi[maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &pi[i]);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
vis[pi[n]] = 1;
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
int cnt = 0;
for (int j = 0; j < G[pi[i]].size(); j++) {
int v = G[pi[i]][j];
if (vis[v] == 1) cnt++;
}
if (cnt + ans + i == n)
ans++;
else
vis[pi[i]] = 1;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, m;
scanf("%d %d", &n, &m);
map<int, int> M{};
int cnt = 0;
for (int i = 0; i < (n); i++) {
int p;
scanf("%d", &p);
M[p] = cnt++;
}
vector<set<int>> N(n, set<int>{});
for (int i = 0; i < (m); i++) {
int u, v;
scanf("%d %d", &u, &v);
u = M[u];
v = M[v];
if (u < v) {
N[v].insert(u);
}
}
vector<int> P(n);
int p = n - 1;
int trSum = 0;
int res = 0;
while (true) {
if (p < 1) {
break;
}
trSum++;
for (int neigh : N[p]) {
P[neigh]++;
}
p--;
while (p >= 0 && P[p] == trSum) {
res++;
p--;
}
}
printf("%d", res);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int INF = 1e9;
const long long mod = 1e9 + 7;
vector<int> v[N];
bool memo[N];
int cnt[N], ans, arr[N], n, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
v[b].push_back(a);
}
int now = n;
int person = arr[n];
for (auto isi : v[person]) {
memo[isi] = true;
}
for (int i = n; i >= 1; i--) {
if (memo[arr[i]]) {
if (cnt[arr[i]] == now - i) {
ans++;
now--;
continue;
}
}
for (auto isi : v[arr[i]]) {
cnt[isi]++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int pos[maxn], ans[maxn], a[maxn];
vector<int> e[maxn];
int main() {
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
e[r].push_back(l);
}
for (int j = 0; j < e[a[n]].size(); j++) {
int v = e[a[n]][j];
ans[v]++;
}
int now = n;
for (int i = n - 1; i >= 1; i--) {
if (i + ans[a[i]] == now) {
now--;
continue;
}
for (int j = 0; j < e[a[i]].size(); j++) {
int v = e[a[i]][j];
if (pos[v] < pos[a[i]]) {
ans[v]++;
}
}
}
cout << n - now << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int siz = 5e5 + 5;
int nex[siz], to[siz], head[siz], t[siz], cnt, n, m;
set<pair<int, int> > s;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
for (int i = 1, a, b; i <= m; i++) {
scanf("%d%d", &a, &b);
s.insert(make_pair(a, b));
}
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
int k = i;
while (k < n - ans && s.count(make_pair(t[k], t[k + 1]))) {
swap(t[k], t[k + 1]);
k++;
}
if (k == n - ans) ans++;
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
long long int modpow(long long int a, long long int b, long long int mod) {
long long int res = 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a;
res %= mod;
a = a * a;
a %= mod;
}
return res;
}
long long int p(long long int a, long long int b) {
long long int res = 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a;
a = a * a;
}
return res;
}
void testcases() {
long long int n, k;
cin >> n >> k;
vector<long long int> a(n);
for (long long int i = (long long int)0; i < (long long int)n; ++i)
cin >> a[i];
set<pair<long long int, long long int>> meme;
for (long long int i = (long long int)0; i < (long long int)k; ++i) {
long long int x;
long long int y;
cin >> x >> y;
meme.insert({x, y});
}
set<long long int> hold;
long long int ans = 0;
for (long long int i = (long long int)n - 2; i >= (long long int)0; --i) {
if (meme.find({a[i], a[n - 1]}) == meme.end()) {
hold.insert(a[i]);
continue;
} else {
long long int flag = 1;
for (long long int j : hold) {
if (meme.find({a[i], j}) == meme.end()) {
flag = 0;
break;
}
}
if (!flag) {
hold.insert(a[i]);
continue;
}
ans++;
}
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
t = 1;
while (t--) testcases();
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> s, temp;
set<int>::iterator it;
vector<int> v[300011];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int i, n, m, a1, a2, c = 0, j, num;
cin >> n >> m;
long long int a[n + 1];
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 0; i < m; i++) {
cin >> a1 >> a2;
v[a2].push_back(a1);
if (a2 == a[n]) s.insert(a1);
}
j = n - 1;
while (s.size() != 0 && j > 0) {
if (s.find(a[j]) != s.end()) {
s.erase(a[j]);
c++;
j--;
continue;
}
temp = s;
num = a[j];
for (i = 0; i < v[num].size(); i++) {
if (temp.find(v[num][i]) != temp.end()) temp.erase(v[num][i]);
}
for (it = temp.begin(); it != temp.end(); it++) {
s.erase(*it);
}
j--;
}
cout << c;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp1;
int a[300005];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
mp1[make_pair(u, v)] = 1;
}
int last = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (mp1[make_pair(a[i], a[last])]) {
int j;
for (j = i; j < last; ++j) {
if (mp1[make_pair(a[j], a[j + 1])]) {
swap(a[j], a[j + 1]);
} else
break;
}
if (j == last) {
--last;
}
}
}
printf("%d\n", n - 1 - last);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[3000001], pos[3000003], dp[3000003];
long long x[3000001], y[3000003];
vector<vector<long long> > v;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
v.resize(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
for (long long i = 0; i < m; i++) {
cin >> x[i] >> y[i];
x[i] = pos[x[i]];
y[i] = pos[y[i]];
if (y[i] > x[i]) v[y[i]].push_back(x[i]);
}
long long ans = 0, kol = 0;
for (long long i = n - 1; i >= 0; i--) {
if (dp[i] == kol && i != n - 1) {
ans++;
} else {
kol++;
for (auto u : v[i])
if (u < i) dp[u]++;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * (1e5 + 1);
int n, m, p[MAX], u[MAX], v[MAX];
int marked[MAX];
vector<int> adj[MAX];
set<int> tb;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
adj[y].push_back(x);
}
memset(marked, 0, sizeof(marked));
int t = p[n - 1], ans = 0;
for (int i : adj[t]) tb.insert(i);
for (int i = n - 2; i >= 0; i--) {
if (tb.find(p[i]) != tb.end())
if (marked[p[i]] == n - 2 - i - ans) {
ans++;
continue;
}
for (int j : adj[p[i]]) marked[j]++;
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> pos(n + 1), order(n + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &pos[i]);
order[pos[i]] = i;
}
vector<set<int>> G(n + 1);
vector<int> take;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
G[u].insert(v);
if (v == pos[n]) take.push_back(order[u]);
}
sort(begin(take), end(take));
int to = n;
int ans = 0;
while (not take.empty()) {
int cur = take.back();
take.pop_back();
while (cur < to and G[pos[cur]].count(pos[cur + 1])) {
swap(pos[cur], pos[cur + 1]);
cur++;
}
if (cur == to) to--, ans++;
}
printf("%d\n", ans);
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<set<int>> g;
int main() {
int n, m;
cin >> n >> m;
g.resize(n);
vector<int> num(n);
for (int i = 0; i < n; i++) {
int cur;
cin >> cur;
num[cur - 1] = i;
}
vector<int> sueta;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
g[num[u - 1]].insert(num[v - 1]);
}
set<int> cur = g[n - 1];
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
auto it = cur.find(i);
if (it != cur.end())
ans++;
else {
std::vector<int> s3;
set_intersection(cur.begin(), cur.end(), g[i].begin(), g[i].end(),
std::back_inserter(s3));
cur.clear();
for (auto x : s3) cur.insert(x);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<vector<int>> vs(n + 1);
vector<int> perm(n + 1), test(n + 1);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
perm[x] = i;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u = perm[u];
v = perm[v];
vs[v].push_back(u);
}
for (int i = 1; i <= n; i++) sort(vs[i].begin(), vs[i].end());
int cnt = 0;
vector<int> cur(vs[n].begin(), vs[n].end());
for (int i = n - 1; i >= 1; i--) {
if (binary_search(cur.begin(), cur.end(), i))
cnt += 1;
else {
vector<int> temp;
for (auto e : cur)
if (binary_search(vs[i].begin(), vs[i].end(), e)) temp.push_back(e);
cur = vector<int>(temp.begin(), temp.end());
}
}
cout << cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long mincnst = 1e18 + 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long i, j;
long long n, m, u, v;
cin >> n >> m;
long long p[n];
for (i = 0; i < n; i++) cin >> p[i];
long long ans = 0;
set<long long> st;
vector<set<long long>> adj(1000005);
for (i = 0; i < m; i++) {
cin >> u >> v;
adj[u].insert(v);
}
long long flg = 0;
st.insert(p[n - 1]);
for (i = n - 2; i > -1; i--) {
flg = 0;
if (!adj[p[i]].size()) {
flg = 1;
} else {
for (auto it : st) {
if (!adj[p[i]].count(it)) {
flg = 1;
break;
}
}
}
if (flg)
st.insert(p[i]);
else {
ans++;
}
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 3e5 + 5;
int n, m, q[sz] = {0};
vector<int> sw[sz];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> q[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
sw[u].push_back(v);
}
int steps = 0;
set<int> s;
s.insert(q[n - 1]);
for (int i = n - 2; i >= 0; i--) {
int u = q[i], cnt = 0;
for (int j = 0; j < sw[u].size(); j++)
if (s.find(sw[u][j]) != s.end()) cnt++;
if (cnt == s.size())
steps++;
else
s.insert(u);
}
cout << steps << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> idtP(n);
vector<int> pIdt(n);
for (int I = 0; I < n; I++) {
cin >> idtP[I];
idtP[I]--;
pIdt[idtP[I]] = I;
}
vector<set<int> > volta(n);
vector<set<int> > avanca(n);
set<pair<int, int> > ultimoViz;
int u, v;
for (int I = 0; I < m; I++) {
cin >> u >> v;
u--;
v--;
volta[u].insert(v);
avanca[v].insert(u);
if (v == idtP[n - 1]) {
ultimoViz.insert({{-pIdt[u], u}});
}
}
for (pair<int, int> p : ultimoViz) {
int el = p.second;
while (pIdt[el] + 1 != n - 1) {
if (volta[el].find(idtP[pIdt[el] + 1]) != volta[el].end()) {
int el2 = idtP[pIdt[el] + 1];
swap(idtP[pIdt[el]], idtP[pIdt[el] + 1]);
swap(pIdt[el], pIdt[el2]);
} else {
break;
}
}
}
int cont = 0;
int p = n - 2;
while (true) {
if (avanca[idtP[n - 1]].find(idtP[p]) != avanca[idtP[n - 1]].end()) {
cont++;
p--;
} else {
break;
}
}
cout << cont << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.precision(12);
cout.setf(ios_base::fixed, ios_base::floatfield);
int n, m, p[300000];
multimap<int, int> q;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
q.emplace(u - 1, v - 1);
}
unordered_set<int> rm;
rm.insert(p[n - 1] - 1);
int rmc = 1, ans = 0;
for (int i = n - 2; i >= 0; i--) {
auto er = q.equal_range(p[i] - 1);
int ht = rmc;
for (auto it = er.first; it != er.second; ++it) {
if (rm.count(it->second)) ht--;
}
if (ht == 0) {
ans++;
} else {
rmc++;
rm.insert(p[i] - 1);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
vector<int> ar;
vector<int> gr[300005];
int unab[300005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
ar.resize(n);
for (int i = 0; i < n; i++) cin >> ar[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
gr[u].push_back(v);
}
int ans = 0;
unab[ar[n - 1]] = 1;
for (int i = n - 2; i >= 0; i--) {
int cnt = 0;
for (int j = 0; j < (int)(gr[ar[i]].size()); j++) {
if (unab[gr[ar[i]][j]] == 1) cnt++;
}
if (cnt + ans == n - 1 - i)
ans++;
else
unab[ar[i]] = 1;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> v[n + 1];
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
v[x].push_back(y);
}
set<int> s;
s.insert(a[n - 1]);
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
int cnt = 0;
for (int j = 0; j < v[a[i]].size(); j++) {
if (s.find(v[a[i]][j]) != s.end()) {
cnt++;
}
}
if (cnt == s.size()) {
ans++;
} else {
s.insert(a[i]);
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> p;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
p.push_back(temp);
}
vector<int> edges[n + 1];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
edges[u].push_back(v);
}
set<int, greater<int> > s;
int ans = 0;
s.insert(p[n - 1]);
for (int i = n - 2; i > -1; i--) {
bool flag = false;
for (auto j = s.begin(); j != s.end(); j++) {
if (find(edges[p[i]].begin(), edges[p[i]].end(), *j) ==
edges[p[i]].end()) {
flag = true;
break;
}
}
if (flag) {
s.insert(p[i]);
} else {
ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 500007;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 7;
long long n, m, ans = 0;
long long seat[MAXN] = {};
set<long long> can;
vector<long long> adj[MAXN] = {};
int main(void) {
cin >> n >> m;
for (long long i0 = 0; i0 < n; i0++) {
cin >> seat[n - i0 - 1];
}
for (long long i0 = 0; i0 < m; i0++) {
long long x, y;
cin >> x >> y;
adj[x].push_back(y);
}
long long need = 1;
can.insert(seat[0]);
for (long long i0 = 1; i0 < n; i0++) {
long long tot = 0;
long long num_Bro = adj[seat[i0]].size();
for (long long i = 0; i < num_Bro; i++) {
long long bro = adj[seat[i0]][i];
if (can.find(bro) != can.end()) tot++;
}
if (tot == need)
ans++;
else {
need++;
can.insert(seat[i0]);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp1;
int a[300005];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
mp1[make_pair(u, v)] = 1;
}
int last = n;
for (int i = n - 1; i >= 1; i--) {
if (mp1[make_pair(a[i], a[last])]) {
int j;
for (j = i; j < last; ++j) {
if (mp1[make_pair(a[j], a[j + 1])]) {
swap(a[j], a[j + 1]);
} else
break;
}
if (j == last) {
--last;
}
}
}
printf("%d\n", n - last);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
scanf("%d", &n);
scanf("%d", &m);
vector<int> p(n), pos(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
p[i]--;
pos[p[i]] = i;
}
vector<set<int>> g(n);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
g[u].insert(v);
}
int tot = 1, sol = 0;
vector<int> moved(n);
for (int i = n - 2; i >= 0; --i) {
int u = p[i];
int cnt = 0;
for (auto v : g[u]) {
if (pos[v] <= pos[u]) continue;
if (moved[v]) continue;
cnt++;
}
if (cnt == tot) {
moved[u] = 1;
sol++;
} else
tot++;
}
cout << sol << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, A[500007], pointer;
vector<long long> V[500007];
set<long long> S, F, RF;
int main() {
scanf("%lld %lld\n", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &A[i]);
}
scanf("\n");
for (int i = 1; i <= m; i++) {
long long x, y;
scanf("%lld %lld\n", &x, &y);
if (y == A[n]) {
S.insert(x);
}
V[x].push_back(y);
}
pointer = n;
long long res = 0;
for (int i = n - 1; i >= 1; i--) {
if (S.count(A[i]) != 0) {
RF = F;
int flag = 0;
for (int j = 0; j < V[A[i]].size(); j++) {
long long k = V[A[i]][j];
if (F.count(k) != 0) {
F.erase(k);
}
if (k == A[n]) {
flag = 1;
}
}
if (F.size() > 0 || flag == 0) {
RF.insert(A[i]);
} else {
res++;
}
F = RF;
} else {
F.insert(A[i]);
}
}
link:
printf("%lld\n", res);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> a(n), r(n + 1), f(n + 1);
for (long long i = 0; i < n; i++) cin >> a[i];
vector<vector<long long>> w(n + 1);
for (long long i = 0; i < m; i++) {
long long g, q;
cin >> g >> q;
w[q].push_back(g);
if (q == a.back()) f[g] = 1;
}
long long kf = 0, knf = 0;
for (long long i = n - 2; i >= 0; i--) {
if (f[a[i]] && r[a[i]] == knf)
kf++;
else {
for (long long g : w[a[i]]) r[g]++;
knf++;
}
}
cout << (kf) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
long long a[n + 2];
map<long long, long long> pam2;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
pam2[a[i]] = i;
}
if (m == 0) {
cout << 0;
return 0;
}
long long u, v;
map<long long, vector<long long> > pam;
long long s = a[n];
vector<long long> v1;
for (long long i = 1; i <= m; i++) {
cin >> u >> v;
if (v == s) v1.push_back(u);
pam[u].push_back(v);
}
if (v1.size() == 0) {
cout << 0;
return 0;
}
vector<pair<long long, long long> > b;
for (long long i = 0; i < v1.size(); i++) {
b.push_back({pam2[v1[i]], v1[i]});
}
sort(b.begin(), b.end());
long long Ans = n;
long long ans = 0;
for (long long i = b.size() - 1; i >= 0; i--) {
long long p = b[i].second;
vector<long long> t = pam[p];
long long cnt = 0;
for (long long j = 0; j < t.size(); j++) {
long long idx = pam2[t[j]];
if (idx > b[i].first and idx < Ans) {
cnt++;
}
}
if (cnt == Ans - b[i].first - 1 - ans) {
ans++;
pam2[b[i].second] = n + 1;
}
pam[p].clear();
t.clear();
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
void du(int& d) {
char t = getchar();
bool Mark = false;
for (; t < '0' || t > '9'; t = getchar())
if (t == '-') Mark = !Mark;
for (d = 0; t >= '0' && t <= '9'; t = getchar()) d = d * 10 + t - '0';
if (Mark) d = -d;
}
const int maxn = 1e6 + 100;
int pup[maxn];
int re[maxn];
set<int> Q[maxn];
int main() {
int n, m;
du(n);
du(m);
int i;
for (i = 1; i <= n; i++) du(pup[i]);
int u, v;
for (i = 1; i <= m; i++) {
du(u);
du(v);
Q[v].insert(u);
}
vector<int> sheet;
for (i = n - 1; i >= 1; i--) {
bool mark;
mark = Q[pup[n]].count(pup[i]);
if (mark) {
for (auto& v : sheet) {
mark = mark && Q[v].count(pup[i]);
}
}
if (!mark) sheet.push_back(pup[i]);
}
int ans = n - 1 - sheet.size();
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int n, m;
int p[MAXN];
set<pair<int, int> > G;
void init() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G.insert({u, v});
}
}
void solve() {
int ans = 0;
for (int i = n - 1; i >= 1; i--)
if (G.count({p[i], p[n]})) {
int j = i;
while (j < n - 1 && G.count({p[j], p[j + 1]})) {
swap(p[j], p[j + 1]);
j++;
}
if (G.count({p[n - 1], p[n]})) {
swap(p[n - 1], p[n]);
n--;
ans++;
}
}
cout << ans << '\n';
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
init();
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[3 * 100001];
int get(int a, int b) {
if (binary_search((v[a]).begin(), (v[a]).end(), b)) {
return 1;
} else {
return 0;
}
}
bool cheak[3 * 100001];
int main() {
int n, m;
cin >> n >> m;
int A[n + 1];
for (int i = 1; i < n + 1; i++) scanf("%d", &A[i]);
vector<int> vv;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
if (b == A[n]) {
cheak[a] = true;
}
}
for (int i = 0; i < 3 * 100001; i++) {
if (v[i].size()) sort((v[i]).begin(), (v[i]).end());
}
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
if (cheak[A[i]]) {
int t = 0;
for (int j : vv) {
if (!get(A[i], j)) {
t = 1;
vv.push_back(A[i]);
break;
}
}
if (t) continue;
ans++;
} else {
vv.push_back(A[i]);
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, c, d;
struct node {
int a, b;
};
bool cmp(node x, node y) {
if (x.a == y.a) return x.b > y.b;
return x.a > y.a;
}
string s;
int num[30];
char cr, cr1;
int check(char cc) {
for (int i = cc - 'a' + 1; i < 26; ++i) {
if (num[i]) return num[i];
}
return 0;
}
int main() {
cin >> n;
getchar();
while ((cr = getchar()) && cr != '\n') {
m++;
if (!num[cr - 'a']) {
num[cr - 'a'] = m;
}
if (check(cr)) {
cout << "YES" << endl;
cout << check(cr) << " " << m << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string str;
cin >> str;
bool ok = false;
int l = -1, r = -1;
for (int i = 1; i < str.length(); i++) {
if (str[i] < str[i - 1]) {
l = i, r = i + 1;
char ch = str[i];
str[i] = str[i - 1];
str[i - 1] = ch;
ok = true;
break;
}
}
if (ok) {
cout << "YES\n";
cout << l << " " << r << "\n";
} else
cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long n;
string s;
cin >> n >> s;
int f = 0;
long i;
for (i = 0; i < n; i++) {
if (s[i] < s[i - 1]) {
f = 1;
break;
}
}
if (f == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << i << " " << i + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double esp = 1e-7;
const double pi = acos(-1.0);
const int maxx = 1101000;
const int mod = int(1e9 + 7);
namespace fastIO {
bool IOerror = 0;
inline char nc() {
static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, 100000, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
;
}
inline void readll(long long int &x) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
;
}
inline void reads(char *s) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
s[0] = ch;
for (int i = 1; (!blank(ch = nc())); ++i) {
s[i] = ch;
}
}
}; // namespace fastIO
string s;
int main() {
int n;
scanf("%d", &n);
cin >> s;
int l = 0, r = 0;
bool ans = true;
for (int i = 0; i < n; ++i) {
if (i) {
if (s[i] < s[i - 1]) {
ans = false;
l = i - 1;
r = i;
break;
}
}
}
if (ans) {
cout << "NO\n";
} else {
cout << "YES\n" << l + 1 << " " << r + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char str[300005];
int main() {
int n;
scanf("%d", &n);
scanf("%s", str);
int flag = 0, pos = 1;
for (int i = 0; i < n - 1; i++) {
if (str[i + 1] >= str[i])
flag++;
else
pos = i;
}
if (flag != n - 1) {
printf("YES\n");
printf("%d %d\n", pos + 1, pos + 2);
} else
printf("NO\n");
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
int isPali(string str) {
string u;
for (int i = str.length() - 1; i >= 0; i--) {
u += str[i];
}
if (u == str) {
return 1;
}
return 0;
}
long long int gcd(long long int a, long long int b) {
if (b != 0) {
return gcd(b, a % b);
} else {
return a;
}
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
bool checkPrimeNumber(int n) {
bool isPrime = true;
if (n == 0 || n == 1) {
isPrime = false;
} else {
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
bool isPerfectSquare(long double x) {
if (x >= 0) {
long long sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
long long int cel(long long int a, long long int b) { return (a + b - 1) / b; }
long long int fib(long long int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
long long int modFact(long long int n, long long int p) {
if (n >= p) return 0;
long long int result = 1;
for (long long int i = 1; i <= n; i++) result = (result * i) % p;
return result;
}
long long int digSum(long long int n) {
long long int sum = 0;
while (n != 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
bool CompFloat(float a, float b) {
if (abs(a - b) < 1e-9) {
return true;
}
return false;
}
bool isPowerOfTwo(int n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
long long int smallest_divisor(long long int n) {
long long int i;
for (i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
return i;
}
}
return n;
}
bool isFibonacci(int n) {
return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4);
}
long long int power(long long int x, long long int y, long long int p) {
int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool sortbysec(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
if (a.second == b.second) {
return a.first > b.first;
}
return (a.second > b.second);
}
int32_t mod_inv(int32_t a, int32_t m) {
int32_t g = m, r = a, x = 0, y = 1;
while (r != 0) {
int32_t q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
long long int DigLeng(long long int n) { return (long long int)log10(n) + 1; }
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; i++) {
if (s[i - 1] > s[i]) {
cout << "YES" << endl;
cout << i << " " << i + 1 << endl;
return;
}
}
cout << "NO" << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int br[26];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, i, j;
string s;
cin >> n >> s;
for (i = n - 1; i >= 0; i--) {
br[s[i] - 'a'] = i;
for (j = s[i] - 'a' - 1; j >= 0; j--)
if (br[j]) {
cout << "YES\n" << i + 1 << " " << br[j] + 1 << '\n';
return 0;
}
}
cout << "NO\n";
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long int read() {
char c = getchar();
long long int num, sign = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (num = 0; c >= '0' && c <= '9';) {
c -= '0';
num = num * 10 + c;
c = getchar();
}
return num * sign;
}
inline long long int pow(long long int b, long long int p) {
long long int ans = 1;
while (p > 0) ans = ans << b, p--;
return ans;
}
inline bool isPrime(long long int n) {
if (!(n % 2)) return false;
for (long long int i = 3; i <= sqrt(n); i += 2)
if (!(n % i)) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string str;
cin >> n >> str;
for (int i = 0; i < n - 1; i++) {
if (str[i] > str[i + 1]) {
cout << "YES" << endl;
cout << i + 1 << " " << i + 2;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
string s;
cin >> s;
int flag = 0;
long long int pos;
for (long long int i = 1; i < n; i++) {
if (s[i] < s[i - 1]) {
flag = 1;
pos = i;
break;
}
}
if (flag == 1) {
cout << "YES\n";
cout << pos << " " << pos + 1;
} else {
cout << "NO";
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d ", &n);
int i, position = -1;
char s[n];
for (i = 0; i < n; i++) {
scanf("%c", &s[i]);
}
for (i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
position = i;
break;
}
}
if (position == -1) {
printf("NO");
} else {
printf("YES\n%d %d", position + 1, position + 2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long INF = 1 << 28;
const double pi = acos(-1.0);
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int knight[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
const long double EPS = 1e-7;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool cmp(int a, int b) { return a < b; }
int on(int mask, int st) { return mask | (1 << st); }
int off(int mask, int st) { return mask & (~(1 << st)); }
bool check(int mask, int st) { return mask & (1 << st); }
long long inf = 1e12;
long long MOD = 1e9 + 7;
long long mod = 1e9 + 7;
int main() {
int n;
scanf("%d", &n);
string s;
cin >> s;
int f = 1;
for (int i = 1; i < n; i++) {
if (s[i - 1] > s[i]) {
cout << "YES" << endl;
cout << i << " " << i + 1 << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d, e, g, h, i, j, k, l, m, n;
cin >> a;
string s;
cin >> s;
string S = s;
sort(s.begin(), s.end());
if (s == S) {
cout << "NO";
} else {
cout << "YES"
<< "\n";
char ans;
int J, j;
for (i = 0; i < a - 1; i++) {
if (S[i] > S[i + 1]) {
cout << i + 1 << " " << i + 2;
break;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5;
const int INF_ = 0x3f3f3f3f;
void init() {}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
char str[MAX_N + 10];
scanf("%d%s", &n, &str);
bool flag = false;
for (int i = 0; i + 1 < n; i++) {
if (str[i] > str[i + 1]) {
printf("YES\n");
printf("%d %d\n", i + 1, i + 2);
flag = true;
break;
}
}
if (!flag) printf("NO\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; ++i) {
if (s[i] < s[i - 1]) {
cout << "YES\n" << i << ' ' << i + 1;
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
string a = s;
sort(a.begin(), a.end());
if (a == s) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = n - 2; i >= 0; i--) {
if (s[i] > s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a, b;
bool flag = 0;
string s;
cin >> n;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
a = i;
b = i + 1;
flag = 1;
break;
}
}
if (flag == 0)
cout << "NO" << endl;
else
cout << "YES" << endl << a + 1 << ' ' << b + 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<char> l;
vector<char> r;
void solve() {
l[0] = s[0];
for (int i = 1; i < s.size(); i++) {
l[i] = max(l[i - 1], s[i]);
}
r.back() = s.back();
for (int i = s.size() - 2; i >= 0; i--) {
r[i] = min(r[i + 1], s[i]);
}
int a, b;
bool ok = false;
for (int i = 0; i < s.size() - 1; i++) {
char u = l[i];
char v = r[i + 1];
if (u > v) {
a = l[i];
b = r[i + 1];
ok = true;
break;
}
}
if (!ok) {
cout << "NO";
return;
};
int posa, posb;
for (int i = 0; i < s.size(); i++)
if (s[i] == a) {
posa = i;
break;
}
for (int i = s.size() - 1; i >= 0; i--)
if (s[i] == b) {
posb = i;
break;
}
cout << "YES\n" << ++posa << ' ' << ++posb;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
l.assign(n, 'a');
r.assign(n, 'a');
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, n, t, b, c, d, i, j, k, x, y, z, l, q, r;
string s;
while (cin >> k >> s) {
string s1, s2;
long long cnt = 0, cn = 1;
n = s.size();
for (i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1] && s[i] > s[i + 1]) {
m = i;
break;
} else
cn++;
}
if (cn == n)
cnt = 0;
else
for (i = m; i < n - 1; i++)
if (s[i] > s[i + 1]) {
cnt = 1, x = i + 1;
break;
}
if (cnt == 1) {
for (i = m; i <= x; i++) s1 += s[i];
s2 = s1;
reverse(s2.begin(), s2.end());
}
if (s1 != s2 && cnt == 1)
cout << "YES" << endl << x << " " << x + 1 << endl;
else
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, r;
string s;
cin >> n >> s;
string m = "zz";
for (int i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
string h = s.substr(i, i + 1);
if (h < m) {
m = h;
l = i + 1;
r = i + 2;
}
}
}
if (m == "zz") {
cout << "NO\n";
return 0;
}
cout << "YES\n" << l << " " << r << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, r, Min;
char s[300010];
int main() {
cin >> n;
scanf("%s", s + 1);
r = n;
Min = s[n];
for (long long l = n - 1; l >= 1; l--) {
if (s[l] < Min) {
Min = s[l];
r = l;
} else if (s[l] > Min) {
cout << "YES" << endl << l << ' ' << r << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string str;
cin >> n >> str;
char ch = str[0];
for (int i = 1; i < n; i++) {
if (str[i] < ch) {
cout << "YES" << endl;
for (int j = 0; j < n; j++) {
if (str[i] < str[j]) {
cout << j + 1 << " " << i + 1 << endl;
exit(0);
}
}
}
ch = max(ch, str[i]);
}
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n - 1; ++i) {
if (s[i] > s[i + 1]) {
cout << "YES\n" << i + 1 << " " << i + 2;
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
string s;
cin >> s;
long long l, r;
long long f = 0;
for (long long i = 1; i < n; i++) {
if (s[i] < s[i - 1]) {
l = i;
r = i + 1;
f = 1;
break;
}
}
if (f == 0) {
cout << "NO"
<< "\n";
} else {
cout << "YES\n";
cout << l << " " << r << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, MAX = 200001, INF = 1 << 30;
int main() {
int N;
cin >> N;
string S;
cin >> S;
bool ok = true;
for (int i = 1; i < N; i++) {
if (S[i - 1] > S[i]) {
cout << "YES" << endl;
cout << i << " " << i + 1 << endl;
ok = false;
}
if (!ok) break;
}
if (ok) cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[300005];
int c[300005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, b, x = 0, y, z, i, p, q;
cin >> n;
int cc[2];
char s[300005];
cin >> s;
for (i = 1; i < n; i++) {
if (s[i] < s[i - 1]) {
cout << "YES" << endl;
cout << i << " " << i + 1 << endl;
return 0;
}
}
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dxk[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyk[] = {-1, 1, -2, 2, -2, 2, -1, 1};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
string s;
cin >> n >> s;
string tmp = s;
sort(s.begin(), s.end());
if (s == tmp) {
cout << "NO\n";
} else {
cout << "YES\n";
for (int i = 0; i < n; i++) {
if (s[i] != tmp[i]) {
for (int j = i; j < n; j++) {
if (tmp[j] == s[i]) {
cout << i + 1 << " " << j + 1 << '\n';
return 0;
}
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int k = 0; k < n - 1; k++) {
if (s[k] > s[k + 1]) {
cout << "YES" << endl;
cout << k + 1 << " " << k + 2;
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = n - 2; i >= 0; i--) {
if (s[i] > s[i + 1]) {
cout << "YES" << endl;
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
}
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
char s[300005];
int n;
int ind[200];
int main() {
scanf("%d", &n);
scanf("%s", s);
for (int i = n - 1; i >= 0; i--) {
for (char c = 'a'; c <= 'z'; c++) {
if (c < s[i]) {
if (ind[(int)c]) {
return printf("YES\n%d %d", i + 1, ind[(int)c] + 1), 0;
}
}
}
ind[(int)s[i]] = i;
}
printf("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
string str;
scanf("%d", &n);
getchar();
getline(cin, str);
for (int i = 1; i < str.length(); i++) {
if (str[i] < str[i - 1]) {
printf("YES\n");
printf("%d %d", i, i + 1);
return 0;
}
}
printf("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; i++) {
if (s[i] < s[i - 1]) {
cout << "YES\n";
cout << i << " " << i + 1 << endl;
return 0;
}
}
cout << "NO\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin >> n >> s;
for (int i = 0; i + 1 < n; i++) {
if (s[i] > s[i + 1]) {
cout << "YES" << endl << i + 1 << " " << i + 2;
return 0;
}
}
cout << "NO";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
int a[26];
fill(a, a + 26, -1);
for (int i = 0; i < n; i++) {
a[s[i] - 'a'] = i + 1;
}
int l = -1, r = -1;
for (int i = 0; i < n; i++) {
int j_upto = s[i] - 'a';
for (int j = 0; j < j_upto; j++) {
if (a[j] != -1 && a[j] > i + 1) {
l = i + 1, r = a[j];
}
}
}
if (l != -1) {
cout << "YES\n" << l << " " << r << '\n';
} else
cout << "NO\n";
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, e;
int main() {
long long int n, a;
bool flag = false;
string s;
cin >> n >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i + 1] < s[i]) {
cout << "YES" << endl;
cout << i + 1 << " " << i + 2;
flag = true;
break;
}
}
if (!flag) cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; i++) {
if (s[i] < s[i - 1]) {
cout << "YES" << '\n';
cout << i << " " << i + 1 << '\n';
return 0;
}
}
cout << "NO" << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
char c[maxn];
int main() {
int n, ma = -1, pos;
scanf("%d%s", &n, c + 1);
for (int i = 1; i <= n; i++) {
if (c[i] - 'a' > ma) {
ma = c[i] - 'a';
pos = i;
} else if (c[i] - 'a' < ma) {
printf("YES\n");
printf("%d %d", pos, i);
return 0;
}
}
printf("NO");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
cout << "YES" << endl;
cout << i + 1 << " " << i + 2;
return 0;
}
}
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0;
char c = getchar();
int q = 1;
while (c < '0' || c > '9') {
if (c == '-') q = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
x = x * q;
return x;
}
void out(int x) {
if (x < 0) x = -x, putchar('-');
if (x >= 10) out(x / 10);
putchar(x % 10 + '0');
return;
}
int n;
string s;
int main() {
n = read();
cin >> s;
for (int i = 0; i <= n - 2; i++) {
if (s[i] > s[i + 1]) {
cout << "YES\n";
cout << i + 1 << " " << i + 2;
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int N = 200001;
const int oo = 1e9;
const double pi = acos(-1);
int main() {
int n;
string s;
cin >> n >> s;
bool can = false;
for (int i = 0; i < n - 1; ++i)
if (s[i] > s[i + 1]) can = true;
if (!can) {
puts("NO");
return 0;
}
int a, b;
char c = 'a';
for (int i = 0; i < n; ++i) {
if (s[i] > c) {
c = s[i];
a = i;
}
if (s[i] < c) {
b = i;
break;
}
}
puts("YES");
cout << a + 1 << " " << b + 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (int(s[i] > s[i + 1])) {
cout << "YES" << endl << i + 1 << ' ' << i + 2;
return 0;
}
}
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
string s;
cin >> n >> s;
long long i, j;
char k = 000;
for (i = 0; i < n; i++) {
if (s[i] < k) {
cout << "YES\n";
cout << j + 1 << " " << i + 1;
return 0;
}
k = max(s[i], k);
if (s[i] == k) j = i;
}
cout << "NO";
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.