Intervals POJ - 1201 差分约束
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
<= 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
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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ struct node {
int u, v, w;
int nxt;
}edge[maxn];
int head[maxn];
int tot;
int dis[maxn];
bool vis[maxn];
void addedge(int u, int v, int w) {
edge[++tot].u = u; edge[tot].v = v; edge[tot].w = w;
edge[tot].nxt = head[u]; head[u] = tot;
}
void spfa() {
queue<int>q;
q.push(0);
vis[0] = 1; dis[0] = 0;
while (!q.empty()) {
int u = q.front(); q.pop(); vis[u] = 0;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
if (dis[v] < dis[u] + edge[i].w) {
dis[v] = dis[u] + edge[i].w;
if (!vis[v]) {
q.push(v); vis[v] = 1;
}
}
}
}
} int main() {
//ios::sync_with_stdio(0);
// int T; rdint(T);
// while (T--) {
ms(head); memset(dis, -0x3f, sizeof(dis));
ms(vis); tot = 0;
int maxx = 0; int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b, c; rdint(a); rdint(b); rdint(c);
addedge(a, b + 1, c); maxx = max(maxx, b + 1);
}
for (int i = 1; i <= maxx; i++) {
addedge(i, i - 1, -1); addedge(i - 1, i, 0);
}
spfa();
cout << dis[maxx] << endl;
// if (T)cout << endl;
// }
return 0;
}
Intervals POJ - 1201 差分约束的更多相关文章
- poj 1201 差分约束
http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...
- POJ 1201 差分约束+SPFA
思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...
- POJ 1201 差分约束(集合最小元素个数)
题意: 给你一个集合,然后有如下输入,a ,b ,c表示在范围[a,b]里面有至少有c个元素,最后问你整个集合最少多少个元素. 思路: 和HDU1384一模一样,首先这个题目可 ...
- Intervals poj 1201 差分约束系统
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22503 Accepted: 8506 Descri ...
- POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- POJ - 3169 差分约束
题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如 果最大距离无限大则输出 ...
- POJ 1201 Intervals(图论-差分约束)
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20779 Accepted: 7863 Descri ...
随机推荐
- Java 实现 JS的eval函数
JS的eval 函数, 给个表达式做参数, 返回表达式的值. Java的脚本引擎可以实现这个功能. 例子: 拼接一个字符串 \uxxxx, Unicode的十六进制编码, 然后把它打印出来. 即输 ...
- notepad++如何移除重复的行
Removing duplicate rows in Notepad++ (so链接) 1. 插件 TextFX 2. 正则表达式:^(.*?)$\s+?^(?=.*^\1$)
- Struts2 - 配置文件详解
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- php断点续传
http://www.cnblogs.com/xproer/archive/2012/10/26/2741264.html
- noip退役赛
上下午 6 题一起考 自闭了 T1 小明要参加一场比赛,赛制给你一个表格 $p$ ,$p_{(i,j)}$ 表示他在第 $i$ 场比赛前如果输了 $j$ 场,他这一场赢的概率,他也可以故意输掉任意多场 ...
- 左右选择框 js插件
随着项目的进展,测试工程师在更多的浏览器中兼容性测试中,发现有些浏览器不支持option的触发事件,这就造成了先前一篇博文bootstrap 左右框多项选择示例 中左右选择框的失效,于是我就由原先的s ...
- bzoj 2969: 矩形粉刷 概率期望
题目: 为了庆祝新的一年到来,小M决定要粉刷一个大木板.大木板实际上是一个W*H的方阵.小M得到了一个神奇的工具,这个工具只需要指定方阵中两个格子,就可以把这两格子为对角的,平行于木板边界的一个子矩形 ...
- C# 利用Xsd验证xml
最近做项目时,用到了xml的序列化与反序列化, 发现最好用xsd来验证xml, 因为反序列化xml不校验xsd. 方法:xmlData变量为xml字符串 MemoryStream ms = new M ...
- 使用jquery扩展表格行合并方法探究
1.前言 最近项目中用到一个表格中对于相同内容的数据进行行合并的需求,本来想从网上找个现成的,省的自己再造轮子.于是就开始谷歌了...不过在搜索的过程中,发现找到的工具类很多都有一个前提,就是该表格中 ...
- Asp.net Core学习笔记
之前记在github上的,现在搬运过来 变化还是很大的,感觉和Nodejs有点类似,比如中间件的使用 ,努力学习ing... 优点 不依赖IIS 开源和跨平台 中间件支持 性能优化 无所不在的依赖注入 ...