Идея: Roms
Разбор
Tutorial is loading...
Решение (pikmike)
for tc in range(int(input())):
a, b, c = map(int, input().split())
print(1 if a < c else -1, end=" ")
print(b if c < a * b else -1)
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
for _ in range(int(input())):
s = input()
print('DA' if min(s.count('0'), s.count('1')) % 2 == 1 else 'NET')
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
for _ in range(int(input())):
s = input()
cur, mn, res = 0, 0, len(s)
for i in range(len(s)):
cur += 1 if s[i] == '+' else -1
if cur < mn:
mn = cur
res += i + 1
print(res)
1373D - Максимальная сумма на четных позициях
Идея: vovuh
Разбор
Tutorial is loading...
Решение (vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &it : a) cin >> it;
vector<vector<long long>> dp(n + 1, vector<long long>(3));
for (int i = 0; i < n; ++i) {
dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + (i & 1 ? 0 : a[i]));
if (i + 2 <= n) dp[i + 2][1] = max(dp[i + 2][1], max(dp[i][0], dp[i][1]) + (i & 1 ? a[i] : a[i + 1]));
dp[i + 1][2] = max(dp[i + 1][2], max({dp[i][0], dp[i][1], dp[i][2]}) + (i & 1 ? 0 : a[i]));
}
cout << max({dp[n][0], dp[n][1], dp[n][2]}) << endl;
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение 1 (BledDest)
#include<bits/stdc++.h>
using namespace std;
int n, k;
bool decimalLess(const string& s, const string& t)
{
if(s.size() != t.size())
return s.size() < t.size();
for(int i = 0; i < s.size(); i++)
if(s[i] != t[i])
return s[i] < t[i];
return false;
}
void upd(string& ans, const string& cur)
{
if(ans == "-1" || decimalLess(cur, ans))
ans = cur;
}
void read()
{
cin >> n >> k;
}
void solve()
{
string ans = "-1";
for(int i = 0; i <= 9; i++)
{
int cnt9 = n / 9;
if(i + k < 10)
cnt9 = 0;
for(int j = 0; j <= cnt9; j++)
{
if(i + k >= 10 && j == 0)
continue;
int curSum = (i + (i + k)) * (k + 1) / 2;
if(j != 0)
{
int cntBefore = 10 - i;
int cntAfter = k + 1 - cntBefore;
curSum = (i + 9) * cntBefore / 2 + cntBefore * 9 * (j - 1) + (1 + cntAfter) * cntAfter / 2;
}
curSum = n - curSum;
if(curSum < 0 || curSum % (k + 1) != 0)
continue;
string curNum = {char('0' + i)};
for(int z = 0; z < j - 1; z++)
curNum += "9";
int maxNum = 9;
if(i + k >= 10)
maxNum = 8;
curSum /= (k + 1);
while(curSum != 0)
{
int d = min(curSum, maxNum);
maxNum = 9;
curSum -= d;
curNum.push_back(char('0' + d));
}
reverse(curNum.begin(), curNum.end());
upd(ans, curNum);
}
}
cout << ans << endl;
}
int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
read();
solve();
}
}
Решение 2 (pikmike)
def get(s):
return str(s % 9) + '9' * (s // 9)
for tc in range(int(input())):
n, k = map(int, input().split())
k += 1
bst = 10**100
for d in range(10):
ends = 0
for i in range(k):
ends += (d + i) % 10
if ends > n:
continue
if d + k > 10:
for cnt in range(12):
s = 9 * cnt * (10 - d)
if s > n - ends:
break
for nd in range(9):
ns = s + (10 - d) * nd + (k - (10 - d)) * (nd + 1)
if ns > n - ends:
break
if (n - ends - ns) % k == 0:
bst = min(bst, int(get((n - ends - ns) // k) + str(nd) + '9' * cnt + str(d)))
elif (n - ends) % k == 0:
bst = min(bst, int(get((n - ends) // k) + str(d)))
print(-1 if bst == 10**100 else bst)
Идея: adedalic
Разбор
Tutorial is loading...
Решение 1 (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef pair<int, int> pt;
const int INF = int(1e9);
const li INF64 = li(1e18);
int n;
vector<int> a, b;
inline bool read() {
if(!(cin >> n))
return false;
a.resize(n);
fore(i, 0, n)
cin >> a[i];
b.resize(n);
fore(i, 0, n)
cin >> b[i];
return true;
}
struct func {
int minx, mink, maxk;
func() : minx(0), mink(0), maxk(0) {}
func(int minx, int mink, int maxk) : minx(minx), mink(mink), maxk(maxk) {}
};
func getFunc(int a, int b) {
if (a <= b)
return func(0, b - a, b);
else
return func(a - b, 0, b);
}
func merge(func a, func b) {
if (a.minx == -1 || b.minx == -1)
return func(-1, -1, -1);
if (a.maxk < b.minx)
return func(-1, -1, -1);
if (a.mink >= b.minx)
return func(a.minx, min(b.maxk, b.mink + (a.mink - b.minx)), min(b.maxk, b.mink + (a.maxk - b.minx)));
else {
int add = b.minx - a.mink;
return func(a.minx + add, b.mink, min(b.maxk, b.mink + (a.maxk - b.minx)));
}
}
inline void solve() {
func total = getFunc(a[0], b[0]);
fore(i, 1, n)
total = merge(total, getFunc(a[i], b[i]));
if (total.minx != -1 && total.minx <= total.mink)
cout << "YES\n";
else
cout << "NO\n";
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int tc; cin >> tc;
while(tc--) {
read();
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Решение 2 (Ne0n25)
#include <bits/stdc++.h>
using namespace std;
typedef long long li;
const int N = 1000 * 1000 + 13;
int n;
int a[N], b[N];
void solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
li need = 0, add = 0, extra = 2e9;
for (int i = n - 1; i >= 0; --i) {
if (b[i] < need) {
puts("NO");
return;
}
li val = b[i] - a[i];
li to_add = min(extra, max(0LL, val - need));
add += to_add;
extra = min(extra - to_add, b[i] - need - to_add);
need = max(0LL, need - val);
}
puts(add >= need ? "YES" : "NO");
}
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; ++i)
solve();
}
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 9;
int n, k;
int m;
int t[8 * N];
int add[8 * N];
int cnt[2 * N];
void push(int v, int l, int r) {
if (r - l != 1 && add[v] != 0) {
t[v * 2 + 1] += add[v];
t[v * 2 + 2] += add[v];
add[v * 2 + 1] += add[v];
add[v * 2 + 2] += add[v];
add[v] = 0;
}
}
void upd(int v, int l, int r, int L, int R, int x) {
if (L >= R) return;
if(l == L && r == R) {
t[v] += x;
add[v] += x;
return;
}
push(v, l, r);
int mid = (l + r) / 2;
upd(v * 2 + 1, l, mid, L, min(R, mid), x);
upd(v * 2 + 2, mid, r, max(L, mid), R, x);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
void upd(int l, int r, int x) {
upd(0, 0, n + n, l, r, x);
}
int get(int v, int l, int r, int L, int R) {
if (L >= R) return 0;
if (l == L && r == R) return t[v];
push(v, l, r);
int mid = (l + r) >> 1;
return max(get(v * 2 + 1, l, mid, L, min(R, mid))
, get(v * 2 + 2, mid, r, max(L, mid), R));
}
int get(int l, int r) {
return get(0, 0, n + n, l, r);
}
int getAns(set <int> &smx) {
if (smx.empty()) return 0;
return max(0, get(0, *smx.rbegin() + 1) - n);
}
void build(int v, int l, int r) {
if (r - l == 1) {
t[v] = l;
return;
}
int mid = (l + r) >> 1;
build(v * 2 + 1, l, mid);
build(v * 2 + 2, mid, r);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
int main(){
scanf("%d %d %d", &n, &k, &m);
build(0, 0, n + n);
set <pair<int, int> > s;
set <int> smx;
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d %d", &x, &y);
int pos = abs(x - k) + y - 1;
pair <int, int> p = make_pair(x, y);
if (s.count(p)) {
--cnt[pos];
if (cnt[pos] == 0) smx.erase(pos);
upd(0, pos + 1, -1);
s.erase(p);
} else {
++cnt[pos];
if (cnt[pos] == 1) smx.insert(pos);
upd(0, pos + 1, 1);
s.insert(p);
}
printf("%d\n", getAns(smx));
}
return 0;
}