Intervals
Intervals |
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 52 Accepted Submission(s): 32 |
Problem 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 endpoints 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 <= 50 000) - 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 <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file. |
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 |
Sample Output
6 |
Author
1384
|
Recommend
Eddy
|
/*
题意:给n个条件 ai bi ci 表示在[ai,bi]最少取ci个数,问你最少取多少点,才能满足这些条件 初步思路:差分约束问题,差分约束问题,实际上就是利用图论的知识计算不等式,每个不等式建立一条边,然后利用最短路
跑一下
*/
#include<bits/stdc++.h>
using namespace std;
int u,v,w,n;
/*****************************************************spaf模板*****************************************************/
template<int N,int M>
struct Graph
{
int top;
struct Vertex{
int head;
}V[N];
struct Edge{
int v,next;
int w;
}E[M];
void init(){
memset(V,-,sizeof(V));
top = ;
}
void add_edge(int u,int v,int w){
E[top].v = v;
E[top].w = w;
E[top].next = V[u].head;
V[u].head = top++;
}
}; Graph<,> g; const int N = 5e4 + ; int d[N];//从某一点到i的最短路
int inqCnt[N]; bool inq[N];//标记走过的点 bool spfa(int s,int n)
{
memset(inqCnt,,sizeof(inqCnt));
memset(inq,false,sizeof(inq));
memset(d,-,sizeof(d));
queue<int> Q;
Q.push(s);//将起点装进队列中
inq[s] = true;
d[s] = ;
while(!Q.empty())
{
int u = Q.front();
for(int i=g.V[u].head;~i;i=g.E[i].next)//遍历所有这个点相邻的点
{
int v = g.E[i].v;
int w = g.E[i].w;
if(d[u]+w>d[v])//进行放缩
{
d[v] = d[u] + w;
if(!inq[v])//如果这个点没有遍历过
{
Q.push(v);
inq[v] = true;
if(++inqCnt[v] > n)
return true;
}
}
}
Q.pop();//将这个点出栈
inq[u] = false;
}
return false;
}
/*****************************************************spaf模板*****************************************************/
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
g.init();
int L=,R=;
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
++u,++v;
//找出左右两个边界
L=min(L,u);
R=max(R,v);
g.add_edge(u-,v,w);
}
for(int i=L;i<=R;i++) {
g.add_edge(i-,i,);
g.add_edge(i,i-,-);
}
spfa(L-,R-L+);
printf("%d\n",d[R]); }
return ;
}
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 ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
随机推荐
- shell脚本获取文件中key/value的小方法
方法有N种,awk.sad.grep.cut... 以上几种方式不写了,就写两个不太常用到的. 废话少说,直接上代码: cat a.txt aa.gif=aaaa.gif bb.gif=bbbb.gi ...
- Hadoop安全(2)——————UserGroupInformation
UserGroupInformation保存hadoop用户及组信息 此类包装JAAS Subject,并提供确定用户的用户名和组的方法.它支持Windows,Unix和Kerberos登录模块.
- Javascript 中 ==(相等运算符) 和 ===(严格相等运算符) 区别
在JS中,"==="叫做严格运算符,"=="叫做相等运算符. 它们的区别是相等运算符(==)比较两个值是否相等,严格相等运算符(===)比较它们是否为" ...
- html表格宽度设置失效
问题描述: 我在写一个网页table时,table宽度超过了我预想的宽度,我想把它设置小一点,但总是没效果.改到怀疑人生!代码如下: 经过多次调试后发现一个问题,table可以改变大小,但是会有一个最 ...
- 集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序
在2015年8月份的时候,决心学习图像算法. 几乎把当时市面上的图像算法相关书籍都看了一遍, 资金有限,采取淘宝买二手书,长期驻留深圳图书馆的做法, 进度总是很慢,学习算法不得其法. 虽然把手上所有书 ...
- 再起航,我的学习笔记之JavaScript设计模式30(简单模板模式)
简单模板模式 概念介绍 简单模板模式(Simple template): 通过格式化字符串拼凑出视图避免创建视图时大量节点操作,优化内存开销. 创建模板 在实际的业务中如果我们需要进行前后台交互,或多 ...
- Canal 同步异常分析:Could not find first log file name in binary log index file
文章首发于[博客园-陈树义],点击跳转到原文Canal同步异常分析:Could not find first log file name in binary log index file. 公司搜索相 ...
- 使用路由延迟加载 Angular 模块
使用路由延迟加载 Angular 模块 Angular 非常模块化,模块化的一个非常有用的特性就是模块作为延迟加载点.延迟加载意味着可以在后台加载一个模块和其包含的所有组件等资源.这样 Angular ...
- Python调用C# Com dll组件实战
之前公司有套C# AES加解密方案,但是方案加密用的是Rijndael类,而非AES的四种模式(ECB.CBC.CFB.OFB,这四种用的是RijndaelManaged类),Python下Crypt ...
- java web Servlet学习笔记-2 请求重定向和请求转发的区别
请求转发与请求重定向的区别 请求重定向和转发 1.请求重定向:浏览器的行为(通过响应对象HttpServletResponse来执行) 特点:可以重新定向访问其他Web应用下的资源 浏览器发出了2次请 ...