1103 - 萌萌哒的第八题
Time Limit:7s Memory Limit:128MByte
Submissions:146Solved:47
DESCRIPTION
给出两个数列A和B,长度都为n,分别编号都是从1到n,再给出m个数对(c, d),表示A数列的地c个数可以跟B数列的第d个数匹配,当这两个数被匹配上了,那么总分加上这两个数,但是每个数最多被匹配一次,而且匹配不能相交。也就是说,假设A(i)和B(j)匹配了,那么不能存在一个匹配A(u)和B(v)满足(u < i and v > j) or (u > i and v < j)。求问最后最多能拿多少分。
INPUT
包含多组测试数据(<=5),每组数据: 第一行包含两个整数n(1 <= n <= 1000000), m(1 <= m <= 3000000) 接下来m行,每行两个整数(c, d)表示A(c)可以跟B(d)匹配。 接下来两行,每行n个整数,分别表示A数列和B数列
OUTPUT
每组测试数据输出一行一个证书表示最大得分。
SAMPLE INPUT
4 41 22 13 43 34 4 5 12 3 5 6
SAMPLE OUTPUT
18
#include <stdio.h>
#include <vector>
#include <queue>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const int maxn = 1e6 + 6;
bool key = false;
int n, m;
int a[maxn], b[maxn];
LL dp[maxn],tree[maxn];
void add(int i, LL x)
{
while (i <= n)
{
tree[i] = max(tree[i], x);
i += (i&-i);
}
}
LL query(int i)
{
LL ret = 0;
while (i)
{
ret = max(ret, tree[i]);
i -= (i&-i);
}
return ret;
}
struct Edge
{
int u, v;
};
vector<Edge> p;
bool cmp(Edge a, Edge b)
{
if (a.u == b.u)
return a.v > b.v;
return a.u < b.u;
}
int readint()
{
int ret = 0;
char c = getchar();
while (c<'0' || c>'9') c = getchar();
while (c >= '0'&&c <= '9')
{
ret = ret * 10 + c - '0';
c = getchar();
}
return ret;
}
int main()
{
while (scanf("%d%d", &n,&m) != EOF)
{
for (int i = 0; i <= n; i++)
tree[i] = 0;
p.clear();
for (int i = 0; i < m; i++)
{
Edge t;
t.u = readint();
t.v = readint();
p.push_back(t);
}
sort(p.begin(), p.end(), cmp);
for (int i = 1; i <= n; i++)
a[i] = readint();
for (int i = 1; i <= n; i++)
b[i] = readint();
LL ans = 0;
for (int i = 0; i < p.size(); i++)
{
int& u = p[i].u;
int& v = p[i].v;
LL maxx = query(v-1);
add(v, maxx + a[u] + b[v]);
ans = max(ans, maxx + a[u] + b[v]);
}
cout << ans << endl;
}
return 0;
}
/*
5 5
1 4
2 1
3 2
4 5
5 3
10 3 3 10 3
1 1 1 1 1
4 4
1 2
2 1
3 4
3 3
4 4 5 1
2 3 5 6
5 6
1 5
1 4
2 1
3 2
4 5
5 3
30 3 3 10 3
1 1 1 1 20
*/