当前位置: 首页 > news >正文

Codeforces Round 853 (Div. 2)

Codeforces Round 853 (Div. 2)

C. Serval and Toxel's Arrays

思路:

求任意两个组合的元素个数。

  1. 注意到,其实每个元素都是独立的。他在任意组合的出现情况组成的贡献是可以分开讨论的。
  2. 我们讨论元素x。假设x在m+1个数组中出现了cnt次(一个数组最多只有一个x)。
  3. 那么对于任意两个数组可能出现的情况有:
    1. 同时有x,这样的组合是C(2,cnt),贡献1
    2. 只有一个有x,这样的组合是cnt*(m+1-cnt),贡献1
    3. 都没有x,不用讨论,贡献0
  4. 我们把每个数都分别这样讨论,就是答案。
#include <bits/stdc++.h>
using namespace std;
#define ll     long long
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef pair<int, int> pii;//double 型memset最大127,最小128
//std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int INF = 0x3f3f3f3f;         //int型的INF
const ll llINF = 0x3f3f3f3f3f3f3f3f;//ll型的llINF
const int N = 4e5 + 10;int a[N];
int cnt[N];
int main()
{std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t;cin >> t;while (t--){int n, m;cin >> n >> m;for (int i = 0; i <= n + m; ++i)cnt[i] = 0;for (int i = 1; i <= n; ++i){cin >> a[i];cnt[a[i]] = m + 1; //如果一个数没被修改,他会一直出现,共m+1次}int p, v;for (int i = 1; i <= m; ++i){cin >> p >> v;cnt[a[p]] -= m - i + 1; //被修改的数后面都不会再出现了(除非再给一次)cnt[v] += m - i + 1; //新的数后面都会出现(直到被修改没)a[p] = v;}ll ans = 0;for (int i = 1; i <= n + m; ++i)ans += (ll)cnt[i] * (cnt[i] - 1) / 2 + (ll)cnt[i] * (m + 1 - cnt[i]);cout << ans << endl;}return 0;
}

D. Serval and Shift-Shift-Shift

思路:

  1. 看数据,1e3,说明我们可以进行复杂度为O(N^2)的操作
  2. 首先,只要a至少存在一个1,我可以用1产生任何值(按位一位一位操作,可以把他们变成想要的1或者0),除了0(因为要求位移至少为1,所以不能消去自己)
  3. 当我们a的最高位1右区间需要1或者0时,我们可以把最高位移动到那里修改他,这个操作遍历右区间,我们每次操作,因为最高位1左边都是0,所以不会对操作位左边产生影响。而操作位右边有影响没事,我会一位一位向右过去修改。
  4. 修改左区间同理,找最小位1,不断左移修改左区间(不能还是从高位修改到低位),我们这次是利用不影响右区间,左区间等下会修改。两者相反。
  5. 最后,注意到a,b要么同时为0,要么同时不为0(a为0,无法变成非0,a不为0,无法变成0)
#include <bits/stdc++.h>
using namespace std;
#define ll     long long
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef pair<int, int> pii;//double 型memset最大127,最小128
//std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int INF = 0x3f3f3f3f;         //int型的INF
const ll llINF = 0x3f3f3f3f3f3f3f3f;//ll型的llINF
const int N = 2e3 + 10;bool a[N], b[N], c[N];
int ans[N];void mysolve()
{int n;string a1, b1;cin >> n >> a1 >> b1;if (a1 == b1){cout << 0 << endl;return;}int cnta = 0, cntb = 0; //记录啊a与b1的个数int cnt = 0; //操作数for (int i = 0; i < n; ++i){a[i] = a1[i] - '0', b[i] = b1[i] - '0';if (a[i])cnta++;if (b[i])cntb++;}//同时非0if (cnta && cntb){int ha = n, hb = n; //记录a与b的最高位1for (int i = 0; i < n; ++i)if (a[i]){ha = i;break;}for (int i = 0; i < n; ++i)if (b[i]){hb = i;break;}//修改b最高位1及往右的区间for (int i = hb; i < n; ++i){if (a[i] != b[i]){int tmp = i - ha; //表示位移ans[++cnt] = ha - i;//先记录,后面ha可能更新memcpy(c, a, sizeof(a)); //不能直接a数组间去异或,途中会修改a的for (int j = i; j < n; ++j){if (j - tmp >= n)break; //越界后面都是异或0了a[j] ^= c[j - tmp];if (a[j])ha = min(ha, j); //hb大于ha时,可能更新ha}}}if (ha < hb) //如果ha小于(注意,越高位越小,不是大于),那么需要把hb前面的1去掉{int la = 0; //a的最低位1for (int i = n - 1; i >= 0; --i)if (a[i]){la = i;break;}for (int i = hb - 1; i >= 0; --i) //往左更新{if (a[i]) //是1就删{int tmp = i - la;ans[++cnt] = la - i;memcpy(c, a, sizeof(a));for (int j = i; j >= 0; --j){if (j - tmp < 0)break;a[j] ^= c[j - tmp];}}}}cout << cnt << endl;for (int i = 1; i <= cnt; ++i)cout << ans[i] << ' ';cout << endl;}else cout << -1 << endl;
}int main()
{std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t;cin >> t;while (t--){mysolve();}return 0;
}

http://www.lryc.cn/news/33495.html

相关文章:

  • Ka频段需要更多带宽?
  • 初学pyinstaller打包过程中的一些问题
  • 第七章:Java常用类
  • Apk加固后多渠道打包
  • K8S + ISTIO 金丝雀部署的例子
  • python自带数据的模型合集
  • 女生学习大数据怎么样~有前景么
  • 统计代码量
  • uniapp在线升级关联云空间
  • 学习streamlit-2
  • Vscode中Vue文件保存格式化、 ElementUI、Font Awesome俩大插件使用
  • 汽车标定知识整理(三):CCP报文可选命令介绍
  • kubeadm安装K8S(集群)
  • Baumer工业相机堡盟相机如何使用PnPEventHandler实现相机掉线自动重连(C++新)
  • Windows 命令行基础
  • 面试官: 谈下音视频同步原理,音频和视频能绝对同步吗?
  • CFS三层靶机安装与配置
  • 爬虫入门教程-Spider
  • Python|蓝桥杯进阶第二卷——贪心
  • Chrome开发使用技巧总结
  • 你真的会在阳光下拍照片么?
  • 量化择时——均线策略及改进方法(第1部分—因子测算)
  • 封装几个有用的 Vue3 组合式API
  • MyBatisPlus中的条件构造器Wrapper
  • 类和对象及其构造方法
  • HStream Console、HStreamDB 0.14 发布
  • 参考文献怎么查找,去哪里查找?一篇文章讲明白这些问题
  • docker-compose+HAProxy+Keepalived搭建高可用 RabbitMQ 集群
  • 自动化框架如何搭建?让10年阿里自动化测试老司机帮你搞定!自动化测试脚本怎么写?
  • 剑指 Offer 15. 二进制中1的个数