【POJ 1201 Intervals】
Time Limit: 2000MS
Meamory Limit: 65536K
Total Submissions: 27949
Accepted: 10764
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
Source
【题解】
①这是一个差分约束模型。
②本题约束条件基于前缀和sum数组。
③外加相邻点的前缀和关系(最多相差1)。
#include<queue>
#include<stdio.h>
#include<algorithm>
#define inf 1000000007
#define go(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v)
const int N=50010;
using namespace std;
bool inq[N];queue<int>q;
struct E{int v,next,w;}e[N<<1];
int n,a[N],b[N],c[N],head[N],k=1,S=1e9,T,d[N];
void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;} void Build()
{
go(i,1,n)scanf("%d %d %d",a+i,b+i,c+i);
go(i,1,n)S=min(S,a[i]-1),T=max(T,b[i]);
go(i,1,n)ADD(a[i]-1,b[i],c[i]);
go(i,S+1,T)ADD(i,i-1,-1);
go(i,S,T-1)ADD(i,i+1,0);
} void SPFA()
{
go(i,S,T)d[i]=-inf;d[S]=0;q.push(S);int u;
while(!q.empty()){inq[u=q.front()]=0;q.pop();fo(i,head,u)
if(d[u]+e[i].w>d[v])d[v]=d[u]+e[i].w,!inq[v]?q.push(v),inq[v]=1:1;}
} int main()
{
scanf("%d",&n);Build();
SPFA();printf("%d\n",d[T]);
return 0;
}//Paul_Guderian
.
【POJ 1201 Intervals】的更多相关文章
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- poj 1201 Intervals 解题报告
Intervals Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Submit Statu ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- 【38.24%】【POJ 1201】Intervals
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25902 Accepted: 9905 Description You are ...
- 【POJ 1201】 Intervals
[题目链接] 点击打开链接 [算法] 令sum(n)表示区间[1,n]中选了几个点 那么,显然有以下不等式 : 1. sum(n)- sum(n - 1) >= 0 2. sum(n) - s ...
- POJ 1201 Intervals【差分约束】
传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...
- 【题解】 POJ 1201 Intervals(差分约束)
懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...
- poj 1201 Intervals【差分约束+spfa】
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
随机推荐
- 构造HTTP请求Header实现“伪造来源IP”
在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/IP 协议中,可以伪造数据包来源 IP ,但这会让发送出去的数据包有去无回,无 ...
- Python学习手册之数据类型
在上一篇文章中,我们介绍了 Python 的异常和文件,现在我们介绍 Python 中的数据类型. 查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/99799 ...
- PAT (Basic Level) Practice (中文)1002
1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...
- 关于 js 对象 转 字符串 和 深拷贝 的探讨
随着更多语言的支持 **json** 作为数据传输和存储的媒体,已经非常成熟且应用广泛.却存在致命硬伤,不携带 **对象方法** .在数据传输和存储中,这是恰当的和合理的. 但是在更多的应用场景中,又 ...
- BLE(Bluetooth Low Energy)---first part
原文地址:https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#terms (本人是技术宅,翻译时候,只要 ...
- PADS9.5打开Altium designer09的原理图
1. 打开PADS Logic原理图工具,文件---导入 2. 选择Protel DXP这个选项,找到相应的文件即可打开.
- php杂记——1(基础知识与文件读写)
1.变量前面需要加美元符号"$",常量则不需要: define('PRICE',100); echo PRICE; 2.用一个变量的值作为另一个变量的名称可以得到类似C中的指针变量 ...
- javascript的优美与鸡肋
--总结来自:<javascript语言精粹> 任何语言都有其优美的地方和其鸡肋的地方.避归一些语言的糟粕,能相应的降低bug出现的几率. 优美处: 函数是头等对象 基于原型继承的动态对象 ...
- Java byte 位移操作 注意事项
转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...
- JavaScript RegExp 身份证、账号密码、email正则
什么是正则表达式 正则表达式是构成搜索模式. 在文本中搜索数据时,可以使用此搜索模式来描述正在搜索的内容. 正则表达式可以是单个字符,也可以是更复杂的模式. 正则表达式可用于执行所有类型的文本搜索和文 ...