题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1384

大意:有 \(n\) 个区间 \([a_i,b_i]\),每个区间有个权值 \(c_i\),找到一个最小的整数集合 \(U\) 满足,任意 \(i\) 都有 \([a_i,b_i]∩U\) 的元素个数大于等于 \(c_i\),求 \(U\) 元素个数

(\(1\le n \le 50000\),\(0\le a_i \le b_i \le 50000\),\(1\le c_i \le b_i-a_i+1\))

网上找了找发现都是差分约束的题解,我用树状数组+并查集过了,于是就有了这篇辣鸡题解

思路简单来说就是贪心,每次找到右端点最小的区间 \([a_i,b_i]\),令 \(cnt\) 为这个区间内已经存在的点数,从 \(b_i\) 开始往前找 \(c_i-cnt\) (小于等于 \(0\) 就不用管啦)个未被选取的点,选取之

那么并查集有啥用呢?就是快速找到未选取的元素(因为选了的元素被合并了)

复杂度 \(O(n\log W+W)\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define mst(a,x) memset(a,x,sizeof(a))
inline ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
const int N=50010;
const int mod=(1?1000000007:998244353);
#define lb(x) (x&(-x))
struct BIT{ //树状数组
ll t[N];
void add(ll x,ll k){
x++;
for(;x<=50009;x+=lb(x))
t[x]+=k;
}
ll sum(ll x){
x++;
ll ans=0;
for(;x!=0;x-=lb(x))
ans+=t[x];
return ans;
}
}bit;
struct DSU{ //并查集
int a[N];
void init(int n){iota(a,a+n+1,0);}
int fa(int x){return a[x]==x?x:a[x]=fa(a[x]);}
inline int &operator[](int x){return a[fa(x)];}
}d;
struct node{
int l,r,w;
bool operator<(const node &b)const{
return r<b.r; //按右端点排序
}
}a[N];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
while(1){
int n=read();
repeat(i,0,n){
a[i].l=read()+2;
a[i].r=read()+2;
a[i].w=read();
}
sort(a,a+n);
mst(bit.t,0);
d.init(N-1);
int ans=0;
repeat(i,0,n){
auto x=a[i];
int cnt=bit.sum(x.r)-bit.sum(x.l-1); //区间中已被选取的元素个数
int k=d[x.r];
while(cnt<x.w){
bit.add(k,1); //选取k
d[k]=d[k-1]; //删除k
k=d[k]; //查找k之前第一个未被删除的元素
ans++,cnt++;
}
}
printf("%d\n",ans);
}
return 0;
}

然后差分约束的解法不算难( 但是我没看出来),别的题解讲挺清楚的我就不写了

HDOJ1384 Intervals 题解的更多相关文章

  1. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  2. POJ1375:Intervals——题解

    http://poj.org/problem?id=1375 题目大意:有一盏灯,求每段被圆的投影所覆盖的区间. —————————————————————— 神题,卡精度,尝试用各种方法绕过精度都不 ...

  3. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  4. 算法与数据结构基础 - 排序(Sort)

    排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...

  5. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  6. [LeetCode]题解(python):056-Merge Intervals

    题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...

  7. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. LeetCode 题解 56. Merge Intervals

    题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...

  9. leetcode个人题解——#56 Merge Intervals

    思路,先按照结构体中start进行排序,然后遍历比较前后项是否有重合. 第一次用到三参数形式的sort(),第三个参数的bool函数要写到类外才通过. /** * Definition for an ...

随机推荐

  1. TensorFlow 编程基础

    1.TensorFlow 安装:https://www.cnblogs.com/pam-sh/p/12239387.html https://www.cnblogs.com/pam-sh/p/1224 ...

  2. Nginx(3)---代理与负载均衡

    一.代理简述 代理分为正向代理和反向代理, 正向代理:客户端与目标服务器之间增加一个代理服务器,客户端直接访问代理服务器,在由代理服务器访问目标服务器并返回客户端并返回 .比如夜深人静的时候访问的一些 ...

  3. 【译】Serilog 配置基础知识

    Serilog 使用简单的C# API来配置日志记录.当外部配置需要时,可以使用Serilog.Settings.AppSettings包(.NET 框架)或Serilog.Settings.Conf ...

  4. 使用Intellij编写Spring Hello World

    编写基于Intellij2016.3与Java SDK1.8 下载Spring最新jar包: http://repo.spring.io/release/org/springframework/spr ...

  5. 基于 H5与WebGL 的科幻风机 3D 展示

    前言 许多世纪以来,风力机同水力机械一样,作为动力源替代人力.畜力,对生产力的发展发挥过重要作用.近代机电动力的广泛应用以及二十世纪50年代中东油田的发现,使风机发电机的发展缓慢下来. 70年代初期, ...

  6. JAVA System.exit(0) 和 System.exit(1) 的区别

    System.exit(int state) 方法都是来结束当前运行的java虚拟机.所有System.exit(1).System.exit(0) 执行后都会退出程序. state为0时时正常退出, ...

  7. Keepalived 配置文件

    keepalived的配置文件: keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs.              全局定义及 ...

  8. k8s系列---部署集群

    docer启动出错 [root@centos-minion yum.repos.d]# systemctl start docker Job for docker.service failed bec ...

  9. Vscode开发Java环境搭建

    VSCode 开发 JAVA 微软为 Java 开发者推出了一个 Visual Studio Code 的安装程序.Visual Studio Code 中目前提供了许多 Java 扩展. 该软件包可 ...

  10. 字符编码及字节串bytes类型

    1 字符编码简介 ASCII码:美国人发明并使用,用1个字节(8位二进制)代表一个字符,ASCII码是其他任意编码表的子集(utf-16除外). Unicode:包含和兼容全世界的语言,与全世界的语言 ...