Codeforces Round 967 (Div. 2)
题目链接:Codeforces Round 967 (Div. 2) - Codeforces
总结:B题没测试就交wa一发,C题一直没想到怎么回溯,哎。
A. Make All Equal
tag:签到
Solution:找到相同元素的最大值,将其它所有元素删去。
void solve(){
cin >> n;
vector<int> a(n);
map<int, int> mp;
int ans = 0;
for (int i = 0; i < n; i ++){
cin >> a[i];
mp[a[i]] ++;
ans = max(ans, mp[a[i]]);
}
cout << n - ans << endl;
}
B. Generate Permutation
tag:构造
Solution:对于一个数\(i\),我们发现如果\(i - 1\)在它左边,则第二台打字机执行一次回车;如果在它右边,则第一台打字机执行一次回车。那么我们构造一个\(a_4a_2a_1a_3a_5\)。同时发现\(n\)是偶数,一定不行。
Competing:一下就猜中了,写完代码后没测试,wa一发。
void solve(){
cin >> n;
if (n % 2 == 0){
cout << "-1\n";
}
else{
vector<int> a(n);
int t = n - 1;
for (int i = 1; t; i ++){
a[i] = t;
t -= 2;
}
t = n;
for (int i = n; t >= 1; i --){
a[i] = t;
t -= 2;
}
for (int i = 1; i <= n; i ++){
cout << a[i] << " \n"[i == n];
}
}
}
C. Guess The Tree
tag:交互 + dfs
Description:给定一个\(n\)个节点的数,每次询问输出? a b,会返回一个节点\(x\),使|d(x - a) - d(x - b)|最小。\(d(x, y)\)表示\(x\)到\(y\)的距离。如果有多个这样的节点,会返回使d(x, a)最小的节点。用最多\(15n\)次查询,得出树的结构。
Solution:分析每次询问得到的结果发现每次都会给出\(a, b\)路径上的中点\(mid\),那么我们继续询问\(a, mid\)与\(mid, b\),直到\(mid == a || mid == b\)。
- 需要记录每个节点的父节点,避免重复询问。
- 对于一棵树,我们可以假设任意一个节点为根
Competing:想到应该用\(mid\)继续往下问,但是没想到使用\(dfs\),导致不知道如何回溯。
int query(int a, int b){
cout << "? " << a << " " << b << endl;
int x;
cin >> x;
return x;
}
void dfs(int a, int b){ // a是父节点
if (fa[a] && fa[b])
return;
int t = query(a, b);
if (a == t || t == b){
fa[b] = a;
ans.push_back({a, b});
return;
}
else{
dfs(a, t);
dfs(t, b);
}
}
void solve(){
cin >> n;
ans.clear();
for (int i = 0; i <= n; i ++){
fa[i] = 0;
}
fa[1] = 1;
while (ans.size() < n - 1){
for (int i = 2; i <= n; i ++){
if (fa[i] == 0){
dfs(1, i);
}
}
}
cout << "! ";
for (int i = 0; i < ans.size(); i ++){
cout << ans[i].fi << " " << ans[i].se << " ";
}
cout << endl;
}
D. Longest Max Min Subsequence
tag:思维 + 贪心 + 单调队列优化
Description:给定一个整数序列\(a\),\(s\)是\(a\)的所有非空子序列的集合,要找到一个最长的\(s\),如果有多个序列,则找出奇数位乘\(-1\),之后字典序最小的序列。
n <= 3e5
Solution:我们首先可以确定哪些值是必须要选的。我们要保证能选出最长的序列,就需要记录每个值最后出现的位置,在最后出现的位置最小的元素之前我们可以任意选择,而不能先选在该元素之后的其他元素。
- 一个可选的区间内,对于奇数位我们选择值最大的元素,对于偶数位我们选择值最小的元素。如果我们每次暴力寻找,时间复杂度为\(O(n ^2)\),考虑使用单调队列优化。
void solve(){
cin >> n;
vector<int> a(n + 1);
unordered_map<int, int> mp;
set<int> st;
for (int i = 1; i <= n; i ++){
cin >> a[i];
mp[a[i]] = i;
}
for (auto [x, y] : mp){ // 记录每个元素最后出现的位置
st.insert(y);
}
int sz = mp.size();
cout << sz << endl;
int s = 1;
int c = 0;
priority_queue<pii, vector<pii>, greater<pii>> smi; // 值从小到大,序号从小到大
priority_queue<pii, vector<pii>, greater<pii>> sma; // 值从大到下,序号从小到大
int tt = 0;
while (c < sz){
int et = *(st.begin()); // 当前最前面最后出现的元素的位置
int res, idx;
for (int i = s; i <= et; i ++){
if (mp[a[i]] != 0){ // 等于0说明选过了
smi.push({a[i], i});
sma.push({-a[i], i});
}
}
if ((c + 1) & 1){ // 选最大值
auto [x, y] = sma.top();
res = x, idx = y;
}
else{ // 选最小值
auto [x, y] = smi.top();
res = x, idx = y;
}
cout << a[idx] << " ";
st.erase(mp[a[idx]]); // 删除选择点的下标
mp[a[idx]] = 0;
s = et + 1;
c ++;
while (sma.size()){ // 删除选择过的元素或者下标小于当前值的元素
auto [x, y] = sma.top();
if (res > 0)
res = -res;
if (!mp[-x] || y <= idx)
sma.pop();
else
break;
}
while (smi.size()){
auto [x, y] = smi.top();
if (res < 0)
res = -res;
if (!mp[x] || y <= idx)
smi.pop();
else
break;
}
}
cout << endl;
}
Codeforces Round 967 (Div. 2)的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- 19、解析2_1(链、chunk、锁)
解析 shared pool 图解: library cache里面,暂时可以认为存储着: 1.SQL以及对应的执行计划(所占空间比较小): 2.存储过程.函数.触发器.包,它们编译后的对象(所占空间 ...
- NoSQL一致性
上面我们讲到了通过将数据冗余存储到不同的节点来保证数据安全和减轻负载,下面我们来看看这样做引发的一个问题:保证数据在多个节点间的一致性是非常困难的.在实际应用中我们会遇到很多困难,同步节点可能会故障, ...
- Python之pandas读取Excel
#! -*- coding utf-8 -*- """ 模块功能:读取当前文件夹下的Source里的Excel文件,显示其相关信息 说明:默认把Excel的第一行当做列名 ...
- 微服务应用整合SEATA实现分布式事务
概要 seata 是alibaba 出的一款分布式事务管理器,他有侵入性小,实现简单等特点.我们能够使用seata 实现分布式事务管理, 是微服务必备的组件.他可以实现在微服务之间的事务管理,也可以实 ...
- node-sass安装问题
前情 最近在开发一个小程序项目,为了开发速度,部分页面使用原有H5,但原有H5需要对小程序做一定兼容适配,发现原有H5项目是个很古老项目. 坑位 在项目启动前,需要执行npm install安装项目依 ...
- idea中yaml文件中文乱码问题解决
idea打开yaml,或是properties文件,出现中文乱码. 解决步骤: 打开notepad++ ,新建iso-8859-1编码的空文件 将乱码文件通过notepad++直接打开,把正常显示的代 ...
- openEuler欧拉安装Gitlab
1. 安装GitLab wget https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh sud ...
- 如何使用图片压缩降低COS流量成本?
导语 本文将介绍如何通过[图片压缩]能力,让您降本增效的使用 COS ,文章将写得浅显易懂,旨在快速带领用户了解图片压缩的用法及带来的收益. **** 图片压缩为什么会让您降本增效?******** ...
- "一基双台三智" 中电金信智慧监督解决方案构筑国央企风控堡垒
近年来,国务院国资委先后下发<关于进一步排查中央企业融资性贸易业务风险的通知>.<关于规范中央企业贸易管理严禁各类虚假贸易的通知>等各类监管法规,并在2024年初中央企业工作会 ...
- metasploit扫描mysql空密码
靶机IP 192.168.255.100 攻击机IP 192.168.255.200 流程开始 查找mysql登录模块 msf5 > search mysql_login 加载这个模块 msf5 ...