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 ...
随机推荐
- Surface pro 11二合一平板参数调研
最近研究了下Surface pro 11,记录下相关参数,矩阵我以表格列出来.可能不够细,大家作个参考吧 模块 技术项 参数 备注 处理器 型号 Snapdragon X Elite(X1E-80-1 ...
- sprintboot-aop切面编程demo
AOP(面向切面编程)的核心概念是"切面". 切面是一个跨越多个对象的类,它封装了横切关注点的具体实现.通过定义切面,开发人员可以将通用功能从业务逻辑中分离出来,形成独立的模块.在 ...
- 低功耗4G模组:RSA算法示例
今天我们学习合宙低功耗4G模组Air780EP_LuatOS_rsa示例,文末[阅读原文]获取最新资料. 一.简介 RSA算法的安全性基于:将两个大质数相乘很容易,但是想要将其乘积分解成原始的质数 ...
- Air780E篇:采集温湿度传感器数据,并网页查看
今天我们学习合宙低功耗4G模组Air780E篇:采集温湿度传感器数据并实现网页查看,以下进入正文. 一.硬件装备 1.1 硬件连接 使用跳线帽将IO_SEL连接3.3V,给引脚供3.3V的电.dh ...
- Linux中更新系统时间、同步系统时间和硬件时间
更新系统的时间 1.手动修改 date -s # 不建议 2.时间同步服务器 ntpdate # 需要安装命令 yum -y install ntpdate [root@oldbo ...
- 进程管理工具之PM2
Github地址 https://github.com/Unitech/pm2 官方文档 http://pm2.keymetrics.io/docs/usage/quick-start/ npm安装 ...
- Elasticsearch之常见问题
一. 聚合操作时,报Fielddata is disabled on text fields by default GET /megacorp/employee/_search { "agg ...
- 【朝花夕拾】蓝牙&WiFi常识篇
一.蓝牙常识点 1.常见英文缩写 缩写 英文全称 释义 BLE Bluetooth Low Energy 低功耗蓝牙 BR Basic Rate 基本速率,一般说的经典蓝牙就是指BR/EDR EDR ...
- Python 调整Excel行高、列宽
在Excel中,默认的行高和列宽可能不足以完全显示某些单元格中的内容,特别是当内容较长时.通过调整行高和列宽,可以确保所有数据都能完整显示,避免内容被截断.合理的行高和列宽可以使表格看起来更加整洁和专 ...
- 高效文件处理:Python pathlib实战指南
在使用Python处理文件路径时,强烈建议使用pathlib. pathlib以面向对象的方式处理文件路径,既避免了很多陷阱,也能使执行许多路径的相关操作变得更容易. 本篇总结了常用的使用pathli ...