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. Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings

    F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...

  2. Kylin启动时错误:Failed to find metadata store by url: kylin_metadata@hbase 解决办法

    一.问题背景 安装kylin后使用命令 $ kylin.sh start 后出现Failed to find metadata store by url: kylin_metadata@hbase的错 ...

  3. 解决命令行运行python文件,出现No module named *** 报错问题

    有时候在一个项目中运行的时候,可能是之前已经mark成sources root 你自己忘记了, 于是就在命令行也执行python文件,然后就出现 No module named *** 等 相关你认为 ...

  4. 使开发更便捷——Visual Studio 使用技巧——快捷键

    下面是.Net开发中常用的快捷键,熟练使用可以提高开发效率: Ctrl + K + C //注释代码 Ctrl + K + U //取消代码注释 Ctrl + k + d //快速格式化代码 Shif ...

  5. 转 Java高级程序员面试题

    1.你认为项目中最重要的过程是那些? 分析.设计阶段  尽量找出进度的优先级 2.如果给你一个4-6人的team,怎么分配? 挑选一技术过硬的人作为我的替补.其它人平均分配任务,每周进行全面的任务分配 ...

  6. 基于特征码文件恢复工具magicrescue

    基于特征码文件恢复工具magicrescue   常见类型的文件都包含一些特殊的字节,用来标识文件的类型.这些字节被称为特征码.在磁盘中,当记录文件存储位置的簇损坏后,就可以基于这些特征码来恢复文件. ...

  7. 用Python开始机器学习(3:数据拟合与广义线性回归)

    机器学习中的预测问题通常分为2类:回归与分类. 简单的说回归就是预测数值,而分类是给数据打上标签归类. 本文讲述如何用Python进行基本的数据拟合,以及如何对拟合结果的误差进行分析. 本例中使用一个 ...

  8. bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp

    给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...

  9. PAT(Basic Level)--个位数统计

    输入一个不超过1000位的整数,计算每个数字出现的次数. 一道十分简单的题目,最开始以为Java的String没有计算长度的方法,还想了半天,而且还用HashMap做了一次,代码特别长,看了别人的代码 ...

  10. service redis does not support chkconfig 的解决办法

    问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...