zoj 浙江省赛 LIS

DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence  of length .

Recall that

  • A subsequence  of length  is a sequence satisfying  and .

  • An increasing subsequence  is a subsequence satisfying .

 

DreamGrid defines the helper sequence  where  indicates the maximum length of the increasing subsequence which ends with . In case you don't know how to derive the helper sequence, he provides you with the following pseudo-code which calculates the helper sequence.

procedure lis_helper(: original sequence)
{Let  be the length of the original sequence,
 be the -th element in sequence , and 
be the -th element in sequence }
for  := 1 to 
     := 1
    for  := 1 to ( - 1)
        if  and 
             :=  + 1
return  { is the helper sequence}

 

DreamGrid has derived the helper sequence using the program, but the original sequence  is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences  and  indicating that  for all .

Please help DreamGrid restore the original sequence which is compatible with the helper sequence and the two range sequences.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the original sequence.

The second line contains  integers  () seperated by a space, indicating the helper sequence.

For the following  lines, the -th line contains two integers  and  (), indicating the range sequences.

It's guaranteed that the original sequence exists, and the sum of  of all test cases will not exceed .

Output

For each test case output one line containing  integers separated by a space, indicating the original sequence. If there are multiple valid answers, print any of them.

Please, DO NOT print extra spaces at the end of each line, or your solution may be considered incorrect!

Sample Input
4
6
1 2 3 2 4 3
0 5
2 4
3 3
1 2
3 5
1 5
5
1 2 1 3 1
100 200
200 300
200 400
400 500
100 500
7
1 2 3 1 1 4 2
0 3
0 3
0 3
0 3
0 3
0 3
0 3
2
1 1
1 2
2 3
Sample Output
1 2 3 2 5 3
200 300 200 500 200
0 1 2 0 0 3 1
2 2
思路:差分约束
  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <set>
 14 #include <map>
 15 #include <list>
 16 #include <iomanip>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <bitset>
 20 #include <ctime>
 21 
 22 using namespace std;
 23 
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair<int, int>
 27 #define pb push_back
 28 #define mp make_pair
 29 #define clr(a, x) memset(a, x, sizeof(a))
 30 
 31 const double pi = acos(-1.0);
 32 const int INF = 0x3f3f3f3f;
 33 const int MOD = 1e9;
 34 const double EPS = 1e-9;
 35 
 36 /*
 37 #include <ext/pb_ds/assoc_container.hpp>
 38 #include <ext/pb_ds/tree_policy.hpp>
 39 
 40 using namespace __gnu_pbds;
 41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
 42 */
 43 
 44 int t, n, f[500015], a[500015], b[500015], la[500015];
 45 struct Edge {
 46     int u, v;
 47     ll w;
 48     Edge () {}
 49     Edge (int u, int v, ll w) : u(u), v(v), w(w) {}
 50 } e[2000015];
 51 int nex[500015], head[500015], tot;
 52 void addEdge(int u, int v, int w) {
 53     e[++tot] = Edge(u, v, w);
 54     nex[tot] = head[u];
 55     head[u] = tot;
 56 }
 57 void build() {
 58     tot = 0;
 59     for (int i = 0; i <= n; ++i) head[i] = 0;
 60     for (int i = 1; i <= n; ++i) {
 61         addEdge(i, 0, -a[i]);
 62         addEdge(0, i, b[i]);
 63         int x = f[i];
 64         if (la[x]) {
 65             addEdge(la[x], i, 0);
 66         }
 67         la[x] = i;
 68         if (x > 1) {
 69             addEdge(i, la[x - 1], -1);
 70         }
 71     }
 72 }
 73 queue<int> que;
 74 bool inque[500015];
 75 ll d[500015];
 76 void spfa() {
 77     for (int i = 0; i <= n; ++i) {
 78         inque[i] = 0;
 79         d[i] = 1e18;
 80     }
 81     que.push(0);
 82     d[0] = 0;
 83     inque[0] = 1;
 84     while (que.size()) {
 85         int x = que.front(); que.pop();
 86         inque[x] = 0;
 87         for (int i = head[x]; i; i = nex[i]) {
 88             int y = e[i].v;
 89             ll w = e[i].w;
 90             if (d[y] > d[x] + w) {
 91                 d[y] = d[x] + w;
 92                 if (!inque[y]) {
 93                     que.push(y);
 94                     inque[y] = 1;
 95                 }
 96             }
 97         }
 98     }
 99 }
100 void print() {
101     for (int i = 1; i < n; ++i) {
102         printf("%lld ", d[i]);
103     }
104     printf("%lld\n", d[n]);
105 }
106 int main() {
107     scanf("%d", &t);
108     while (t--) {
109         scanf("%d", &n);
110         for (int i = 1; i <= n; ++i) {
111             la[i] = 0;
112             scanf("%d", &f[i]);
113         }
114         for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]);
115         build();
116         spfa();
117         print();
118     }
119     return 0;
120 }
View Code

 

转载于:https://www.cnblogs.com/BIGTOM/p/8971697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值