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. USACO 6.1 A Rectangular Barn

    A Rectangular Barn Mircea Pasoi -- 2003 Ever the capitalist, Farmer John wants to extend his milking ...

  2. USACO 4.4 Pollutant Control (网络流求最小割割集)

    Pollutant ControlHal Burch It's your first day in Quality Control at Merry Milk Makers, and already ...

  3. 自动化CI构建工具

    hudson/maven jenkins,bamboo, hudson

  4. web中的相对路径与绝对路径

    1.什么叫绝对路径 相对于WEB应用的跟路径的路径,即任何路径都必须带上contentPath. 2.javaEE中的/代表什么 代表WEB应用的跟路径(需交由Servlet容器处理) 请求转发时. ...

  5. thinkphp条用函数与类库

    手册上说的很冗余,没看懂,下面简单的讲一下具体用法. 函数调用: lib公共函数库叫 common.php App/common/common.php 分组模块下的公共函数库叫 function.ph ...

  6. 牛客练习赛3 B - 贝伦卡斯泰露

    链接:https://www.nowcoder.net/acm/contest/13/B来源:牛客网 题目描述 贝伦卡斯泰露,某种程度上也可以称为古手梨花,能够创造几率近乎 为0的奇迹,通过无限轮回成 ...

  7. Linux signal 编程(转载)

    转载地址:http://blog.sina.com.cn/s/blog_4b226b92010119l5.html 当服务器close一个连接时,若client端接着发数据.根据TCP协议的规定,会收 ...

  8. CTF实验吧让我进去writeup

    初探题目 两个表单,我们用burp抓包试试 这时候我们发现Cookie值里有个很奇怪的值是source,这个单词有起源的意思,我们就可以猜测这个是判断权限的依据,让我们来修改其值为1,发送得到如下显示 ...

  9. [leetcode DP]70. Climbing Stairs

    一共有n个台阶,每次跳一个或者两个,有多少种走法,典型的Fibonacii问题 class Solution(object): def climbStairs(self, n): if n<0: ...

  10. Xamarin无法调试Android项目

    Xamarin无法调试Android项目   项目可以正常编译,生成APK,也可以通过右键菜单部署.但是一旦开启调试,就报错.错误信息如下: 没有为此解决方案配置选中要生成的项目   出现这种问题是因 ...