您的位置:首页 > 产品设计 > UI/UE

Subsequence Weighting

2016-07-21 23:21 309 查看
题目链接

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100010;
const ll  inf  = 0x3f3f3f3f3f3f3f3fLL;
map<int, ll> mp;
int a[maxn], b[maxn];

ll max(ll a, ll b) {
return a > b ? a : b;
}

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */

int T, n;
ll ans, tmp;
map<int, ll>::iterator it1, it2;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
}
mp.clear();
ans = b[0];
mp[a[0]] = b[0];
for(int i = 1; i < n; ++i) {
if(mp.lower_bound(a[i]) == mp.begin()) {
mp[a[i]] = max(mp[a[i]], (ll)b[i]);
} else {
it1 = mp.lower_bound(a[i]);
it1--;
mp[a[i]] = max(mp[a[i]], it1->second + b[i]);
}
ans = max(ans, mp[a[i]]);
tmp = mp[a[i]];
it1 = mp.find(a[i]);
it2 = ++it1;
while(it2 != mp.end()) {
if(it2->second <= tmp) {
mp.erase(it2);
it1 = mp.find(a[i]);
it2 = ++it1;
} else {
break;
}
}
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: