zoj4028 LIS
差分约束瞎搞一下,话说这个数据不知道怎么回事,我的图按道理而言最多只有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的更多相关文章
- zoj4028 LIS,差分约束
题意:给你以i为结尾的最长上升子序列的值,和每个值的区间范围求可行的a[i] 题解:差分约束,首先满足l[i]<=a[i]<=r[i],可以建一个虚拟节点n+1,那么有a[n+1]-a[i ...
- Lis日常维护
1.[问题]护士站打印LIs条码,出来是PDF格式的 [解决]在文件夹Client\NeusoftLis\Xml\Print.xml中把BarcodePrint Name的值改成安装的斑马打印机名(不 ...
- uva10635 LIS
Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...
- Codeforces 486E LIS of Sequence 题解
题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...
- 出操队形(LIS)
题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- 从LIS问题浅谈动态规划
今天以LIS问题切入动态规划,现在做一些简单的总结. LIS问题: http://www.cnblogs.com/Booble/archive/2010/11/27/1889482.html
- [noip科普]关于LIS和一类可以用树状数组优化的DP
预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...
随机推荐
- 搜索模板elasticsearch
搜索: like 对中文分词效率与支持都不太友好elasticsearch 实时的(效率高).分布式(可扩展)的搜索和分析引擎,基于Lucene全文搜索引擎工具包,算法基于倒排索引算法(eg:一篇文章 ...
- 遍历NSView下的子视图方法
如何遍历NSView下的子视图呢 for (NSView *aview in [SuperV subviews]) { if([aview isMemberOfClass:[NSButton clas ...
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 忘记mysql密码
[root@mysql-db03 ~]# mysql -uroot -poldboy123Warning: Using a password on the command line interface ...
- Android接入支付宝和微信支付
然后把下载下来的aar包,放到项目目录下面的libs目录下,通过下面的gradle依赖进来 // 支付宝 SDK AAR 包所需的配置compile(name: 'alipaySdk-15.6.0-2 ...
- instance of type of object.prototype.tostring 区别
typeof typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型. 返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 6 种: number.boolea ...
- zeromq编译与应用
libzmq是c++语言开发的,正式版本在这里: https://github.com/zeromq/libzmq/releases 到这篇文件发布为止,正式稳定版是4.2.2 1,按照给出的链接下载 ...
- django 127.0.0.1 将您重定向的次数过多
"GET /?next=/%3Fnext%3D/%253Fnext%253D/ HTTP/1.1" 302 0 solution reference from django.con ...
- Java中的代理--proxy
讲到代理,好像在之前的springMVC,还是spring中或者是hibernate中学习过,并没有特别在意,这次好好理解一下.(原来是在spring中的AOP,面向切面 Aspect Oriente ...
- 《BUG创造队》作业8:软件测试与Alpha冲刺(第四天)
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...
- 正则表达式入门教程&&经典Javascript正则表达式----share
前言 例子: ^.+@.+\\..+$ 这样的代码曾经多次把我自己给吓退过.可能很多人也是被这样的代码给吓跑的吧.继续阅读本文将让你也可以自由应用这样的代码. 正文 教程:正则表达式30分钟入门教程 ...