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 = 110000, 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[1000500]; 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 = 0; i <= n + 1; i++) { head[i] = -1; ans[i] = 0; vis[i] = 0; d[i] = INF; } cnt = 0; } bool spfa() { deque<int> Q; Q.push_front(s); d[s] = 0; vis[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop_front(); vis[u] = 0; for(int i = head[u]; i != -1; 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] = 1; if(++ans[v] > n) return 1; } } } } return 0; } int pre[maxn]; int main() { int T; rd(T); while(T--) { rd(n); init(); s = n + 1; mem(pre, 0); for(int i = 1; i <= n; i++) { int f; rd(f); if(pre[f]) add(pre[f], i, 0); if(f > 0) add(i, pre[f - 1], -1); pre[f] = i; } for(int i = 1; i <= n; i++) { int l, r; rd(l), rd(r); add(s, i, r); add(i, s, -l); } spfa(); for(int i = 1; i <= n; i++) { if(i != 1) printf(" "); printf("%lld", d[i]); printf("\n"); } } return 0; }
原文地址:https://www.cnblogs.com/WTSRUVF/p/10776950.html
时间: 2024-10-19 11:21:09