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 ...
随机推荐
- 洛谷【P2431】正妹吃月饼
二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P24 ...
- 二、java 与 scala相互调用
介绍:scala 是简化的java,运行于jvm的脚步语言.Java和scala通过各自编译器编译过都是jvm能解析class文件.本文介绍java和scala如何互调 scala的源代码文件是以. ...
- openstack常见问题汇总
汇总下常见的问题以及解释下一些比较容易让人萌的参数配置等等 问题汇总1.使用纯文本模式进行复制粘贴,打死不要用word!!!可以解决绝大多数问题,如果你依然执迷不悟,那么就好自为之吧 2.创建路由器时 ...
- 一. kafka 入门
一.基本概念 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因素. K ...
- atoi函数实现
#include int my_atoi(const char *str) { int result; char sign; for (; str && isspace(*str); ...
- CSS之BFC详解
What:了解该知识点的概念,本质以及有关牵扯到的相关知识概念 BFC这个东西说常见的话你可能不觉得,但是你肯定会常用,也许你在用的时候也没想到BFC这东西.网上也有很多写这些东西的文章,但是自己写一 ...
- 关于startactivity初始化activity的过程以及activity和window以及view的关系
activity 构造一个实现了window的phonewindow,获得viewroot,然后往里面加入view 当发生事件的时候,如KEYDOWN,windowmanagerservice就接受到 ...
- 关于Android项目中,突然就R类找不到已存在的资源文件的解决方法
项目代码早上打开正常,下午开的时候突然提示R类找不到已存在的布局文件,于是试了各种方法,CLEAN啊,重启啊,均无效,然后去网上搜了下,遇到这个问题的人还不少. 看到其中有这么一条解决方法,删除导入的 ...
- 全文检索技术---solr
1 Solr介绍 1.1 什么是solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr可以独立运行在Jetty.Tom ...
- 2、java 启动参数
1.java启动参数共分为三类: 其一是标准参数 (-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:其二是非标准参数 (-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都 ...