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 ...
随机推荐
- [UIKit学习]06.懒加载,模型,自定义代码段,重写构造方法
懒加载 在get中加载,且只加载一次 - (NSArray *)shops { if (_shops == nil) { NSString *file = [[NSBundle mainBundle] ...
- macbook 263企业邮箱设置
第一步:打开邮箱,点击添加账号,选择其他 第二步:填写完整的电子邮件地址和密码 第三步:填写收件服务器(popcom.263xmail.com),发件服务器(smtpcom.263xmail.com)
- (BCB)C++ Builder 6.0在Win7下如何使用帮助Help
因为Win7.Win8.Win8.1不再支持.hlp帮助类型,在BCB中无法打开帮助Help,打开时出错:"不包括功能"或"不受支持的帮助",提示如下: 为何无 ...
- 【OOM】GC overhead limit exceeded
我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...
- Sublimetext3安装Emmet插件步骤
看清楚哦~~这是Sublime text 3不是2的版本,两者的安装还是有区别的,下面的方法是我感觉比较简单的,其他的要命令什么的感觉太复杂了,经测试是OK的. 先关闭Sublime text 3: ...
- git添加比较和合并工具(meld)
git 下的(difftool)和(mergetool)是专门提供给使用者用自己的工具进行diff和merge的命令: # git config --global diff.tool meld # g ...
- Ubuntu16.04 install jdk-8u144-linux-x64.tar.gz
打开终端: Ctrl+Alt+T 下载jdk: wget http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b ...
- Linq常见操作示例
static void DeferredQuery() { var names = new List<string> { "Nino", "Alberto&q ...
- sqlserver常用sql语句,更改字段,建立唯一键,多个字段去重复等
--修改字段类型: --alter table 表名 alter column 待修改字段名 待修改字段类型 alter table users alter column userid varchar ...
- C#调用存储过程带输出参数或返回值
CREATE PROCEDURE [dbo].[GetNameById] @studentid varchar(8), @studentname nvarchar(50) OUTPUT AS BEGI ...