Description

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.

Write a program that:

reads the number of intervals, their end points and integers c1, ..., cn from the standard input,

computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,

writes the answer to the standard output.

Input

The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.

Output

The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.

Sample Input

5

3 7 3

8 10 3

6 8 1

1 3 1

10 11 1

Sample Output

6


思路

发现对于整个区间的约束是可以转化成前缀和的

然后就把\([a_i,b_i]\)这个区间的约数转化成\(a_i-1和b_i\)两个点,连边

然后从后向前相邻点连上权值是-1的边

从前向后连权值是0的边

然后跑一遍最长路就可以满足所有的约数条件了

注意这里的最长路,其实就是满足所有约束的最小代价


//Author: dream_maker
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e5 + 10;
struct Edge {
int v, w, nxt;
Edge(int v = 0, int w = 0, int nxt = 0):v(v), w(w), nxt(nxt) {}
} E[N * 3];
int head[N], tot = 0;
int n, a[N], b[N], c[N], maxl = 0;
int dis[N], inq[N];
void add(int u, int v, int w) {
E[++tot] = Edge(v, w, head[u]);
head[u] = tot;
}
void spfa() {
queue<int> q;
q.push(0);
fu(i, 1, maxl) dis[i] = -INF_of_int;
while (q.size()) {
int u = q.front(); q.pop();
inq[u] = 0;
for (int i = head[u]; i != -1; i = E[i].nxt) {
int v = E[i].v;
if (dis[v] < dis[u] + E[i].w) {
dis[v] = dis[u] + E[i].w;
if (!inq[v]) {
inq[v] = 1;
q.push(v);
}
}
}
}
}
int main() {
Read(n);
memset(head, -1, sizeof(head));
fu(i, 1, n) {
Read(a[i]); ++a[i];
Read(b[i]); ++b[i];
Read(c[i]);
maxl = max(maxl, max(a[i], b[i]));
add(a[i] - 1, b[i], c[i]);
}
fu(i, 1, maxl) add(i - 1, i, 0);
fu(i, 1, maxl) add(i, i - 1, -1);
spfa();
Write(dis[maxl]);
return 0;
}

POJ1201 Intervals【差分约束系统】的更多相关文章

  1. POJ1201 Intervals差分约束系统(最短路)

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  2. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

  3. Intervals(差分约束系统)

    http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...

  4. POJ 1201 Intervals (差分约束系统)

    题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...

  5. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  6. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  7. poj 1201/zoj 1508 intervals 差分约束系统

      // 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...

  8. PKU 1201 Intervals(差分约束系统+Spfa)

    题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...

  9. POJ1201 Intervals(差分约束系统)

    与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...

  10. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

随机推荐

  1. java自带的MD5

    前言:        MD5可生成16.32.64位数的签名. // MD5加码,32位 public static String toMD5(String plainText) { String r ...

  2. web.config文件配置解决网站上传大文件限制

    Asp.Net网站对上传文件的大小,请求执行的时间都做了限制,上传的文件如果超过限制或者执行上传时间超出, 文件上传都将失败. 因此,需要配置web.config来增加最大文件上传的大小和执行超时时间 ...

  3. Web应用体系结构

    容器 Servlet没有main()方法,它们受控于另一个Java应用,这个Java应用称为容器(Container).我们最常见的tomcat就是这样一个容器. Web服务器应用(如Apache)得 ...

  4. spring mvc: Hibernate验证器(字段不能为空,在1-150自己)

    spring mvc: Hibernate验证器(字段不能为空,在1-150自己) 准备: 下载Hibernate Validator库 - Hibernate Validator.解压缩hibern ...

  5. javascript 关键词 new都做了写什么

    翻译自stackoverflow:http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript ne ...

  6. 22.线程池之ScheduledThreadPoolExecutor

    1. ScheduledThreadPoolExecutor简介 ScheduledThreadPoolExecutor可以用来在给定延时后执行异步任务或者周期性执行任务,相对于任务调度的Timer来 ...

  7. 2-4-搭建DHCP服务实现动态分配IP地址-NTP网络时间同步

    本节所讲内容: •DHCP服务器工作原理 •使用DHCP为局域网中的机器分配IP地址 •使用DHCP为服务器分配固定IP地址 •ntpdate加计划任务同步服务器时间 ---------------- ...

  8. HDU-4355-三分

    Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. Spring3.0 核心jar包详解

    org.springframework.aop  包含在应用中使用Spring的AOP特性时所需的类. org.springframework.asm   Spring独立的ASM程序, Spring ...

  10. python:打包成exe程序

    1.需要安装 py2exe 2.示例代码: #exetest.py #创建一个gui界面,只用一个标签和按钮,无功能 from Tkinter import * win = Tk() label = ...