当前位置:网站首页>ABC260 E - At Least One (Dual Pointer)
ABC260 E - At Least One (Dual Pointer)
2022-08-01 13:32:00 【Harris-H】
ABC260 E - At Least One(双指针)
一开始想到two pointers 了,But I don't know how to quickly maintain whether the conditions are met.
Originally opened onevector,然后用cntThe variable holds the currently reached number of groups.Then only the pair becomes 0 0 0barrel effectcnt.
因为每个 i i i 对应的vector Iterates at most twice.
因此复杂度是: O ( n + m ) O(n+m) O(n+m)
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) cin >> A[i] >> B[i];
vector<vector<int>> inv(M + 1);
for (int i = 0; i < N; i++) {
inv[A[i]].push_back(i);
inv[B[i]].push_back(i);
}
vector<int> cnt(N), ans(M + 3);
int cnt_zero = N;
for (int i = 1, j = 1; i <= M;) {
while (j <= M and cnt_zero != 0) {
for (auto& x : inv[j]) {
if (cnt[x] == 0) cnt_zero--;
cnt[x]++;
}
j++;
}
if (cnt_zero != 0) break;
ans[j - i]++, ans[M + 1 - i + 1]--;
for (auto& x : inv[i]) {
cnt[x]--;
if (cnt[x] == 0) cnt_zero++;
}
i++;
}
for (int i = 1; i <= M; i++) {
ans[i] += ans[i - 1];
cout << ans[i] << " \n"[i == M];
}
}
According to double pointer,我们可以枚举左端点,The right endpoint actually we just need to update r = m a x ( r , m x [ l + + ] ) r=max(r,mx[l++]) r=max(r,mx[l++])
这里 m x [ v a l ] mx[val] mx[val] are all left endpoints of v a l val valThe corresponding maximum right endpoint value.
显然 l + + l++ l++之后, r r r 必须大于等于 m x [ l ] mx[l] mx[l].
特别地,当 l = 1 l=1 l=1, r r r is the maximum value of all left endpoints.这样 r r r是最小的 r r r.
然后双指针 O ( 1 ) O(1) O(1)更新即可.
注意 l ≤ min { b [ i ] } l\le \min\{b[i]\} l≤min{ b[i]} ,不然无解.
时间复杂度: O ( n ) O(n) O(n)
// Problem: E - At Least One
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Date: 2022-07-30 23:13:40
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {
402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
if(x>y) x=y;
}
PII a[N];
int left_mx[N];
int is_right[N];
ll pre[N];
int n,m;
bool ck(int x){
for(int i=1;i<=n;i++){
if(x<a[i].x) return false;
}
return true;
}
int main(){
ll mn = 1e18;
cin>>n>>m;
rep(i,1,n){
cin>>a[i].x>>a[i].y;
cmx(left_mx[a[i].x],a[i].y);
cmn(mn,1LL*a[i].y);
}
int l=1,r=m,ans=0;
while(l<=r){
int mid = l+r>>1;
if(ck(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
//printf("%d %d\n",1,ans);
for(int i=1,j=ans;i<=mn;cmx(j,left_mx[i++])){
pre[j-i+1]++,pre[m-i+2]--;
}
rep(i,1,m) pre[i]+=pre[i-1];;
rep(i,1,m){
printf("%lld ",pre[i]);
}
return 0;
}
边栏推荐
- 批量替换Word中的表格为图片并保存
- 多线程案例——阻塞式队列
- iPhone难卖,被欧洲反垄断的服务业务也难赚钱了,苹果的日子艰难
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- PAT 1162 Postfix Expression(25)
- 华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
- Efficiency tools to let programmers get off work earlier
- The basic knowledge of scripting language Lua summary
- 【2022蓝帽杯】file_session && 浅入opcode
- SQL函数 SQUARE
猜你喜欢
MCU开发是什么?国内MCU产业现状如何
台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
多线程案例——阻塞式队列
【每日一题】1331. 数组序号转换
让程序员早点下班的效率工具
NebulaGraph v3.2.0 Performance Report
搭建LNMT架构
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
免费使用高性能的GPU和TPU—谷歌Colab使用教程
随机推荐
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
透过开发抽奖小程序,体会创新与迭代
The obstacles to put Istio into production and how we solve them
【每日一题】1161. 最大层内元素和
【无标题】
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
微信UI在线聊天源码 聊天系统PHP采用 PHP 编写的聊天软件,简直就是一个完整的迷你版微信
什么是混合元编程
库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
一文带你彻底厘清 Isito 中的证书工作机制
tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
态路小课堂丨浅谈优质光模块需要具备的条件!
【StoneDB Class】入门第二课:StoneDB 整体架构解析
How do we do full-link grayscale on the database?
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
CCS软件安装教程(超级详细)「建议收藏」
人像分割技术解析与应用