POJ1201 Intervals【差分约束系统】
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【差分约束系统】的更多相关文章
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
随机推荐
- 安装webpack命令环境
1.通过cnpm安装webpack命令环境,如图 2.安装完后查看webpack的版本,如图
- Mysql(基础篇)
linux下的mysql操作 1.# 打开 MySQL 服务 sudo service mysql start 2.#使用 root 用户登录,密码为空 mysql -u root 3.创建数据库 C ...
- Oracle归档的开启和关闭
--1.开启归档 [步骤] a.一致性关闭数据库(shutdown [immediate | transactional |normal]) b.启动到mount阶段(startup mount) c ...
- 奇怪的表达式求值 (java实现)
题目参考:http://blog.csdn.net/fuxuemingzhu/article/details/68484749 问题描述; 题目描述: 常规的表达式求值,我们都会根据计算的优先级来计算 ...
- C++复习9.面向对象编程
C++ 面向对象编程概述 20131001 一些基本概念:封装.继承.组合.虚函数.抽象基类.动态绑定.多态性等等 1.一个笑话:如果坐在后排聊天的同学能够像中间打牌的同学那样安静的话,那么就不会影响 ...
- 【2018 “百度之星”程序设计大赛 - 初赛(B)- 1001】degree
Problem Description 度度熊最近似乎在研究图论.给定一个有 N 个点 (vertex) 以及 M 条边 (edge) 的无向简单图 (undirected simple graph) ...
- HtmlHelper.Raw,<%%>,<%:%>,<%=%>的区别及使用
Mvc中<%%>,<%:%>,<%=%>的区别及使用 1.<%%> <%%>之间可以执行服务端代码,如<% foreach (Data ...
- ES获取磁盘使用率情况
private void diskUage() { ClusterStateResponse stateResponse = client.admin().cluster().prepareState ...
- VPS安装metasploit-framework
一.安装过程 在/etc/apt/sources.list添加kali源: root@localhost:~# cat >> /etc/apt/sources.list << ...
- 20165202 2017-2018-2《Java程序设计》课程总结
每周作业链接汇总 ++预备作业一:我期待的师生关系++ ++预备作业二:学习基础和C语言基础调查++ ++预备作业三:linux安装及学习++ ++第一周作业:初识JAVA,注册码云并配置Git++ ...