Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27746   Accepted: 10687

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

差分约束系统是线性规划中的一种,在一个差分约束系统中,可以看成一个矩阵乘以一个向量小于另一个向量,求其中向量两个坐标的距离关系,约束条件对的不等式和单元最短路的松弛操作十分类似!

抽象出节点,根据节点性质和题目信息建边,最短路即可。

注意一定<=建边!

如果出现负权回路说明无解!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//[a,b]区间内至少有c个数在集合内,问集合最少包含多少点
//a,b 可以取0 再读入的时候手动 a++,b++
//定义ti 为[0,i]内至少有多少个数字,那么由ta-1 - tb <= -c
//由ti的定义可以推出它的性质1.ti-ti+1<=0 ti+1-ti<=1 const int MAXM = * + ;
const int MAXN = + ; struct edge
{
LL to, next, dis;
}E[MAXM];
LL head[MAXN],tot;
LL dist[MAXN];
bool vis[MAXN];
void init()
{
tot = ;
memset(head, -, sizeof(head));
}
void spfa(LL ed)
{
memset(dist, 0x3f3f3f3f, sizeof(dist));
memset(vis, false, sizeof(vis));
queue<LL> q;
q.push();
vis[] = true;
dist[] = ;
while (!q.empty())
{
LL f = q.front();
q.pop();
vis[f] = false;
for (LL i = head[f]; i != -; i = E[i].next)
{
LL v = E[i].to, d = E[i].dis;
if (dist[v] > dist[f] + d)
{
dist[v] = dist[f] + d;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
cout << -dist[ed] << endl;
}
void addedge(LL u, LL v, LL d)
{
E[tot].to = v;
E[tot].dis = d;
E[tot].next = head[u];
head[u] = tot++;
}
int main()
{
ios::sync_with_stdio();
init();
LL f, t, d, ed;
LL n;
cin >> n;
while (n--)
{
cin >> f >> t >> d;
f++, t++;
addedge(f - , t, -d);
ed = max(t, ed);
}
for (int i = ; i < ed; i++)
{
addedge(i, i + , );
addedge(i + , i, );
}
spfa(ed);
}

最新文章

  1. 结对编程--基于android平台的黄金点游戏(2.0版本)
  2. tomcat 启动参数 Xms, Xmx, XX:MaxNewSize, XX:PermSize, -XX:MaxPermSize, Djava.awt.headless
  3. [oracle] 设置PL/SQL Developer 字符集
  4. jQuery实现登录提示
  5. windows官方多语言方案
  6. Centos 添加Root用户
  7. &lt;正向/反向&gt;最大匹配算法(Java)
  8. 如何在cocos2d项目中enable ARC
  9. 上帝的归上帝,凯撒的归凯撒—— CODING 权限管理更新
  10. Python爬虫入门教程 47-100 mitmproxy安装与安卓模拟器的配合使用-手机APP爬虫部分
  11. How To Configure NetScaler AppFlow for SolarWinds
  12. android中Imageview的布局和使用
  13. 输入法设置,SublimeTest,putty掉线
  14. javascript 通过模块模式实现代码访问控制
  15. optimizer_mode优化器模式
  16. Chapter 3 Phenomenon——13
  17. 指向NULL的类
  18. 使用cursor递归遍历sqlserver的相应表
  19. uboot的FIT功能
  20. Python学习札记(十八) 高级特性4 生成器

热门文章

  1. ReactJS-0-React介绍
  2. GCC的函数声明问题
  3. 机器学习-Probabilistic interpretation
  4. webpack3整理(第二节/满三节)
  5. vba,设置,excel,wps ,页面设置例子
  6. WPF学习- AllowDrop 用户控件启用拖放功能
  7. vue工程化之项目引入jquery
  8. OpenGL C#绘图环境配置
  9. python3+beautifulSoup4.6抓取某网站小说(三)网页分析,BeautifulSoup解析
  10. 【讲●解】火车进出栈类问题 &amp; 卡特兰数应用