loj10087 Intervals
分析
我们设S[i]表示到第i个数为止一共有多少个数在集合Z之中,最终答案就是S[max]-S[min]的最小值。所以我们不难发现对于每一个[ai,bi]都表示S[bi]-S[ai-1]>=ci,而我们又知道0<=S[i]-S[i-1]<=1,所以建图策略便是每一个ai向bi连一条权值为ci的边,i-1向i连一条权值为0的边,i向i-1连一条权值为-1的边,然后跑最长路就可以了。注意在代码中为了方便起见所有的值i向后移了一位。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
vector<pair<int,int> >v[];
int d[],iq[];
queue<int>q;
inline void spfa(int s){
memset(d,-,sizeof(d));
memset(iq,,sizeof(iq));
q.push(s);d[s]=;iq[s]=;
while(!q.empty()){
int x=q.front();
q.pop();iq[x]=;
for(int i=;i<v[x].size();i++){
int y=v[x][i].first,z=v[x][i].second;
if(d[y]<d[x]+z){
d[y]=d[x]+z;
if(!iq[y]){
q.push(y);
iq[y]=;
}
}
}
}
return;
}
int main(){
int n,m,i,j,k,s=inf,t=-;
scanf("%d",&n);
for(i=;i<=n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
y++;
v[x].push_back(make_pair(y,z));
s=min(s,x),t=max(t,y);
}
for(i=s;i<t;i++){
v[i].push_back(make_pair(i+,));
v[i+].push_back(make_pair(i,-));
}
spfa(s);
printf("%d\n",d[t]);
return ;
}
loj10087 Intervals的更多相关文章
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- Understanding Binomial Confidence Intervals 二项分布的置信区间
Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- Merge Intervals 运行比较快
class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...
- [LeetCode] 435 Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
随机推荐
- Unity自带寻路Navmesh
自带寻路Navmesh的三大组件: 1.Nav Mesh Agent:主要挂在寻路物体上 2.Off Mesh Link:实现区域转移功能(例如,有时不一定只是在地面上进行寻路,可能有些高高的平台,平 ...
- metaclass 了解一下
创建类的两种方式 方式一: class Foo(object,metaclass=type): CITY = "bj" def func(self,x): return x + 1 ...
- MyISAM引擎的特点及优化方法
1.什么是MyISAM引擎?MyISAM引擎是MySQL关系数据库管理系统的默认存储引擎(MySQL5.5.5以前),这种MySQL的表存储结构从旧的ISAM代码扩展出许多有用的功能.在存储的时候,每 ...
- stack容器
一.stack特性 stack是一种先进后出(first in last out,FILO)的数据结构,它只有一个出口,stack只允许在栈顶新增元素,移除元素,获得顶端元素,但是除了顶端之外,其他地 ...
- OTL调用Oracle存储过程
OTL很早前用过,今天写东西要调存储过程,程序写完了,调试死活通不过,折腾了一早晨. 最后才发现错误,这里总结一下: 1.代码写的不规范. 有个参数后边少写了个“,”以至于总是抱错.而单独写的测试例子 ...
- 为什么很多公司招聘前端开发要求有 Linux / Unix 下的开发经验?
知乎: http://www.zhihu.com/question/19666395
- Jeesite开垦
1. userIndex里面,$ctx是在哪里定义的? 就是request.context 2. 增加新的包后,扫描配置修改1) spring-context.xml文件中,扫描非@controlle ...
- hdu 1028 && hdu 1398 && hdu 1085 && hdu 1171 ——生成函数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1028 就是可以用任意个1.2.3....,所以式子写出来就是这样:(1+x+x^2+...)(1+x^2+ ...
- 【转】js获取对象的所有属性和方法
//有时候需要知道一个js对像的所有属性和方法来帮助调试,下面是再网上找到的一个比较给力的方法 function displayProp(obj){ var names=""; f ...
- c语言-构建一个静态二叉树
第一.树的构建 定义树结构 struct BTNode { char data; struct BTNode* pLChild; struct BTNode* pRChild; }; 静态方式创建一个 ...