【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 ...
随机推荐
- Mysql数据库的瓶颈处理一点建议
Mysql数据库的瓶颈处理一点建议 我们在使用Mysql数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈的出 ...
- mysql-5.7.19-winx64服务无法启动解决方案
解压mysql压缩包时没有data文件夹,不要手动创建,在cmd下直接运行命令: mysqld –initialize-insecure,data文件夹会自动生成,注意单词千万不要拼错,不要写成–in ...
- System and method for controlling switching between VMM and VM using enabling value of VMM timer indicator and VMM timer value having a specified time
In one embodiment, a method includes transitioning control to a virtual machine (VM) from a virtual ...
- 感谢党,软考过了。系统集成项目管理project师
人品爆发了,刚用干巴巴的日语做完2小时的设计说明,回到家一查,人品爆发了.软考竟然过了. 绝对是评卷老师给人品啊!真想请他吃顿饭. 系统集成项目管理project师 64 53 幸运飞过! 今天真是 ...
- listview-属性大全
<ListView <!-- 决定listview里的内容是否从底部开始 -- android:stackFromBottom="true" </Listview ...
- 让单选input框,不在被选中,添加disabled即可。输入框input的一些技巧
1.让单选input框,不在被选中,添加disabled即可 2.input的file文件对象的清空,只需要input.val("");就可以了.
- $(dom).each(index,ele){} 真正的jquery对象
1.$(ele)才是each真正的jquery对象,而不是ele.$('.mt-story-info').each(function(index,ele){ if($('.mt-story-info' ...
- Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境)
原文:Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境) 1.双11抢购***VPS.配置如下: CPU:2 核 内存:2048 M ...
- 驱动学习3-make
在向内核中添加驱动的时候要完成3项工作 (1)在Kconfig中添加新代码对应项目的编译条件(下面Makefile文件中需要用到它定义的的宏变量) (2)将驱动源码添加到对应的目录中 (3)在目录Ma ...
- Redis学习笔记--String(四)
Redis的第一个数据类型string 1.命令 1.1赋值 语法:SET key value Set key value; > OK 1.2取值 语法:GET key > get tes ...