当前位置:网站首页>ABC260 E - At Least One(双指针)
ABC260 E - At Least One(双指针)
2022-08-01 13:22:00 【Harris-H】
ABC260 E - At Least One(双指针)
一开始想到two pointers 了,但是不知道怎么快速维护是否满足条件。
原来开一个vector,然后用cnt变量存当前达到的组数。然后只对变为 0 0 0的桶影响cnt。
因为每个 i i i 对应的vector 至多遍历两次。
因此复杂度是: 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];
}
}
根据双指针,我们可以枚举左端点,右端点其实我们只需要更新 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] 是所有左端点为 v a l val val对应的最大右端点值。
显然 l + + l++ l++之后, r r r 必须大于等于 m x [ l ] mx[l] mx[l]。
特别地,当 l = 1 l=1 l=1, r r r 为所有左端点的最大值。这样 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;
}
边栏推荐
- What Can Service Mesh Learn from SDN?
- 【每日一题】952. 按公因数计算最大组件大小
- How to integrate 3rd party service center registration into Istio?
- 2022-07-29 网工进阶(二十二)BGP-其他特性(路由过滤、团体属性、认证、AS欺骗、对等体组、子路由器、路由最大接收数量)
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- iPhone难卖,被欧洲反垄断的服务业务也难赚钱了,苹果的日子艰难
- LeetCode_动态规划_中等_377.组合总和 Ⅳ
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- The basic knowledge of scripting language Lua summary
- 论文详读《基于改进 LeNet-5 模型的手写体中文识别》,未完待补充
猜你喜欢
随机推荐
The obstacles to put Istio into production and how we solve them
sql中常用到的正则表达
PAT 1167 Cartesian Tree(30)
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
postgresql之page分配管理(一)
SQL函数 %SQLUPPER
拥抱NFV,Istio 1.1 将支持多网络平面
shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
动态库、静态库浅析
响应式2022英文企业官网源码,感觉挺有创意的
LeetCode_动态规划_中等_377.组合总和 Ⅳ
数据挖掘-03
六石编程学:问题要面对,办法要技巧,做不好的功能要想办法
JMP Pro 16.0软件安装包下载及安装教程
为什么最大值加一等于最小值
NFV迈向云原生时代:Network Service Mesh项目介绍
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
Two Permutations
什么是一致性哈希?可以应用在哪些场景?
SQL函数 STR