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 ...
随机推荐
- HTML实用案例(1)—— 左侧菜单,右侧内容的布局(带左侧菜单点击隐藏显示效果)
效果图 代码部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- Linux 下如何调试 Python?
一般开发者都是在 IDE 中进行程序的调试,当然,有 IDE 的话,当然首选 IDE 进行调试. 但是,有时我们的业务场景,限制只能在 Linux 命令行模式进行调试. 这时该怎么办呢? 今天,就给大 ...
- Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of th ...
- JavaWeb框架_Struts2_(三)---->Struts2的拦截器
2. Struts2的拦截器(使用拦截器实现权限控制) 2.1 拦截器的概述 拦截器是Struts2的核心组成部分,它可以动态的拦截Action调用的对象,类似与Servlet中的过滤器.Struts ...
- qq群文件管理
一.怎样登录QQ群空间查看.管理群文件 1)登录自己的QQ,打开主面板!小编在这里以访问自己的群“我们的六班”为例.2)鼠标移动到主面板中“我们的六班”群图标处,右建单击——选择“访问QQ群空间”—— ...
- git-SSH连接配置
1.1 在电脑端生成sshkey文件 打开git bash 输入: ssh-keygen -t rsa -C "xxxxxxx邮箱地址" 期间要输入两次密码[github远程添加s ...
- MLCC 电容的的 NP0 C0G 材质
MLCC 电容的的 NP0 C0G 材质 随手记一下. MLCC 中最稳定的材质 NP0 C0G,NP0 和 C0G 是相同的,只是不同的产商不同的名字而已. 注意中间的是 0 不是 英文字母 O,虽 ...
- PHP根据两点间的经纬度计算距离,php两点经纬度计算(转)
这是一个不错的示例,直接贴代码,首先要知道纬度值.经度值 /** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经 ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...
- Angular5学习笔记 - 服务优化(十)
一.服务合并 二.验证效果