【38.24%】【POJ 1201】Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25902 Accepted: 9905
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
Southwestern Europe 2002
【题目链接】:http://poj.org/problem?id=1201
【题解】
题意:
给你m个条件ai bi ci;
表示ai..bi这些整数在序列中至少出现了ci次;
然后问你这个序列最少能由多少个数字组成;
设d[i]表示1..i这些数字里面有多少个数字;
则所给的m个条件可以写出
d[bi]-d[ai]>=ci;
然后就转化为差分约束的题了;
考虑转化为最长路
if (d[u]-d[v]<c)
d[u] = d[v]+c;
可以看到我们令d[u]=d[v]+c后实际上是让d[u]-d[v]=c;
而原本d[u]-d[v]是小于c的,而如果d[u]再变大一点也可以满足d[u]-d[v]>c但是显然直接让d[u]-d[v]=c会使得d[u]更小;
这样进行松弛操作后;整个d就是最小的了;
所以对输入的ai bi ci;
即d[bi]-d[ai]>=ci;
则在ai与bi之间建一条由ai指向bi的单向边,边权为ci;
又有0<=d[i]-d[i-1]<=1;->因为一个数字没必要出现两次;只可能有两种情况,出现一次或者不出现;
则
d[i]-d[i-1]>=0
d[i-1]-d[i]>=-1
再根据这两个在i-1和i之间建边权为0的边,在i和i-1之间建边权为-1的边;
然后一开始dis数组赋值为无穷小;然后从起点跑spfa即可;
但是这样还不够;有些细节要做到位
比如
2
1 2 1
2 3 1
这个输入
如果我们单纯地在ai与bi之间建一条由ai指向bi的单向边,边权为ci;
则会得到ans=2的错解;
原因在于程序没办法判断1-2和2-3是否重合了;
做法是把[ai,bi]换成[ai,bi+1);
因为是整数,所以这样的做法是正确的;
也即对于输入的ai bi ci;
直接令bi++;
然后再建边;
找到最左的端点作为s,最右的端点作为t;则从s开始跑spafa;最后输出d[t];
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXM = (50000+100)*4;
const int INF =-0x3f3f3f3f;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int w[MAXM],en[MAXM],fir[MAXM],nex[MAXM];
int n,totm=0;
void add(int x,int y,int z)
{
totm++;
nex[totm] = fir[x];
fir[x] = totm;
en[totm] = y;
w[totm] = z;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
int s = 21e8,t=0;
rei(n);
rep1(i,1,n)
{
int x,y,z;
rei(x);rei(y);rei(z);
s = min(s,x);t=max(t,y+1);
//dis[y]-dis[x]>=z
add(x,y+1,z);
}
rep1(i,s,t)
{
add(i,i+1,0);
add(i+1,i,-1);
}
int dis[MAXM];
memset(dis,INF,sizeof(dis));
dis[s] = 0;
queue <int>dl;
bool in[MAXM];
dl.push(s);in[s] = true;
while (!dl.empty())
{
int x = dl.front();
in[x] = false;
dl.pop();
for (int temp = fir[x];temp;temp=nex[temp])
{
int y = en[temp];
if (dis[y]<dis[x]+w[temp])
{
dis[y] = dis[x] + w[temp];
if (!in[y])
{
in[y] = true;
dl.push(y);
}
}
}
}
cout << dis[t]<<endl;
return 0;
}
【38.24%】【POJ 1201】Intervals的更多相关文章
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- JAVA 基础编程练习题24 【程序 24 根据输入求输出】
24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...
- 【POJ 1201】 Intervals
[题目链接] 点击打开链接 [算法] 令sum(n)表示区间[1,n]中选了几个点 那么,显然有以下不等式 : 1. sum(n)- sum(n - 1) >= 0 2. sum(n) - s ...
- 【38.24%】【codeforces 621E】 Wet Shark and Blocks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)
题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
随机推荐
- Appium_python3使用汇总
1. 对webview页面元素的处理self.driver.switch_to.context("WEBVIEW_com.aaa.bbb")source = self.driver ...
- JS实践与写博客-序
大二的时候,就开始接触JavaScript了. 当时学了1年多,主要是认真看了一本JavaScript的入门书籍,了解了JavaScript大致怎么回事.在独自做Web项目的时候,用的都是JavaSc ...
- Boost 解析xml——插入Item
XML格式为 <?xml version="1.0" encoding="utf-8"?> <Config> <Item name ...
- 洛谷 P1054 等价表达式
洛谷 P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式, ...
- SSO单点登录学习总结(3)—— 基于CAS实现单点登录实例
第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登录实例(我们可以布到多个机器上使用都是同一个道理的),一个服务器主机,和两个客户端虚拟主机 [html] view plaincop ...
- 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)
接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...
- STM32W108无线射频模块串行通信接口编程实例
STM32W108无线射频模块UART通信应用实例 基于STM32W108芯片,编写串口測试程序,測试串口通信.完毕PC通过串口与STM32W108进行通信. 开发环境与硬件平台 硬件:STM32W1 ...
- ubuntu-软件解压方法(转载)
一下内容转载自 http://blog.csdn.net/zad522/article/details/2770446 今天用到了ubuntu解压,所以就在网上查找了下方法.自己菜鸟一枚,收录别人的文 ...
- TextView-属性大全(设置超链接颜色)
今天想要修改一个textview下的超链接的颜色值,自己当时在网上搜了一下,结果看到的全是怎么给一个textview中的部分内容设置颜色.下划线等.当时就以为在textview属性里面可能不存在设定超 ...
- 使用solr的DIHandler 构建mysql大表全量索引,内存溢出问题的解决方法
solr官方给出的解决方式是: DataImportHandler is designed to stream row one-by-one. It passes a fetch size value ...