Tutorial is loading...
Python Solution
T = int(input())
for tc in range(T):
s = [c for c in input()]
n = len(s)
i = 0
while (i < n):
if (s[i] == '?'):
prv = 'd' if i == 0 else s[i - 1]
nxt = 'e' if i + 1 >= n else s[i + 1]
for x in ['a', 'b', 'c']:
if (x != prv) and (x != nxt):
s[i] = x
break
else:
i += 1
ok = True
for i in range(n - 1):
if (s[i] == s[i + 1]):
print("-1")
ok = False
break
if (ok == True):
print("".join(s))
Author: isaf27, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ solution
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 239;
int n, p[M], x;
void solve()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
p[x - 1] = i;
}
int l = n;
int r = 0;
string ans = "";
for (int i = 0; i < n; i++)
{
l = min(l, p[i]);
r = max(r, p[i]);
if (r - l == i)
ans += '1';
else
ans += '0';
}
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Author: isaf27, prepare isaf27
Tutorial is loading...
C++ solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
int main() {
int t;
cin >> t;
forn(tt, t) {
int n;
cin >> n;
map<int,int> c;
forn(i, n) {
int pi;
cin >> pi;
c[-pi]++;
}
vector<int> pp;
for (auto p: c)
pp.push_back(p.second);
bool ok = false;
int g = pp[0];
int i = 1;
int s = 0;
while (s <= g && i < pp.size())
s += pp[i++];
if (g < s) {
int b = 0;
while (b <= g && i < pp.size())
b += pp[i++];
while (i < pp.size() && g + s + b + pp[i] <= n / 2)
b += pp[i++];
if (g < b && g + s + b <= n / 2) {
ok = true;
cout << g << " " << s << " " << b << endl;
}
}
if (!ok)
cout << 0 << " " << 0 << " " << 0 << endl;
}
}
Author: MikeMirzayanov, prepare MikeMirzayanov
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> hs;
cin >> hs[0] >> hs[1] >> hs[2] >> hs[3];
int total = hs[0] + hs[1] + hs[2] + hs[3];
for (int st = 0; st < 4; st++) if (hs[st]) {
vector<int> res;
auto ths = hs;
ths[st]--;
res.push_back(st);
int last = st;
for (int i = 0; i < total - 1; i++) {
if (ths[last - 1]) {
ths[last - 1]--;
res.push_back(last - 1);
last--;
}
else if (ths[last + 1]) {
ths[last + 1]--;
res.push_back(last + 1);
last++;
}
else {
break;
}
}
if ((int) res.size() == total) {
cout << "YES\n";
for (int i = 0; i < (int) res.size(); i++) {
cout << res[i] << " \n"[i == (int) res.size() - 1];
}
return 0;
}
}
cout << "NO\n";
return 0;
}
Author: laoriu, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
const int MOD = 119 << 23 | 1;
int inv(int a) {
int r = 1, t = a, k = MOD - 2;
while (k) {
if (k & 1) r = (long long) r * t % MOD;
t = (long long) t * t % MOD;
k >>= 1;
}
return r;
}
int main() {
int n; cin >> n;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i], p[i] = (long long) p[i] * inv(100) % MOD;
int a = 1, b = 0;
for (int i = 0; i < n; i++) {
a = (long long) a * inv(p[i]) % MOD;
b = (long long) b * inv(p[i]) % MOD;
a = (a + (long long) (p[i] - 1) * inv(p[i])) % MOD;
b = (b - inv(p[i]) + MOD) % MOD;
}
cout << (long long) (MOD - b) * inv(a) % MOD << "\n";
return 0;
}
Author: chemthan, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
const int MOD = 119 << 23 | 1;
int fpow(int a, int k) {
int r = 1, t = a;
while (k) {
if (k & 1) r = (long long) r * t % MOD;
t = (long long) t * t % MOD;
k >>= 1;
}
return r;
}
int main() {
string s; cin >> s;
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
vector<int> f(n + 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i];
f[i + 1] += s[i] == '?';
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
if (s[i] != '(') {
dp[i][j] += dp[i + 1][j];
dp[i][j] %= MOD;
}
if (s[j] != ')') {
dp[i][j] += dp[i][j - 1];
dp[i][j] %= MOD;
}
if (s[i] != '(' && s[j] != ')') {
dp[i][j] -= dp[i + 1][j - 1];
dp[i][j] += MOD;
dp[i][j] %= MOD;
}
if (s[i] != ')' && s[j] != '(') {
dp[i][j] += dp[i + 1][j - 1];
dp[i][j] %= MOD;
dp[i][j] += fpow(2, f[j] - f[i + 1]);
dp[i][j] %= MOD;
}
}
}
cout << dp[0][n - 1] << "\n";
return 0;
}
Author: chemthan, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
const int MOD = 119 << 23 | 1;
struct node_t {
int prd;
int val;
node_t() {
prd = val = 0;
}
};
node_t operator + (node_t a, node_t b) {
if (!a.prd) return b;
if (!b.prd) return a;
node_t c;
c.prd = (long long) a.prd * b.prd % MOD;
c.val = (long long) a.val * b.prd % MOD;
c.val = (c.val + b.val) % MOD;
return c;
}
int inv(int a) {
int r = 1, t = a, k = MOD - 2;
while (k) {
if (k & 1) r = (long long) r * t % MOD;
t = (long long) t * t % MOD;
k >>= 1;
}
return r;
}
int main() {
int n, q; cin >> n >> q;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i], p[i] = (long long) p[i] * inv(100) % MOD;
vector<node_t> st(n << 2);
auto upd = [&] (int p, node_t val) {
for (st[p += n] = val; 1 < p; ) p >>= 1, st[p] = st[p << 1] + st[p << 1 | 1];
};
auto query = [&] (int l, int r) {
node_t lres, rres;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
lres = lres + st[l++];
}
if (r & 1) {
rres = st[--r] + rres;
}
}
return (lres + rres).val;
};
for (int i = 0; i < n; i++) {
node_t c;
c.val = c.prd = inv(p[i]);
upd(i, c);
}
set<int> ss;
ss.insert(0);
int res = query(0, n - 1);
for (int i = 0; i < q; i++) {
string op; int u; cin >> u; u--;
auto it = ss.lower_bound(u);
if (it == ss.end() || *it != u) {
it = ss.insert(u).first;
int lo = *(--it);
it++;
int hi = n;
if (++it != ss.end()) {
hi = *it;
}
res -= query(lo, hi - 1);
res += MOD;
res %= MOD;
res += query(lo, u - 1);
res %= MOD;
res += query(u, hi - 1);
res %= MOD;
}
else {
int lo = *(--it);
it++;
int hi = n;
if (++it != ss.end()) {
hi = *it;
}
res += query(lo, hi - 1);
res %= MOD;
res -= query(lo, u - 1);
res += MOD;
res %= MOD;
res -= query(u, hi - 1);
res += MOD;
res %= MOD;
ss.erase(u);
}
cout << res << "\n";
}
return 0;
}
Author: chemthan, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
const int MOD = 119 << 23 | 1;
int inv(int a) {
int r = 1, t = a, k = MOD - 2;
while (k) {
if (k & 1) r = (long long) r * t % MOD;
t = (long long) t * t % MOD;
k >>= 1;
}
return r;
}
int main() {
string s; cin >> s;
int k = s.size() + 1;
int a = 0, b = 0, c = 0, d = 0;
for (char e : s) {
if (e == ')') {
b++;
}
if (e == '?') {
d++;
}
}
map<int, vector<int>> dps;
auto calc = [&] (int a, int b, int c, int d) {
int x = b + d - a;
if (x < 0) return 0;
int k = c + d;
if (k < x) x = k;
if (dps.count(k)) return dps[k][x];
auto& dp = dps[k];
dp.resize(k + 1);
int t = 0, w = 1;
for (int i = 0; i <= k; i++) {
t += w;
t %= MOD;
dp[i] = t;
w = (long long) w * (c + d - i) % MOD;
w = (long long) w * inv(i + 1) % MOD;
}
return dp[x];
};
int res = 0;
for (int i = 0; i < (int) s.size(); i++) {
char e = s[i];
if (e == '(') {
a++;
}
if (e == ')') {
b--;
}
if (e == '?') {
c++;
d--;
}
if (e == '(') {
res += calc(a, b, c, d);
res %= MOD;
}
else if (e == '?') {
a++, c--;
res += calc(a, b, c, d);
res %= MOD;
a--, c++;
}
}
cout << res << "\n";
return 0;
}
Author: chemthan, prepare laoriu, I_love_Hoang_Yen
Tutorial is loading...
C++ Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m; cin >> n >> m;
vector<int> d(n);
vector<vector<int>> g(n, vector<int>(n));
vector<vector<int>> f(n, vector<int>(n, 1));
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v; u--, v--;
d[u]++;
g[u][v] = 1, g[v][u] = 2;
f[u][v] = f[v][u] = 0;
}
priority_queue<pair<int, int>> heap;
for (int i = 0; i < n; i++) {
if (d[i] < n - 1) {
heap.push({-d[i], i});
}
}
while (!heap.empty()) {
int u = heap.top().second;
heap.pop();
queue<int> que;
vector<int> vis(n), from(n);
que.push(u), vis[u] = 1;
int id = -1;
while (!que.empty()) {
int v = que.front(); que.pop();
int found = 0;
for (int w = 0; w < n; w++) if (w != v && !g[v][w]) {
found = 1;
break;
}
if (found) {
id = v;
break;
}
for (int w = 0; w < n; w++) if (!vis[w] && g[v][w] == 2 && f[v][w] == 1) {
que.push(w), vis[w] = 1;
from[w] = v;
}
}
if (id == -1) {
continue;
}
for (int v = 0; v < n; v++) if (v != id && !g[id][v]) {
g[id][v] = 1, g[v][id] = 2;
break;
}
while (id != u) {
int nid = from[id];
swap(g[id][nid], g[nid][id]);
id = nid;
}
d[u]++;
if (d[u] < n - 1) {
heap.push({-d[u], u});
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
cout << 0;
}
else {
cout << 2 - g[i][j];
}
}
cout << "\n";
}
return 0;
}
Author: chemthan, prepare laoriu, I_love_Hoang_Yen
Another cool problem: WEICOM.
Tutorial is loading...
Python Solution
n,a,d=map(int,input().split())
print(368131125*a%10**9*12*10**9+1,368131125*d%10**9*12*10**9)
Author: chemthan, prepare laoriu, I_love_Hoang_Yen