Intervals
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24134   Accepted: 9177

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
差分约束系统模板题;转换为最短路来求解,关键是建图;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
void spaf(int n,int ans);
using namespace std;
const int N=1e9;
int d[30005*5];
bool flag[30005*5];
void add(int x,int y,int z,int co);
typedef struct pp
{
int x;
int y;
int cost;
int pre;
}ss;ss aa[30005*5];
int id[30005*5];
int main(void) {
int i,j,k,p,q;int z;
int maxx=0;
while(scanf("%d",&k)!=EOF) {int ans=0;
fill(id,id+30005*5,-1);
while(k--) {
scanf("%d %d %d",&p,&q,&z);
if(maxx<p)maxx=p;
if(maxx<q)maxx=q;
add(p-1,q,ans++,-z);
}
for(i=1; i<=maxx; i++) {
add(i-1,i,ans++,0);
add(i,i-1,ans++,1);
}spaf(0,maxx);printf("%d\n",-d[maxx]);
}
} void spaf(int n,int ans) {
fill(d,d+30005*5,N);
d[n]=0;
queue<int>que;int i;
memset(flag,0,sizeof(flag));
flag[n]=true;
que.push(n);
while(!que.empty()) {
int c=que.front();
que.pop();
flag[c]=false;
int x=id[c];
while(x!=-1)
{
int uu=aa[x].y;
if(d[uu]>d[c]+aa[x].cost)
{d[uu]=d[c]+aa[x].cost;
if(!flag[uu])
{que.push(uu);
flag[uu]=true;
}
}
x=aa[x].pre;
} }
}
void add(int x,int y,int z,int co)
{
aa[z].x=x;
aa[z].y=y;
aa[z].cost=co;
aa[z].pre=id[x];
id[x]=z;
}
Integer Intervals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13511   Accepted: 5756

Description

An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

Input

The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #include<queue>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 void spaf(int n,int ans);
9 using namespace std;
10 const int N=1e9;
11 int d[30005];
12 bool flag[30005];
13 void add(int x,int y,int z,int co);
14 typedef struct pp
15 {
16 int x;
17 int y;
18 int cost;
19 int pre;
20 }ss;ss aa[30005];
21 int id[30005];
22 int main(void) {
23 int i,j,k,p,q;
24 int maxx=0;
25 while(scanf("%d",&k)!=EOF) {int ans=0;
26 fill(id,id+30005,-1);
27 while(k--) {
28 scanf("%d %d",&p,&q);
29 p++;
30 q++;
31 if(maxx<p)maxx=p;
32 if(maxx<q)maxx=q;
33 add(p-1,q,ans++,-2);
34 }
35 for(i=1; i<=maxx; i++) {
36 add(i-1,i,ans++,0);
37 add(i,i-1,ans++,1);
38 }spaf(0,maxx);printf("%d\n",-d[maxx]);
39 }
40 }
41
42 void spaf(int n,int ans) {
43 fill(d,d+30005,N);
44 d[n]=0;
45 queue<int>que;int i;
46 memset(flag,0,sizeof(flag));
47 flag[n]=true;
48 que.push(n);
49 while(!que.empty()) {
50 int c=que.front();
51 que.pop();
52 flag[c]=false;
53 int x=id[c];
54 while(x!=-1)
55 {
56 int uu=aa[x].y;
57 if(d[uu]>d[c]+aa[x].cost)
58 {d[uu]=d[c]+aa[x].cost;
59 if(!flag[uu])
60 {que.push(uu);
61 flag[uu]=true;
62 }
63 }
64 x=aa[x].pre;
65 }
66
67 }
68 }
69 void add(int x,int y,int z,int co)
70 {
71 aa[z].x=x;
72 aa[z].y=y;
73 aa[z].cost=co;
74 aa[z].pre=id[x];
75 id[x]=z;
76 }

Intervals(poj1201)的更多相关文章

  1. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...

  2. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. 1089 Intervals(中文版)

    开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思. 试题描述 输入一个n,然后输入n组数据,每个数据有两个数,代表这个闭区间是从几到几.然后看,如果任意两个闭区间有相重 ...

  4. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  5. [LeetCode]题解(python):056-Merge Intervals

    题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...

  6. poj1716 Integer Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Integer Intervals Time Limit: 1000MS   Me ...

  7. POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...

  8. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  9. 【POJ 1201】 Intervals(差分约束系统)

    [POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: ...

随机推荐

  1. 字符串String的trim()方法

    用来删除字符串两端的空白字符并返回,trim方法并不影响原来的字符串本身,它返回的是一个新的字符串 String a = "  Hello World  "; String b = ...

  2. 【MarkDown】--使用教程

    MarkDown使用教程 目录 MarkDown使用教程 一. 常用设置 1.1 目录 1.2 标题 1.3 文本样式 (1)引用 (2)高亮 (3)强调 (4)水平线 (5)上下标 (6)插入代码 ...

  3. ListView的item不能点击(焦点冲突问题)

    一般这种问题就是item里面有checkbox或button之类抢占焦点的控件,解决方案有2种: 第一种:就是在checkbox或button添加android:focusable="fal ...

  4. jquery的each和js原生for循环性能对比

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  5. 【Linux】【Services】【SaaS】Docker+kubernetes(12. 部署prometheus/grafana/Influxdb实现监控)

    1.简介 1.1. 官方网站: promethos:https://prometheus.io/ grafana:https://grafana.com/ 1.2. 架构图 2. 环境 2.1. 机器 ...

  6. 智龙开发板搭建llsp环境

    智龙开发板搭建llsp(linux+lighttpd+sqlite3+php)环境 1. 准备 1. 智龙开发板V3 2. 软件编译环境:VirtualBox6+CentOS6.10-i386.min ...

  7. DP笔记

    这是一篇蒟蒻被大佬踩爆后写的笔记 套路 0.贪心(废话)(排序...) 1.dp预处理出要用的东西 2.两头同时dp 3.化简题目中本质相同的东西 转化模型 4.数学计算优化 5.分析题目数据考虑该从 ...

  8. 联盛德 HLK-W806 (八): 4线SPI驱动SSD1306/SSD1315 128x64 OLED液晶屏

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  9. C#.NET编程小考30题错题纠错

    1)以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储介质中的过程 b) 二进制格式化器的Serialize()和Deserialize()方法可以分别用来实现 ...

  10. Gitlab-CICD实践篇

    一.背景 随着公司项目使用gitlab越来越多,业务发布的次数越来越频繁,对于发布效率提出了更高的要求.从2012开始,Gitlab官方开始集成了Continuous Integration (CI) ...