Codeforces Round #367 (Div. 2) 题解

2023-12-15 17:49
文章标签 codeforces round div 题解 367

本文主要是介绍Codeforces Round #367 (Div. 2) 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://codeforces.com/contest/706


感想:唯一一次前四道题全部都有思路,而且能保证正确的一次CF。但是最后却只A了前两道水题,最后没能来得及写后两道题。也许是太晚了,脑袋不太清醒吧!后面才想到后两题的做法。回想一下,从18分钟写完B后,就开始打酱油到比赛末,最后只A两题,就愤愤不平。555555!


A.思路:水题。直接将各个点与源点比较算时间,求出最小值即可。详见代码。

#include <bits/stdc++.h>
using namespace std;
double a, b;
int n;int main(){//ios::sync_with_stdio(false);//cin.tie(0);cin >> a >> b >> n;double ans = INT_MAX;double x, y, v;while (n--){cin >> x >> y >> v;ans = min(ans, sqrt((x-a)*(x-a)+(y-b)*(y-b))/v);}printf("%.8f\n", ans);return 0;
}

B.思路:排序后二分查找或者树状数组可以解决。这里用的是树状数组,树状数组下标是输入的数,存储的是某区间小于等于给定数的个数。详见代码。注意:树状数组的写法,要注意询问时,给出的数可能大于最大下标。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int sum[maxn];
int n, q;int lowbit(int x){return x&(-x);
}void add(int p){while (p < maxn){++sum[p];p += lowbit(p);}
}int query(int p){int ans = 0;while (p > 0){ans += sum[p];p -= lowbit(p);}return ans;
}int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> n;int num;while (n--){cin >> num;add(num);}cin >> q;while (q--){cin >> num;if (num >= maxn)num = maxn-1;cout << query(num) << endl;}return 0;
}

C.思路:一开始准备枚举一遍后,得出结果。后来发现有问题,得到的结果明显不对。后来再仔细分析一下,发现是一道简单DP题,状态转移比较简单,不多解释。详见代码。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
ll dp[maxn][2];
int cost[maxn];
string s[maxn], t[maxn];
int n;int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> n;for (int i=1; i<=n; ++i)cin >> cost[i];for (int i=1; i<=n; ++i){cin >> s[i];t[i] = s[i];reverse(t[i].begin(), t[i].end());dp[i][0] = dp[i][1] = inf;}dp[1][0] = 0;dp[1][1] = cost[1];for (int i=2; i<=n; ++i){if (s[i] >= s[i-1])dp[i][0] = dp[i-1][0];if (s[i] >= t[i-1])dp[i][0] = min(dp[i][0], dp[i-1][1]);if (t[i] >= s[i-1])dp[i][1] = dp[i-1][0]+cost[i];if (t[i] >= t[i-1])dp[i][1] = min(dp[i][1], dp[i-1][1]+cost[i]);}ll ans = min(dp[n][0], dp[n][1]);cout << (ans==inf ? -1 : ans) << endl;return 0;
}

D.思路:一道字典树的题。将要插入的数的二进制位倒着建树(为什么?因为异或时高位尽量大,结果才尽量大),即高位在深度低的节点上。用一个数组记录经过各个节点的数的个数,插入时,每经过一个点,将节点的这个值加一,删除时,则减一。查找时,当前节点的这个值大于0,说明有数经过。对于要查找的这个数的高位,如果是1,要使异或值尽量大,那么就要往0的地方走,反之,往1的地方走,实在没办法走,只有按原路径走啦。详见代码。注意:0永远在树中。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000005;
int child[maxn][2], val[maxn];
int q, sz=1;
string op;void _insert(int x){int pos = 0;for (int i=29; i>=0; --i){int id = (x>>i)&1;if (!child[pos][id])child[pos][id] = sz++;pos = child[pos][id];++val[pos];}
}void _delete(int x){int pos = 0;for (int i=29; i>=0; --i){int id = (x>>i)&1;pos = child[pos][id];--val[pos];}
}int query(int x){int ans=0, pos=0;for (int i=29; i>=0; --i){int id = (x>>i)&1;if (id == 1){if (child[pos][0] && val[child[pos][0]]){ans += 1<<i;pos = child[pos][0];}elsepos = child[pos][1];}else{if (child[pos][1] && val[child[pos][1]]){ans += 1<<i;pos = child[pos][1];}elsepos = child[pos][0];}}return ans;
}int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> q;int num;_insert(0);while (q--){cin >> op >> num;if (op[0] == '+')_insert(num);else if (op[0] == '-')_delete(num);elsecout << query(num) << endl;}return 0;
}


这篇关于Codeforces Round #367 (Div. 2) 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/497323

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述