Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
l1, r1, l2, r2 = map(int, input().split())
if max(l1, l2) <= min(r1, r2):
print(max(l1, l2))
else:
print(l1 + l2)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n, m = map(int, input().split())
s = []
for j in range(n):
s.append(input())
minx = 10 ** 9
miny = 10 ** 9
for x in range(n):
for y in range(m):
if s[x][y] == 'R':
minx = min(minx, x)
miny = min(miny, y)
print('YES' if s[minx][miny] == 'R' else 'NO')
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
def can(pos, m):
k = len(pos)
x = k - m
for i in range(m + 1):
l = pos[i]
r = pos[i + x - 1]
if r - l + 1 - x <= m:
return True
return False
t = int(input())
for i in range(t):
s = input()
pos = []
n = len(s)
for i in range(n):
if s[i] == '1':
pos.append(i)
lf = 0
rg = len(pos)
while rg - lf > 1:
mid = (lf + rg) // 2
if can(pos, mid):
rg = mid
else:
lf = mid
if len(pos) == 0 or pos[-1] - pos[0] == len(pos) - 1:
print(0)
else:
print(rg)
Tutorial
Tutorial is loading...
Solution (vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
long long k;
cin >> n >> k;
vector<long long> a(n);
for (auto &it : a) {
cin >> it;
}
long long ans = 0;
for (int it = 0; it < n; ++it) {
vector<int> cnt(n);
for (int i = n - 1; i >= 0; --i) {
cnt[i] = (a[i] == 0);
if (i + 1 < n) {
cnt[i] += cnt[i + 1];
}
}
vector<long long> b = a;
long long s = accumulate(b.begin(), b.end(), 0ll);
bool ok = true;
for (int i = 0; i < n; ++i) {
if (b[i] == 0) {
long long x = (i + 1 < n ? cnt[i + 1] : 0);
b[i] = min(k, x * k - s);
if (b[i] < -k) {
ok = false;
}
s += b[i];
}
}
if (ok) {
long long pos = 0, mn = 0, mx = 0;
for (int i = 0; i < n; ++i) {
pos += b[i];
mn = min(mn, pos);
mx = max(mx, pos);
}
if (pos == 0) {
ans = max(ans, mx - mn + 1);
}
}
rotate(a.begin(), a.begin() + 1, a.end());
}
if (ans == 0) {
ans = -1;
}
cout << ans << endl;
return 0;
}
Idea: vovuh
Tutorial
Tutorial is loading...
Solution (vovuh)
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int tc;
cin >> tc;
while (tc--) {
int n;
string s[2];
cin >> n >> s[0] >> s[1];
for (int it = 0; it < 2; ++it) {
while (s[0].back() == '.' && s[1].back() == '.') {
s[0].pop_back();
s[1].pop_back();
}
reverse(s[0].begin(), s[0].end());
reverse(s[1].begin(), s[1].end());
}
n = s[0].size();
vector<vector<int>> cost(n, vector<int>(2));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
cost[i][j] = (s[j][i] == '*');
}
}
vector<vector<int>> dp(n, vector<int>(2, INF));
dp[0][0] = cost[0][1];
dp[0][1] = cost[0][0];
for (int i = 0; i + 1 < n; ++i) {
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + 1 + cost[i + 1][1]);
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 2);
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 1 + cost[i + 1][0]);
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 2);
}
cout << min(dp[n - 1][0], dp[n - 1][1]) << endl;
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
vector<vector<int>> g, h;
vector<int> tin, tout, clr;
vector<vector<int>> sum(2);
int T;
int flip;
int cnt;
bool isp(int v, int u){
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
void init(int v){
tin[v] = T++;
for (int u : g[v]){
if (clr[u] == -1){
clr[u] = clr[v] ^ 1;
h[v].push_back(u);
init(u);
}
else if (tin[u] < tin[v]){
int dif = clr[v] ^ clr[u];
if (!dif){
flip = clr[v] ^ 1;
++cnt;
}
--sum[dif][u];
++sum[dif][v];
}
}
tout[v] = T;
}
int sv;
void dfs(int v){
for (int u : h[v]){
dfs(u);
if (sum[0][u] == cnt && sum[1][u] == 1){
sv = u;
flip = clr[v] ^ 1;
}
forn(i, 2) sum[i][v] += sum[i][u];
}
}
int main() {
cin.tie(0);
iostream::sync_with_stdio(false);
int t;
cin >> t;
forn(_, t){
int n, m;
cin >> n >> m;
g.assign(n, vector<int>());
h.assign(n, vector<int>());
forn(i, m){
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
tin.resize(n);
tout.resize(n);
forn(i, 2) sum[i].assign(n, 0);
clr.assign(n, -1);
cnt = 0;
T = 0;
clr[0] = 0;
init(0);
if (cnt <= 1){
cout << "YES\n";
forn(v, n) cout << (clr[v] ^ flip);
cout << "\n";
continue;
}
sv = -1;
dfs(0);
if (sv == -1){
cout << "NO\n";
}
else{
cout << "YES\n";
forn(v, n) cout << (clr[v] ^ isp(sv, v) ^ flip);
cout << "\n";
}
}
return 0;
}