Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

Submit Status

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

——————————————————我是华丽丽的分割线——————————————————

差分约束系统题目。

约束条件如下:

设S[i]为集合Z中小于等于i的元素个数,即S[i]=|{s|s属于Z,s<=i}|。则:

(1) S[b(i)]-S[a(i-1)]>=c(i)   ------------> S[a(i-1)]-S[b(i)]<=-c(i)

(2) S[i]-S[i-1]<=1。

(3) S[i]-S[i-1]>=0,即S[i-1]-S[i]<=0.

设所有区间极右端点为mx,极左端点为mn 则 ans=min(s[mx]-s[mn-1]) 即求源点s[mx]与s[mn-1]之间的最短路 此题得解。

改进后方法如下: (1)先只用条件(1)构图,各顶点到源点的最短距离初始为0。 (2)即刻用Bellman-ford算法求各顶点到源点的最短路径。 p.s.在每次循环中,条件1判完后判断2和3.

2的判断: s[i]<=s[i-1]+1等价于s[i]-s[mx]<=s[i-1]-s[mx]+1 设dist[i]为源点mx到i的最短路径,则s[i]-s[mx]即为dist[i],s[i-1]-s[mx]+1即为dist[i-1]+1。 即若顶点i到源点的最短路径长度大于i-1到源点的最短路径长度+1,则dist[i]=dist[i-1]+1。

3的判断: s[i-1]<=s[i]等价于s[i-1]-s[mx]<=s[i]-s[mx] s[i]-s[mx]即为dist[i],s[i-1]-s[mx]即为dist[i-1]。 即若顶点i-1到源点的最短路径长度大于i到源点的最短路径长度,则dist[i-1]=dist[i]。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<vector>
#include<list>
#include<map>
#define maxn 1000001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
#define maxm 2016
#define mod 1000000007
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int lsh=inf,rsh;
struct EDGE
{
int from;
int to;
int value;
int next;
}e[maxn];
int head[maxn];
int tot;
inline void addedge(int u,int v,int w)
{
tot++;
e[tot].from=u;
e[tot].to=v;
e[tot].value=w;
e[tot].next=head[u];
head[u]=tot;
}
int f[maxn];
queue<int> q;
bool vis[maxn];
int d[maxn],in[maxn];
bool ford()
{
int i,t;
bool flag=true;
while(flag){
flag=false;
F(i,,n){
t=d[e[i].from]+e[i].value;
if(d[e[i].to]>t){
d[e[i].to]=t;
flag=true;
}
}
F(i,lsh,rsh){
t=d[i-]+;
if(d[i]>t){
d[i]=t;
flag=true;
}
}
FF(i,rsh,lsh){
t=d[i];
if(d[i-]>t){
d[i-]=t;
flag=true;
}
}
}
return true;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n;M(head,-);
F(i,,n){
int a,b,c;
cin>>a>>b>>c;
addedge(b,a-,-c);
if(lsh>a) lsh=a;
if(rsh<b) rsh=b;
}
ford();
cout<<d[rsh]-d[lsh-]<<endl;
return ;
}
/*
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
*/

poj 1201

poj 1201 Intervals 解题报告的更多相关文章

  1. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  2. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  3. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

  4. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  5. POJ 1201 Intervals(图论-差分约束)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Descri ...

  6. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  7. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  8. POJ 1201 Intervals【差分约束】

    传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...

  9. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 跟我一起学WPF(1):WPF的UI设计语言——XAML

    XAML是什么 XAML全称是Extensible Application Markup Language (可扩展应用程序标记语言),是专门用于WPF技术中的UI设计语言. XAML基础 XAML是 ...

  2. mysql插入数据时,去掉重复的数据;

    1. 利用insert ignore into语句去重 mysql> INSERT IGNORE INTO person_tbl (last_name, first_name) -> VA ...

  3. CSUOJ 1900 锋芒不露

    Description 小闪最近迷上了二刀流--不过他耍的其实是剑--新买了一个宝库用来专门存放自己收集的双剑.一对剑有两把,分只能左手用的和只能右手用的,各自有一个攻击力数值.虽然一对剑在小闪刚拿到 ...

  4. 大数据技术之_13_Azkaban学习_Azkaban(阿兹卡班)介绍 + Azkaban 安装部署 + Azkaban 实战

    一 概述1.1 为什么需要工作流调度系统1.2 常见工作流调度系统1.3 各种调度工具特性对比1.4 Azkaban 与 Oozie 对比二 Azkaban(阿兹卡班) 介绍三 Azkaban 安装部 ...

  5. FGPA 中的计数器Verilog语言(时钟分频器)

    在quartusII8.0中为ALTERAFPGA设置一个分频器(计数器) 输入时钟48Mhz 输出时钟9600HZ /* 实验名称: 计数器 ** 程序功能: 将48Mhz的时钟分频为9600Hz ...

  6. [python 源码]整数对象的创建和维护

    刚开始学python时候,发现一个很迷惑的现象,一直到看了源码后才知道了: >>> a=6 >>> b=6 >>> a is b True 想用同 ...

  7. [leetcode tree]101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. 韩梦飞沙Android应用集合 想法

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 那些收藏的图片 那些收藏的微博 那些收藏的音乐 定时短信 音乐列表汇 每天都是快乐的

  9. GNU C __attribute__ 机制简介

    摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了__attribute__相 ...

  10. 新的起点 Entry KINGSOFT

    夜里,陪宝宝睡了会,呃岁月转变,变化里,不经意间加入了kingsoft. 呃,第一天所以算是一个起点或是一个开始.遇到些琐事,Slow network,oa Account login O(∩_∩)O ...