【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 ...
随机推荐
- Day4上午解题报告
预计分数:50 +0+0=50 实际分数:50+0+10=60 毒瘤出题人,T3不给暴力分 (*  ̄︿ ̄) T1 https://www.luogu.org/problem/show?pid=T155 ...
- modal模态框插件
用法: <!--模态框--> <div class="modal fade" id="myModal"> <div class=& ...
- “ping”命令的原理就是向对方主机发送UDP数据包,HTTP在每次请求结束后都会主动释放连接,因此HTTP连接是一种“短连接”
Socket 是一套建立在TCP/IP协议上的接口不是一个协议 应用层: HTTP FTP SMTP Web 传输层: 在两个应用程序之间提供了逻辑而不是物理的通信(TCP UDP) T ...
- Bitmap-把方形图片处理为圆形
这个是直接在网上转载的,自己验证可靠 转载自http://my.oschina.net/zhouz/blog/213164 直接贴上代码 import android.graphics.Bitmap; ...
- 10. Spring Boot JDBC 连接数据库
转自:https://blog.csdn.net/catoop/article/details/50507516
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第三篇:ASP.NET MVC全局观
摘要 本文对ASP.NET MVC的全局运行机理进行一个简要的介绍,以使得朋友们更好的理解后续文章. 前言 在上一篇文章中,我们实现了第一个ASP.NET MVC页面.对于没有接触 ...
- Fiddler代理配置
1.下载安装软件Fiddler 2.Fiddler设置HTTPS代理(如果代理的是https请求的需要操作这一步) 打开Fiddler,菜单栏:Tools -> Fiddler Options ...
- .net core 修改网站启动端口
原文:.net core 修改网站启动端口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yenange/article/details/81675 ...
- jvm compile
>>>Making sec-files-win @ Thu Oct 17 20:34:02 CST 2013 ... >>>Making jgss-files @ ...
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...