Educational Codeforces Round 172 (Rated for Div. 2)(C-D)
题目链接:Dashboard - Educational Codeforces Round 172 (Rated for Div. 2) - Codeforces
C. Competitive Fishing
tag:后缀和 + 思维
Description:有一个序列含\(n\)个数(每个数是\(0\)或\(-1\)),将其分为\(m\)个区间,从前往后每个区间中第\(i\)个区间的权值为\((i - 1)\),求序列权值和大于等于\(k\)的最小区间划分数,如果不能输出\(-1\)。
Solution:在第\(i\)个数前面划分一次的贡献为其后缀和,因此我们贪心的取后缀和即可,注意第一个数前面不能划分。
%%
1
4 4
0011
3
%%
void solve(){
int n, k;
string ss;
cin >> n >> k >> ss;
ss = '&' + ss;
vector<int> a(n + 1), s(n + 2);
for (int i = 1; i <= n; i ++){
if (ss[i] == '1')
a[i] = 1;
else
a[i] = -1;
}
priority_queue<int> qu;
for (int i = n; i > 1; i --){ // 后缀和
s[i] = s[i + 1] + a[i];
qu.push(s[i]);
}
int ans = 0, sum = 0;
while (qu.size()){
sum += qu.top();
qu.pop();
ans ++;
if (sum >= k){
cout << ans + 1 << endl;
return;
}
}
cout << -1 << endl;
}
D. Recommendations
tag:思维
Description:给定n个区间[l,r],输入所有包含该区间的公共区间减去该区间的长度。1 <= n <= 2e15, 1 <= l <= r <= 1e9。
Solution:区间问题考虑先排序再处理。用两个数组R[i],L[i],存储第i个区间对应的右边界和左边界。考虑如何求右边界。
- 显然需要按左端点从小到大排序,因为需要左边包含当前区间,同时右端点从大到小排序,让能包含的区间先出现。
- 左边界同理。
- 如果有两个区间相同特判为0。(用map记录)
trick:区间问题考虑先排序再处理,用set自带的st.lower_bound()比lower_bound函数快,但是前者返回的是迭代器。
struct node{
int l, r, id;
};
bool cmp1(node a, node b){ // 左端点从小到大排序,右端点大在前,计算Ri
if (a.l == b.l)
return a.r > b.r;
else
return a.l < b.l;
}
bool cmp2(node a, node b){
if (a.r == b.r)
return a.l < b.l;
else
return a.r > b.r;
}
void solve(){
int n;
cin >> n;
vector<node> a(n);
vector<node> id(n);
map<pii, int> mp;
for (int i = 0; i < n; i ++){
cin >> a[i].l >> a[i].r;
a[i].id = i;
id[i] = a[i];
mp[{a[i].l, a[i].r}] ++;
}
vector<int> L(n), R(n);
sort(a.begin(), a.end(), cmp1);
set<int> st;
for (int i = 0; i < n; i ++){
if (st.size() == 0){
R[a[i].id] = a[i].r;
}
else{
if (st.lower_bound(a[i].r) == st.end()){
R[a[i].id] = a[i].r;
}
else{
int xr = *(st.lower_bound(a[i].r));
R[a[i].id] = xr;
}
}
st.insert(a[i].r);
}
st.clear();
sort(a.begin(), a.end(), cmp2);
for (int i = 0; i < n; i ++){
if (st.size() == 0){
L[a[i].id] = a[i].l;
}
else{
if (st.lower_bound(-a[i].l) == st.end()){
L[a[i].id] = a[i].l;
}
else{
int xr = *(st.lower_bound(-a[i].l));
L[a[i].id] = -xr;
}
}
st.insert(-a[i].l);
}
for (int i = 0; i < n; i ++){
if (mp[{id[i].l, id[i].r}] > 1){
cout << 0 << endl;
}
else
cout << (R[i] - id[i].r) + (id[i].l - L[i]) << endl;
}
}
Educational Codeforces Round 172 (Rated for Div. 2)(C-D)的更多相关文章
- Educational Codeforces Round 59 (Rated for Div. 2) (前四题)
A. Digits Sequence Dividing(英文速读) 练习英语速读的题,我还上来昏迷一次....只要长度大于2那么一定可以等于2那么前面大于后面就行其他no 大于2的时候分成前面1个剩下 ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)
第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...
- Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)
感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...
- D Merge Equals Educational Codeforces Round 42 (Rated for Div. 2) (STL )
D. Merge Equals time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
随机推荐
- 高德地图API-搜索提示并定位到位置,卫星地图和标准地图的切换
// _yourMap地图实例 _yourMap.plugin(["AMap.MapType"], function () { //添加地图类型切换插件 //地图类型切换 mapT ...
- 【每天学点AI】前向传播、损失函数、反向传播
在深度学习的领域中,前向传播.反向传播和损失函数是构建和训练神经网络模型的三个核心概念.今天,小编将通过一个简单的实例,解释这三个概念,并展示它们的作用. 前向传播:神经网络的"思考&quo ...
- The 2024 ICPC Asia East Continent Online Contest (II) K.Match
题面 K.Match 给定长度为 \(n\) 的两个序列 \(a\) 和 \(b\),当且仅当 \(a_i ⊕ b_j ≥ k\) 时,\(a_i\) 与 \(b_j\) 连一条双向边,其中 \(⊕\ ...
- meta-analysis初学--笔记摘抄
meta-analysis的定义 Meta-analysis是指对研究的研究,可以翻译为元分析.后设分析.整合分析.荟萃分析等.最常用的翻译是荟萃分析.Meta-analysis是用统计的概念与方法, ...
- Webshell流量分析之菜刀Chopper&蚁剑AntSword
目录 中国菜刀 蚁剑 菜刀和蚁剑的一句话木马的流量都有一个特点,都没有加密的,使用wireshark抓包来分析. 中国菜刀 中国菜刀是一款经典的webshell管理工具,具有文件管理.数据库管理.虚拟 ...
- [昌哥IT课堂]|ubuntu18.04手动安装mysql8.0.33 deb包
前期准备 1.更新aliyun的软件包安装源: 手动更改 用你熟悉的编辑器打开: /etc/apt/sources.list 把源来链接删除或注释: 加入以下命令: ubuntu 18.04 LTS ...
- 移动端PDF阅读器重排版效果对比-小白PDF阅读器与KOReader重排效果对比
PDF是一种跨操作系统平台的电子文件格式,它能在各种不同的平台上以相同的版式显示.很多扫描书籍或者电子书籍都会采用PDF格式存储.但是移动端由于屏幕的限制,以原版展示PDF会导致画面缩放严重,影响阅读 ...
- Web网页端IM产品RainbowChat-Web的v7.2版已发布
一.关于RainbowChat-Web RainbowChat-Web是一套Web网页端IM系统,是RainbowChat的姊妹系统(RainbowChat是一套基于开源IM聊天框架 MobileIM ...
- 长连接网关技术专题(十一):揭秘腾讯公网TGW网关系统的技术架构演进
本文由腾讯技术团队peter分享,原题"腾讯网关TGW架构演进之路",下文进行了排版和内容优化等. 1.引言 TGW全称Tencent Gateway,是一套实现多网统一接入,支持 ...
- 基于开源IM即时通讯框架MobileIMSDK:RainbowChat v8.2版已发布
关于MobileIMSDK MobileIMSDK 是一套专门为移动端开发的开源IM即时通讯框架,超轻量级.高度提炼,一套API优雅支持UDP .TCP .WebSocket 三种协议,支持iOS.A ...