当前位置:网站首页>[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;
}
边栏推荐
- Flink Window&Time 原理
- English grammar_ Demonstrative pronoun this / these / that / those
- 2019 Hangdian multi school first 6581 vacation [thinking]
- Introduction and advanced tutorial of Albert duilib
- Solve the problem of error l6218e undefined symbol XXX
- Leetcode 146: LRU cache
- 存储类别
- Huawei set up login with account and password
- Thinking of @requestbody caused by hi and hello requests
- Classic interview questions of interface testing: what is the difference between session, cookie and token?
猜你喜欢

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

Leetcode 48 rotating image (horizontal + main diagonal), leetcode 221 maximum square (dynamic programming DP indicates the answer value with ij as the lower right corner), leetcode 240 searching two-d
![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](/img/60/6b75484a65a49c6e20c2b79c062310.png)
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

【LeetCode】1184. 公交站间的距离

Unity2d~ game practice of decrypting Zhou mu (completed in three days)

Each blogger needs to ask himself seven basic questions

"Hualiu is the top stream"? Share your idea of yyds

Introduction to WDK development 1- basic environment construction and the first driver (VS2010)

Huawei set up login with account and password
随机推荐
Software core data protection solution
How to select the shelling tool?
Huawei set up login with account and password
Sword finger offer 40. minimum number of K
Xiaomi 12s ultra products are so powerful, but foreigners can't buy Lei Jun: first concentrate on the Chinese market
Make Huawei router into FTP server (realize upload and download function)
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
Actual measurement of Qunhui 71000 Gigabit Network
Modbus communication protocol specification (Chinese) sharing
Covid-19-20 - basic method of network segmentation based on vnet3d
Usage and introduction of MySQL binlog
Sword finger offer 47. the maximum value of gifts
Recursion of function [easy to understand]
Know typescript
[German flavor] safety: how to provide more protection for pedestrians
English grammar_ Demonstrative pronoun this / these / that / those
BGP - border gateway protocol
Each blogger needs to ask himself seven basic questions
Read the registry through the ATL library clegkey (simple and convenient)
"Hualiu is the top stream"? Share your idea of yyds