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 ...
随机推荐
- Spring Boot 简单日志配置
在生产环境中,只打印error级别的错误,在测试环境中,可以调成debugapplication.properties文件## 默认使用logback logging.level.root=error ...
- ps6-工具的基础使用
1.图像的移动与对齐 ctrl+j:复制图层,然后再移动不损坏原来的图像. Ctrl+Z =返回键 Shift+单击最下方图层 选择全部 Alt+鼠标移动 复制并粘贴 2.规则选择工具组 shift键 ...
- hdu5542 The Battle of Chibi[DP+BIT]
求给定序列中长度为M的上升子序列个数.$N,M<=1000$. 很容易想到方法.$f[i,j]$表示以第$i$个数结尾,长度为$j$的满足要求子序列个数.于是转移也就写出来了$f[i][j]+= ...
- Hive的JDBC访问
实现hive查询源码: String driverName = "org.apache.hive.jdbc.HiveDriver"; try { Class.forName(dri ...
- AFN 请求数据https
第一步: 导入afn库 第二步: 在pch中添加 #import <SystemConfiguration/SystemConfiguration.h> #import <Mobil ...
- 基于TCP协议 I/O多路转接(select) 的高性能回显服务器客户端模型
服务端代码: myselect.c #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> ...
- HDU4825(字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- Java基础--枚举Enum
Java中的枚举是一种特殊的类,可以将一组固定常量的集合组成一种类型,使用方便且类型安全.使用enum关键字定义. enum类型父类为Enum,通过Enum.class可见Enum为抽象类,实现了Co ...
- Windows 7 下将 Tomcat Java 程序设置为 Windows Service
方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...
- Contiki学习笔记
http://blog.chinaunix.net/uid-9112803-id-2975824.html