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 ...
随机推荐
- 使用Cfssl生成etcd证书(pem)
CFSSL是CloudFlare开源的一款PKI/TLS工具,CFSSL包含一个命令行工具和一个用于签名,验证并且捆绑TLS证书的HTTP API服务,使用Go语言编写. github: https: ...
- Win10使用SSH反向隧道(端口转发)连接远程桌面
应用场景: 如果你有Linux云主机(腾讯.华为等),且公司有一台只有内网IP (或动态IP) 的Win10工作机:你计划在家里工作时,通过家里的电脑连接公司的工作机 (且不想使用类似Teamview ...
- Python实验:Socket编程
实验六 Socket 编程 一.实验目标: 了解TCP协议原理.标准库socket 的用法.熟悉Socket 编程. 1.TCP协议原理: TCP(Transmission Control Proto ...
- Java 设计模式——从冰雪经济看设计模式的综合运用(工厂、单例、策略、观察者)
当冬季的寒风拂过大地,冰雪经济如同一颗璀璨的明珠,在寒冷中散发着炽热的魅力.滑雪场.冰雕展.冰雪主题酒店等各类冰雪产业蓬勃发展,其背后的运营逻辑和策略,与 Java 设计模式有着奇妙的相似之处,为我们 ...
- 前后端数据传递之form-data
前情 最近在项目开发中,跟服务端连调发现接口一直报错,服务端一直提示是数据没有传,而通过浏览器控制台发现数据是有传的. 坑 服务通过postman自测是OK的.经过和服务端一起定位发现服务端只接收以f ...
- RabbitMQ 快速入门
RabbitMQ 快速入门 官网:https://www.rabbitmq.com/ 入门教程:https://www.rabbitmq.com/tutorials 最新版本:4.0.2 版本参考:J ...
- Typecho COS插件实现网站静态资源存储到COS,降低本地存储负载
** Typecho 简介** Typecho 是一个简单.强大的轻量级开源博客平台,用于建立个人独立博客.它具有高效的性能,支持多种文件格式,并具有对设备的响应式适配功能.Typecho 相对于其他 ...
- 腾讯云携手Commvault,为云上用户提供安全存储服务
11月2日获悉,腾讯云对象存储COS近日正式通过Commvault备份软件标准化测试,并获得官方认证. 同时,Commvault对COS的支持已经从底层打通.这意味着用户只要购买了腾讯云COS的云存储 ...
- nodejs koa2 ocr识别 身份证信息
1. 安装依赖 npm install baidu-aip-sdk 2.创建AipOcrClient 注:需要到百度api创建应用,拿到所需的APPID/AK/SK https://console.b ...
- kubeadm安装 k8s集群证书过期更新
kubeadm安装 k8s集群证书过期更新 kubeadm版本 v1.18.8 #查看证书 #mast节点,查看所有证书 kubeadm alpha certs check-expiration #若 ...