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 ...
随机推荐
- unity3D写一个hello world
unity3D写一个hello world 打开unity并且在assets建立一个新的文件,新的文件命名为hello world.unity.接着创建一个新的C#Sript脚本文件,命名为hello ...
- 协议端口号(protocol port number)
协议端口号(protocol port number) 先来个注意事项 (-> ->) 这种在协议层间的抽象的协议端口是软件端口,和硬件端口是完全不同的概念.硬件端口是不同设备进行交互的接 ...
- SSM之框架整合
前言 SSM框架,即Spring + Spring MVC + MyBatis的整合框架集,是继SSH后比较主流的Java EE企业级框架,采用标准的MVC模式,项目结构与微软的ASP.NET MVC ...
- vue-ajax小封装
1. js 文件: /** ajax封装:* 1. 引入文件* 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url, ...
- VacmMIB
VACM 基于视图的访问控制模型 是SNMPV3对MIB中被管对象的访问进行控制的模型 特点: 1.VACM 确定是否允许用户访问本地MIB的被管理对象.当用户请求消息到达代理的命令响应器时,命令响 ...
- Android开发更新UI的几种方式
1.runOnUiThread 2.handler post 3.handler sendmessage 4.view post xml布局文件: <RelativeLayout xmlns:a ...
- Zabbix(二) : Zabbix Server端配置文件说明
Zabbix Server端配置文件说明 # This is a configuration file for Zabbix Server process # To get more informat ...
- express简介
Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具. 使用 Express 可以快速地搭建一个完整功能的网 ...
- SpringMVC框架(一)
SpringMVC最核心:DispatcherServlet SpringMVC环境搭建: 结构: 过程: 1.导包 2.声明SpringMVC核心Servlet:org.springframewor ...
- localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车
localStorage.setItem("key","value");//存储变量名为key,值为value的变量 localStorage.key = &q ...