比赛链接:Here

1556A. A Variety of Operations

注意到2, 3操作不改变总和,1操作使得总和加上了一个偶数,故直接判断总和是否为偶数即可。如果和为偶数,只要判断c和d是否相等即可。注意0要判一下

【AC Code】

const int N = 1e5 + 7;
int n, m, k, tot, a[N];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int _; for (cin >> _; _--;) {
int c, d; cin >> c >> d;
if (c == 0 && d == 0) {
puts("0");
continue;
}
if ((c + d) % 2 == 0) {
if (c == d ) {
puts("1");
} else puts("2");
} else puts("-1");
}
}

1566B. Take Your Places!

题意:

给一个有 \(n\) 个数字的数组,可以交换相邻的数字,求最小交换次数使得相邻的数奇偶性不相同。

思路:

先判一下奇数和偶数的个数,在按顺序分配位置就行。如果奇数和偶数个数相等,还得枚举奇数先放还是偶数先放。注意开 ll

【AC Code】

int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n; cin >> n;
vector<int>a(n);
for (int &x : a) {
cin >> x;
x &= 1;
}
ll ans = LLONG_MAX;
for (int j = 0; j < 2; ++j) {
vector<int>b(n);
for (int i = 0; i < n; ++i) b[i] = (i & 1) == j;
vector<int>la, lb;
for (int i = 0; i < n; ++i) {
if (a[i]) la.push_back(i);
if (b[i]) lb.push_back(i);
}
if (la.size() != lb.size()) continue;
ll op = 0;
for (int i = 0; i < la.size(); ++i) op += abs(la[i] - lb[i]);
ans = min(ans, op);
}
cout << (ans >= LLONG_MAX ? -1 : ans) << "\n";
}
}

1566C. Compressed Bracket Sequence

莫名其妙地ac了,不是很懂。

首先枚举左端点 \(l\) 和右端点 \(r\) ,用函数 \(check(l,r)\) 统计这两个端点的贡献(在 \(l\) 处开始,$ r$ 处结束的合法括号序列的数量)。假设每一个左括号为 \(1\),右括号为 \(-1\) ,对序列求个前缀和,方便判断是否合法。

所有的从 \(l\) 开始,\(r\)​​ 结束的合法括号序列,不一定用了\(l\)​和\(r\)中的每一个左右括号。

首先计算 \(d=sum[r]-sum[l-1]\)​ ,以得出左右两边是否有多余的括号,我们要舍去。之后再枚举\(l+1\)到\(r−1\)的每一个位置\(i\),他们必须满足\(sum[i]−sum[l−1]>=0\) .统计这个差值的最小值,记为 \(mx\) ,其表示左边 \(l\)处最多能有几个括号多出来。那么答案至多有\(mx+1\).注意当\(d>0\)时,这个值要减去 \(d\) 后才能用于更新 \(mx\) 。

之后保险起见,还得和左边能贡献的左括号数量,右边能贡献的右括号数量取 \(\min\),注意此时也要把$ d$ 的偏差加上去,具体见代码。

【AC Code】

const int N = 1e5 + 7;
int n, m, k;
ll tot, c[N], ans, sum[N];
ll check(int l, int r) {
ll d = sum[r] - sum[l - 1], mx = 99999999999999999ll;
for (int i = l + 1; i < r; i += 2) {
if (d <= 0)
mx = min(mx, sum[i] - sum[l - 1]);
if (d > 0)
mx = min(mx, sum[i] - d - sum[l - 1]);
}
if (mx < 0) return 0;
//if (mx == 0) return 1;
if (d == 0) return min(c[l], min(mx + 1ll, -c[r]));
if (d < 0) return min(c[l], min(mx + 1ll, -c[r] + d));
if (d > 0) return min(c[l] - d, min(mx + 1ll, -c[r]));
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
if (i % 2 == 0) c[i] = -c[i];
sum[i] = sum[i - 1] + c[i];
}
for (int l = 1; l <= n; l += 2) {
for (int r = l + 1; r <= n; r += 2) {
ans += check(l, r);
}
}
cout << ans;
}

1566D. Take a Guess

第一次做交互题,要频繁清空流感觉有点麻烦

$ a+b=a & b+a \mid b$ ,用 \(a+b, a+c, b+c\)​ 可以求出 $ a b c $ ,已知 $ a$ 和 \(a \& b+a \mid b\) 可以求 b , 这样直接求出所有的 $ a[i]$​ 就行。

