LIS ZOJ - 4028

时间:2021-09-22 05:08:12

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4028

memset超时

这题竟然是一个差分约束

好吧呢

对于每一个a[i], l <= a[i] <= r

那么设一个源点s

使 l <= a[i] - s <= r  是不是就能建边了

然后对于每一个f[i]

如果前面有一个相等的f[j]

则肯定 a[i] <= a[j]  又能建边了

根据LIS的传递关系

对于每个f[i] 肯定是由上一个等级的传递过来的

即 a[i] > a[j]   是不是又能建边了

不会建边?

请移步: https://www.cnblogs.com/WTSRUVF/p/9153758.html

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], vis[maxn];
LL d[maxn];
int cnt;
int n, m, s;
int ans[maxn]; struct node
{
int u, v, next;
int w;
}Node[]; void add(int u, int v, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
for(int i = ; i <= n + ; i++)
{
head[i] = -;
ans[i] = ;
vis[i] = ;
d[i] = INF;
}
cnt = ;
} bool spfa()
{
deque<int> Q;
Q.push_front(s);
d[s] = ;
vis[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop_front();
vis[u] = ;
for(int i = head[u]; i != -; i = Node[i].next)
{
int v = Node[i].v;
if(d[v] > d[u] + Node[i].w)
{
d[v] = d[u] + Node[i].w;
if(!vis[v])
{
if(Q.empty()) Q.push_front(v);
else if(d[v] < d[Q.front()]) Q.push_front(v);
else Q.push_back(v);
vis[v] = ;
if(++ans[v] > n) return ;
}
}
}
}
return ;
} int pre[maxn]; int main()
{
int T;
rd(T);
while(T--)
{ rd(n);
init();
s = n + ;
mem(pre, );
for(int i = ; i <= n; i++)
{
int f;
rd(f);
if(pre[f]) add(pre[f], i, );
if(f > ) add(i, pre[f - ], -);
pre[f] = i;
}
for(int i = ; i <= n; i++)
{
int l, r;
rd(l), rd(r);
add(s, i, r);
add(i, s, -l);
}
spfa();
for(int i = ; i <= n; i++)
{
if(i != ) printf(" ");
printf("%lld", d[i]); printf("\n");
} } return ;
}