Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
for(int tc = 1; tc <= t; ++tc) {
int n; cin >> n;
int mn = INT_MAX, mx = INT_MIN;
for(int i = 0; i < n; ++i) {
int x; cin >> x;
mn = min(mn, x);
mx = max(mx, x);
}
if(mn < 0) cout << mn << '\n';
else cout << mx << '\n';
}
}
1838B - Minimize Permutation Subarrays
Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
#define N 200010
int idx[N];
int main() {
int t; cin >> t;
for(int tc = 1; tc <= t; ++tc) {
int n; cin >> n;
for(int i = 1; i <= n; ++i) {
int x; cin >> x;
idx[x] = i;
}
if(idx[n] < min(idx[1], idx[2])) {
cout << idx[n] << ' ' << min(idx[1], idx[2]) << '\n';
} else if(idx[n] > max(idx[1], idx[2])) {
cout << idx[n] << ' ' << max(idx[1], idx[2]) << '\n';
} else {
cout << idx[1] << ' ' << idx[2] << '\n';
}
}
}
Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
for(int tc = 1; tc <= t; ++tc) {
int n, m; cin >> n >> m;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(i % 2 == 0) cout << (n / 2 + i / 2) * m + j + 1 << ' ';
else cout << (i / 2) * m + j + 1 << ' ';
}
cout << '\n';
}
}
}
Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q; string s;
cin >> n >> q >> s;
set<int> a;
for(int i = 1; i <= n; ++i)
if((i % 2) != (s[i - 1] == '('))
a.insert(i);
while(q--) {
int i; cin >> i;
if(a.count(i)) a.erase(i);
else a.insert(i);
if(n % 2) cout << "NO\n";
else if(a.size() && (*a.begin() % 2 || !(*a.rbegin() % 2))) cout << "NO\n";
else cout << "YES\n";
}
}
Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll M = 1000000007;
ll pw(ll a, ll p) { return p ? pw(a * a % M, p / 2) * (p & 1 ? a : 1) % M : 1; }
ll inv(ll a) { return pw(a, M - 2); }
int main() {
ll t; cin >> t;
for(ll tc = 1; tc <= t; ++tc) {
ll n, m, k, ai;
cin >> n >> m >> k;
for(ll i = 0; i < n; ++i) cin >> ai;
ll ans = pw(k, m), mCi = 1;
for(ll i = 0; i < n; ++i) {
ans = (ans + M - mCi * pw(k - 1, m - i) % M) % M;
mCi = mCi * (m - i) % M * inv(i + 1) % M;
}
cout << ans << '\n';
}
}
Solution
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
char grid[110][110];
int n;
vector<pair<int, int>> snake;
map<pair<int, int>, char> getChar = {
{{1, 0}, 'v'},
{{-1, 0}, '^'},
{{0, 1}, '>'},
{{0, -1}, '<'}
};
pair<pair<int, int>, char> getBeltAndDir(pair<int, int> p) {
if(p.first == -1) return {p, 'X'};
pair<int, int> q = p;
if(p.first == 0) q.first++;
if(p.second == 0) q.second++;
if(p.first > n) q.first--;
if(p.second > n) q.second--;
return {q, getChar[{p.first - q.first, p.second - q.second}]};
}
pair<pair<int, int>, char> query(int idx) {
cout << "? " << snake[idx].first << ' ' << snake[idx].second << endl;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
cout << grid[i][j];
cout << endl;
}
int i, j; cin >> i >> j;
return getBeltAndDir({i, j});
}
void fillGrid() {
for(int i = 0; i < n * n; ++i) {
int dx = snake[i + 1].first - snake[i].first;
int dy = snake[i + 1].second - snake[i].second;
grid[snake[i].first][snake[i].second] = getChar[{dx, dy}];
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; ++i)
if(i % 2 == 0)
for(int j = n; j >= 1; --j)
snake.emplace_back(i, j);
else
for(int j = 1; j <= n; ++j)
snake.emplace_back(i, j);
snake.emplace_back(n + 1, (n % 2 ? n : 1));
fillGrid();
auto ans = query(0);
if(ans.second != 'X') {
snake.pop_back();
reverse(snake.begin(), snake.end());
snake.emplace_back(0, 1);
fillGrid();
ans = query(0);
}
if(ans.second == 'X') {
int id = 0;
for(int j = 13; j >= 0; --j)
if(id + (1 << j) < n * n && query(id + (1 << j)).second == 'X')
id += (1 << j);
ans.first = snake[id];
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(i < ans.first.first) grid[i][j] = '^';
else if(i > ans.first.first) grid[i][j] = 'v';
else grid[i][j] = j < ans.first.second ? '<' : '>';
ans.second = query(id).second;
}
cout << "! " << ans.first.first << ' ' << ans.first.second << ' ' << ans.second << endl;
}