【AC Code】

注意每一行输出之后要立刻 fflush(stdout);

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
typedef pair<int, int> pii;
#define mk make_pair
const int maxn = 1e4 + 7;
int rd() {
int s = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {s = s * 10 + c - '0'; c = getchar();}
return s * f;
}
int n, m, k, tot;
ll a[maxn];
ll getPlus(int i, int j) {
printf("or %d %d\n", i, j);
fflush(stdout);
ll orij = rd();
printf("and %d %d\n", i, j);
fflush(stdout);
ll andij = rd();
return orij + andij;
}
void get(int i, int j, int k) {
ll pij = getPlus(i, j);
ll pjk = getPlus(j, k);
ll pik = getPlus(i, k);
a[i] = (pij - pjk + pik) / 2ll;
a[j] = (pjk - pik + pij) / 2ll;
a[k] = (pik - pij + pjk) / 2ll;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
n = rd(); k = rd();
for (int i = 1; i <= n; i += 3) {
if (i + 2 > n) break;
get(i, i + 1, i + 2);
}
if (n % 3 == 1) {
ll p1 = getPlus(1, n);
a[n] = p1 - a[1];
}
if (n % 3 == 2) {
ll p1 = getPlus(1, n - 1);
a[n - 1] = p1 - a[1];
ll p2 = getPlus(1, n);
a[n] = p2 - a[1];
}
sort(a + 1, a + n + 1);
printf("finish %lld\n", a[k]);
}

1556E. Equilibrium

给出俩个长为 \(n\) 的整数序列 \(a\) 和 \(b\)​ ,以及一些询问 $(1, \mathrm{r}) \(. 对于每个\) (l, r) \(, 回答最少要执行多少次操作,使得
\) \mathrm{a}[1 \ldots \mathrm{r}]=\mathrm{b}[1 \ldots \mathrm{r}] $​, 或是判断无解。

每次操作为选取偶数个下标 \(p o s_{i}\)​ , 满足 $ l \leq p o s_{1}<p o s_{2}<\cdots<p o s_{k} \leq r $​, 然后让 \(a\) 中下标为

$\operatorname{pos}{1}, \operatorname{pos}, p o s_{5}, \cdots, p o s_{k}-1 $ 的数 \(+1\) , 让 \(b\) 中下标为 $p o s_{2}, p o s_{4}, \operatorname{pos}{6}, \cdots, p o s $ 的位置的数 $+1 $.

每次询问是互相独立的。

sol:这里参考了高Rank大佬们的思路

【AC Code】

const int maxn = 1e5 + 7;
#define ll long long
int n, m, k, tot, a[maxn], q;
ll sum[maxn];
ll stmx[22][maxn], stmi[22][maxn];
int rd() {
int s = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {s = s * 10 + c - '0'; c = getchar();}
return s * f;
}
ll querymi(int l, int r) {
int len = r - l + 1, len2;
for (len2 = 0; (1 << (len2 + 1)) < len; len2++);
return min(stmi[len2][l], stmi[len2][r - (1 << len2) + 1]);
}
ll querymx(int l, int r) {
int len = r - l + 1, len2;
for (len2 = 0; (1 << (len2 + 1)) < len; len2++);
return max(stmx[len2][l], stmx[len2][r - (1 << len2) + 1]);
}
int main() {
n = rd();
q = rd();
//printf("%d %d\n", n, q);
for (int i = 1; i <= n; i++)
a[i] = rd();
for (int i = 1; i <= n; i++) {
a[i] -= rd();
sum[i] = sum[i - 1] + a[i];
stmx[0][i] = stmi[0][i] = sum[i];
}
//q = rd();
for (int j = 1; j <= 20; j++)
for (int i = 1; i <= n - (1 << j) + 1; i++)
stmi[j][i] = 100000000000001ll, stmx[j][i] = -100000000000001ll;
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= n - (1 << i) + 1; j++) {
stmi[i][j] = min(stmi[i - 1][j], stmi[i - 1][j + (1 << (i - 1))]);
stmx[i][j] = max(stmx[i - 1][j], stmx[i - 1][j + (1 << (i - 1))]);
}
}
while (q--) {
int l = rd();
int r = rd();
//printf("l == %d r == %d qmx == %lld qmi == %lld\n", l, r, querymx(l, r)-sum[l-1], querymi(l, r)-sum[l-1]);
if (sum[r] - sum[l - 1] != 0 || querymx(l, r) > sum[l - 1]) {
puts("-1");
continue;
} else {
printf("%lld\n", -(querymi(l, r) - sum[l - 1]));
}
}
return 0;
}

