差分约束瞎搞一下,话说这个数据不知道怎么回事,我的图按道理而言最多只有4n条边,开5n还不够??必须6n??

约束条件首先根据f函数可建立两点之间的约束,不妨设d[i]为i到0的距离,则对于f[i] == f[j] + 1(i>j)的情况,必定d[i] - d[j] >= 1,对于f[i] == f[j]的情况,必定d[j] - d[i] >= 0(i>j)

区间范围就不说了吧。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define check(x,y) (x>=5&&y>=5&&x<n+5&&y<m+5)
#define dight(chr) (chr>='0'&&chr<='9')
#define alpha(chr) (chr>='a'&&chr<='z')
#define rep(i,a,n) for(int i=a;i<n;++i)
#define repe(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define loop(a) for(int i=0;i<a;++i)
#define loope(a) for(int i=1;i<=a;++i)
#define clc(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f3f3f3f
#define maxn 100005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 1000000000
#define repe(x,y,i) for(i=(x);i<=(y);++i)
#define repne(x,y,i) for(i=(x);i<(y);++i)
#define MAX(x,y) (x) < (y) ? (y) : (x);
struct edge{
int to;
int cost;
int next;
}G[];
int f,l,r,p[maxn],len;
int head[maxn],n;
ll dis[maxn];
bool inq[maxn];
void Read(int &n){char chr=getchar(),sign=;for(;!dight(chr);chr=getchar())if(chr=='-')sign=-;
for(n=;dight(chr);chr=getchar())n=n*+chr-'';n*=sign;}
void Read(ll &n){char chr=getchar(),sign=;for(;!dight(chr);chr=getchar())if
(chr=='-')sign=-;
for(n=;dight(chr);chr=getchar())n=n*+chr-'';n*=sign;}
void spfa(int st){
queue<int> q;
memset(inq,false,sizeof(inq));
for(int i = ;i <= n;++i) dis[i] = INF;
q.push(st);
inq[st] = true;
dis[st] = ;
while(!q.empty()){
int v = q.front();
q.pop();
inq[v] = false;
for(int i = head[v];i;i = G[i].next){
edge& t = G[i];
if(dis[t.to] > dis[v] + t.cost){
dis[t.to] = dis[v] + t.cost;
if(!inq[t.to]) q.push(t.to);
}
}
}
for(int i = ;i < n;++i) printf("%lld ",dis[i]);
printf("%lld\n",dis[n]);
}
int main(){
//// freopen("a.in","r",stdin);
// freopen("b.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
len = ;
memset(head,,sizeof(head));
memset(p,,sizeof(p));
Read(f);
p[f] = ;
for(int i = ;i <= n;++i){
Read(f);
if(f == ){
G[len].to = i,G[len].cost = ,G[len].next = head[p[]],head[p[]] = len,++len;
p[f] = i;
continue;
}
int t = p[f - ];
G[len].to = t,G[len].cost = -,G[len].next = head[i],head[i] = len,++len;
t = p[f];
if(t) G[len].to = i,G[len].cost = ,G[len].next = head[t],head[t] = len,++len;
p[f] = i;
}
for(int i = ;i <= n;++i){
Read(l),Read(r);
G[len].to = ,G[len].cost = -l,G[len].next = head[i],head[i] = len,++len;
G[len].to = i,G[len].cost = r,G[len].next = head[],head[] = len,++len;
}
spfa();
}
return ;
}

zoj4028 LIS的更多相关文章

  1. zoj4028 LIS,差分约束

    题意:给你以i为结尾的最长上升子序列的值,和每个值的区间范围求可行的a[i] 题解:差分约束,首先满足l[i]<=a[i]<=r[i],可以建一个虚拟节点n+1,那么有a[n+1]-a[i ...

  2. Lis日常维护

    1.[问题]护士站打印LIs条码,出来是PDF格式的 [解决]在文件夹Client\NeusoftLis\Xml\Print.xml中把BarcodePrint Name的值改成安装的斑马打印机名(不 ...

  3. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  4. Codeforces 486E LIS of Sequence 题解

    题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...

  5. 出操队形(LIS)

    题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...

  6. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  7. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  8. 从LIS问题浅谈动态规划

    今天以LIS问题切入动态规划,现在做一些简单的总结. LIS问题: http://www.cnblogs.com/Booble/archive/2010/11/27/1889482.html

  9. [noip科普]关于LIS和一类可以用树状数组优化的DP

    预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...

随机推荐

  1. RHEL5.8上SAMBA源码修改打包安装流程

    之前一直使用系统自带的SAMBA,近期需要对SAMBA代码做一些修改,然后还是打算用RPM包的方式来安装部署. 这个流程本身不复杂,在这里记录下来,免得在另外写说明文档. 关键词:RHEL5.8, s ...

  2. javascript innerHTML 大数据量加载 导致IE 内存溢出 的解决办法

    在做 ajax 滚动加载的时候,越到后面 数据量越大,使用obj.innerHTML+=row添加到页面的时候,出现ie内存不足的情况,此时使用createDocumentFragment,创建一个文 ...

  3. [Windows Server 2012] 安装IIS8.5及FTP

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:安装IIS ...

  4. mybatis 存储过程的写法

    (注意事项: 在使用游标的时候,不能在游标声明之前,使用crud) 存储过程示例 CREATE DEFINER=`root`@`::` PROCEDURE `earnings_proceduce`() ...

  5. pagehelper 分页

    分页jar包: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pa ...

  6. 4.关于while循环的基础小练习

    1)使用while.if循环输入123456 8910 count = 0 while count < 10: count += 1 if count == 7: print('') else: ...

  7. _vimrc配置

    set nocompatible set encoding=utf8 set guioptions-=T set number set guifont=consolas:h12 source $VIM ...

  8. 获得Dictionary所有key和value值

    Dictionary<string, string> dc = new Dictionary<string, string>(); dc.Add("code" ...

  9. 前端打包--source-map=false作用

    参考:http://www.cnblogs.com/axl234/p/6500534.htmlng serve默认会产生.map文件,该文件保存有原始代码与运行代码的映射关系, 浏览器可以通过它找到原 ...

  10. WEB 前端模块化,读文笔记

    文章链接 WEB 前端模块化都有什么? 知识点 根据平台划分 浏览器 AMD.CMD 存在网络瓶颈,使用异步加载 非浏览器 CommonJS 直接操作 IO,同步加载 浏览器 AMD 依赖前置 req ...