当前位置:网站首页>[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;
}
边栏推荐
- Duilib actual combat 1- imitate Baidu online disk login interface
- Create a life cycle aware MVP architecture
- Redis basic knowledge, application scenarios, cluster installation
- Each blogger needs to ask himself seven basic questions
- Use of paging assistant PageHelper
- Using videoview to realize video playback in turns
- Look at the interface control devaxpress WinForms - how to customize auxiliary function properties (Part 2)
- TCP sliding window, singleton mode (lazy and hungry) double checked locking / double checked locking (DCL)
- The U.S. economy continues to be weak, and Microsoft has frozen recruitment: the cloud business and security software departments have become the hardest hit
- Work notes - some problems encountered when using jest
猜你喜欢

Xiaomi 12s ultra products are so powerful, but foreigners can't buy Lei Jun: first concentrate on the Chinese market

Getting started with COM programming 1- creating projects and writing interfaces

Alibaba cloud technology expert Yang Zeqiang: building observable capabilities on elastic computing cloud
![Microservice architecture | service monitoring and isolation - [sentinel] TBC](/img/28/8ca90e9dbd492688e50446f55959ff.png)
Microservice architecture | service monitoring and isolation - [sentinel] TBC

Usage and introduction of MySQL binlog

Read the registry through the ATL library clegkey (simple and convenient)

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

Description of large and small end mode

Huawei set up login with account and password
![[leetcode] 1184. Distance between bus stops](/img/8c/c396e6f614f465bc09b0653540a1c8.png)
[leetcode] 1184. Distance between bus stops
随机推荐
Richview table table alignment
Leetcode 206 reverse linked list, 3 longest substring without repeated characters, 912 sorted array (fast row), the kth largest element in 215 array, 53 largest subarray and 152 product largest subarr
Student achievement management system based on PHP
Pix2seq: Google brain proposes a unified interface for CV tasks!
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
Redisgraph graphic database multi activity design scheme
Duilib actual combat 1- imitate Baidu online disk login interface
In the era of new knowledge economy, who is producing knowledge?
Covid-19-20 - basic method of network segmentation based on vnet3d
Substr and substring function usage in SQL
Using videoview to realize video playback in turns
Istio II traffic hijacking process
The difference between delete, truncate and drop in MySQL
(posted) differences and connections between beanfactory and factorybean
[leetcode] 1184. Distance between bus stops
聊下自己转型测试开发的历程
MySQL stored procedure
Expression evaluation (stack)
Description of large and small end mode
2019 Hangzhou Electric Multi School Game 9 6684 Rikka with game [game question]