Deltix Round, Summer 2021 Div1 + Div2 A~E的更多相关文章

  1. CodeForce——Deltix Round, Autumn 2021 (open for everyone, rated, Div. 1 + Div. 2)前三道题目题解

    目录 A: B: C: 题目链接 A Divide and Multiply standard input/output 1 s, 256 MB 正在上传-重新上传取消 x13036 B Willia ...

  2. 【前行&赛时总结】◇第4站&赛时9◇ CF Round 513 Div1+Div2

    ◇第4站&赛时9◇ CF Round 513 Div1+Div2 第一次在CF里涨Rating QWQ 深感不易……作blog以记之 ( ̄▽ ̄)" +Codeforces 的门为你打 ...

  3. Codeforce Round #643 #645 #646 (Div2)

    codeforce Round #643 #645 #646 div2 Round #643 problem A #include<bits/stdc++.h> using namespa ...

  4. Codeforces Round #503 Div1+Div2 1019&1020

    https://winniechen.cn/?p=188 这个还是直接放链接吧,毕竟内容比较多...

  5. codeforces round #414 div1+div2

    A:判断一下就可以了 #include<bits/stdc++.h> using namespace std; typedef long long ll; int a, b, c, n; ...

  6. TopCoder 603 div1 & div2

    div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...

  7. TopCoder 649 div1 & div2

    最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...

  8. codeforces div1 & div2 参与人员分数统计

    Analysis helps to see the nature of things.

  9. Codeforces #505(div1+div2) D Recovering BST

    题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...

  10. Codeforces #505(div1+div2) C Plasticine zebra

    题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...

随机推荐

  1. Python有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

    n = 0 for i in range(1, 5): for j in range(1, 5): for k in range(1, 5): if(i != k) and (i != j) and ...

  2. phpmyadmin修改上传限制,phpmyadmin修改上传文件大小限制,docker版本phpmyadmin

    我用的是DOCKER 版本的phpmyadmin 修改/usr/local/etc/php/conf.d/phpmyadmin-misc.ini 内的限制变量文件为 100M,重启docker 容器后 ...

  3. 浅析MySQL代价模型:告别盲目使用EXPLAIN,提前预知索引优化策略

    背景 在 MySQL 中,当我们为表创建了一个或多个索引后,通常需要在索引定义完成后,根据具体的数据情况执行 EXPLAIN 命令,才能观察到数据库实际使用哪个索引.是否使用索引.这使得我们在添加新索 ...

  4. Bert-vits2新版本V2.1英文模型本地训练以及中英文混合推理(mix)

    中英文混合输出是文本转语音(TTS)项目中很常见的需求场景,尤其在技术文章或者技术视频领域里,其中文文本中一定会夹杂着海量的英文单词,我们当然不希望AI口播只会念中文,Bert-vits2老版本(2. ...

  5. scrum|敏捷开发之任务看板

    上篇文章中,我讲了敏捷第一步-每日站立会,讲了我们平时是怎么开站立会的,其实15-30分钟就够了,绝对不是时间长得让你想拄拐那种.本文我们开始讲敏捷开发中的看板.没有看板之前,我们真的是在白板上画泳道 ...

  6. jvm总结图解

    浅析jvm  内存模型 https://www.cnblogs.com/lewis0077/p/5143268.html

  7. 微调baichuan2-7b遇到的显存坑

    问题描述: 微调baichuan2-7b模型,验证一轮后继续训练第一个iteration显存大幅增加 项目链接: https://github.com/wp931120/baichuan_sft_lo ...

  8. 【C#】【System.IO】关于拷贝文件夹以及(Directory和DirectoryInfo、File和FileInfo)的区别

    本次问题是想要拷贝文件夹,但是找了一圈发现只有File有Copy或者FileInfo的CopyTo,并没有Directory的拷贝操作方法. 针对C#中拷贝文件夹的方法就是先生成一个目标文件夹(des ...

  9. Office 2016 2019 2021 正版部署

    教学视频:https://www.youtube.com/watch?v=VSjRx7Hoa60 文章摘抄自零度解说:https://www.freedidi.com/6619.html 1.offi ...

  10. docker: 'buildx' is not a docker command

    解决方法 sudo apt install docker-buildx-plugin