当前位置:网站首页>[training Day10] tree [interval DP]
[training Day10] tree [interval DP]
2022-07-24 20:10:00 【VL——MOESR】

Ideas :
We set up f[i][j][0/1] Express i~j The maximum weight sum of the root node of this segment on the left or right .
Just enumerate the root nodes k, Image interval DP It's OK to transfer in that way
c o d e code code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll MAXN = 310;
ll n, g[MAXN][MAXN], s[MAXN], f[MAXN][MAXN][2];
struct node {
ll key, val;
}a[MAXN];
ll gcd(ll x, ll y) {
if(y == 0) return x;
else return gcd(y, x % y);
}
bool cmp(node x, node y) {
return x.key < y.key;
}
int main() {
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
scanf("%lld", &n);
for(ll i = 1; i <= n; i ++)
scanf("%lld%lld", &a[i].key, &a[i].val);
memset(f, -0x3f, sizeof(f));
sort(a + 1, a + 1 + n, cmp);
for(ll i = 1; i <= n; i ++)
for(ll j = 1; j <= n; j ++) g[i][j] = gcd(a[i].key, a[j].key);
for(ll i = 1; i <= n; i ++) s[i] = a[i].val + s[i - 1];
for(ll i = 1; i <= n; i ++) {
if(i != 1 && g[i][i - 1] != 1) f[i][i][0] = a[i].val;
if(i != n && g[i][i + 1] != 1) f[i][i][1] = a[i].val;
}
ll ans = - 1e18;
for(ll l = 2; l <= n; l ++) {
for(ll i = 1; i + l - 1 <= n; i ++) {
ll j = i + l - 1;
for(ll k = i; k <= j; k ++) {
ll sum;
if(k == i) sum = f[i + 1][j][0] + s[j] - s[i - 1];
else if(k == j) sum = f[i][j - 1][1] + s[j] - s[i - 1];
else sum = f[i][k - 1][1] + f[k + 1][j][0] + s[j] - s[i - 1];
if(i != 1 && g[k][i - 1] != 1) f[i][j][0] = max(f[i][j][0], sum);
if(j != n && g[k][j + 1] != 1) f[i][j][1] = max(f[i][j][1], sum);
if(l == n) ans = max(ans, sum);
}
}
}
if(ans == - 1e18) printf("-1");
else printf("%lld", ans);
return 0;
}
边栏推荐
- vlan技术
- Leetcode 300 longest increasing subsequence (greedy + binary search for the first element subscript smaller than nums[i]), leetcode 200 island number (deep search), leetcode 494 target sum (DFS backtr
- Look at the interface control devaxpress WinForms - how to customize auxiliary function properties (Part 2)
- Redis common configuration description
- Choose the appropriate container runtime for kubernetes
- Please ask a question. Follow the quick start method. After creating the table, the Flink SQL queries and displays the table structure, but there is an error when it exceeds the limit. What should we
- Install MySQL 5.7.37 on windows10
- Duilib actual combat 1- imitate Baidu online disk login interface
- Solve the problem that gd32f207 serial port can receive but send 00
- Sword finger offer 47. the maximum value of gifts
猜你喜欢

Flink Window&Time 原理

Lunch break train & problem thinking: thinking about the problem of converting the string formed by hour: minute: second to second

ATL container - catlmap, crbmap

In the era of new knowledge economy, who is producing knowledge?

Modbus communication protocol specification (Chinese) sharing

vlan技术

clip:learning transferable visual models from natural language supervision

Create a life cycle aware MVP architecture

Istio二之流量劫持过程
![[leetcode] 1184. Distance between bus stops](/img/8c/c396e6f614f465bc09b0653540a1c8.png)
[leetcode] 1184. Distance between bus stops
随机推荐
Introduction and advanced tutorial of Albert duilib
About the largeheap attribute
Hcip early summary
Flink Window&Time 原理
Ask a question: is there an error msg = ora-04036: instance usage when using CDC to monitor oracle
英文翻译中文常见脏话
871. Sum of divisors
Leetcode 560 and the subarray of K (with negative numbers, one-time traversal prefix and), leetcode 438 find all alphabetic ectopic words in the string (optimized sliding window), leetcode 141 circula
Valdo2021 - vascular space segmentation in vascular disease detection challenge (3)
Unity2d~ game practice of decrypting Zhou mu (completed in three days)
Duilib actual combat 1- imitate Baidu online disk login interface
Inconsistent time
Substr and substring function usage in SQL
Leetcode 146: LRU cache
Safe way -- Analysis of single pipe reverse connection back door
Maya coffee machine modeling
Basic idea of regularization
Call Baidu AI open platform to realize image and character recognition
How to select software dongle
Getting started with COM programming 1- creating projects and writing interfaces