当前位置:网站首页>[Luogu p6891] ビルのりけ 4 (DP) (conclusion)
[Luogu p6891] ビルのりけ 4 (DP) (conclusion)
2022-07-18 18:40:00 【SSL_ TJH】
ビルの Decoration り pay け 4
Topic link :luogu P6891
The main idea of the topic
Here are two lengths for you 2n Sequence , Then you have to gather one 2n Each position of the sequence can be selected from the corresponding position in the two sequences .
Then you need to ensure that the sequence you get is monotonic and that both sequences are used n individual .
You need to judge whether there is no solution or output one of the legal solutions .
Ideas
On the subject
The first one is obvious DP set up f i , j , 0 / 1 f_{i,j,0/1} fi,j,0/1 For the former i i i One of the first sequences used j j j One last, one first / Is there an answer to the second sequence .
And then this is n 2 n^2 n2 Of , Consider some nature .
Observe the appearance of the edge , You will find that there are such restrictions .
There are some places where you can only choose one , In some places, you can't choose one after choosing one .
( It's small enough to connect two , Big ones can only be connected with one , From the perspective of not being able to connect, this is the case )
The front one is easy to do , Consider the following limitations , The whole graph can certainly be divided into several complementary constraints , Then the limitation of connection will form a chain , Specifically, the adjacent two on the chain cannot be selected at the same time .
The quantity selected for each chain is an interval , The sum of those intervals is also an interval , So we can draw such a conclusion :
The number of two sequences that can be selected is in an interval .
Because of complementarity , We can separate DP Out f i , 0 / 1 , 0 / 1 f_{i,0/1,0/1} fi,0/1,0/1 For the former i i i The last one is the first sequence or the second , How many can you choose from the first sequence or the second sequence at most .
So as long as the maximum value of both sequences is greater than the requirement , It falls into the interval .
Then we ask for a plan , Let's push backwards , If you can still meet the conditions, choose , Then if you are not satisfied with either choice, there is no solution .
ex Bupleurum
Niuke has an upgraded version of this one , It requires legal choice , I'm too lazy to write here .
Let's continue to see from the proof , Let's consider each link , Assumed length is x x x, We have to choose among them y y y individual , The inserting method is :
( x − y + 1 y ) \binom{x-y+1}{y} (yx−y+1)( The subtracted ones are put behind the ones you choose, and you are forced not to choose )
Then you make these polynomials , Then it is not difficult to see that several polynomials multiply to take a certain term , Divide and conquer directly + NTT that will do .
Code
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N = 1e6 + 100;
int n, a[N][2], f[N][2][2];
char ans[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n << 1; i++) scanf("%d", &a[i][0]);
for (int i = 1; i <= n << 1; i++) scanf("%d", &a[i][1]);
memset(f, -0x7f, sizeof(f));
f[1][0][0] = 1; f[1][0][1] = 0; f[1][1][0] = 0; f[1][1][1] = 1;
for (int i = 2; i <= n << 1; i++) {
if (a[i - 1][0] <= a[i][0]) {
f[i][0][0] = max(f[i][0][0], f[i - 1][0][0] + 1); f[i][0][1] = max(f[i][0][1], f[i - 1][0][1]);
}
if (a[i - 1][1] <= a[i][0]) {
f[i][0][0] = max(f[i][0][0], f[i - 1][1][0] + 1); f[i][0][1] = max(f[i][0][1], f[i - 1][1][1]);
}
if (a[i - 1][0] <= a[i][1]) {
f[i][1][0] = max(f[i][1][0], f[i - 1][0][0]); f[i][1][1] = max(f[i][1][1], f[i - 1][0][1] + 1);
}
if (a[i - 1][1] <= a[i][1]) {
f[i][1][0] = max(f[i][1][0], f[i - 1][1][0]); f[i][1][1] = max(f[i][1][1], f[i - 1][1][1] + 1);
}
}
int numa = 0, numb = 0, lst = 2e9;
for (int i = n << 1; i >= 1; i--) {
if (numa + f[i][0][0] >= n && numb + f[i][0][1] >= n && a[i][0] <= lst) {
numa++; ans[i] = 'A'; lst = a[i][0];
}
else if (numa + f[i][1][0] >= n && numb + f[i][1][1] >= n && a[i][1] <= lst) {
numb++; ans[i] = 'B'; lst = a[i][1];
}
else {
printf("-1"); return 0;
}
}
for (int i = 1; i <= n << 1; i++) putchar(ans[i]);
return 0;
}
边栏推荐
猜你喜欢

UPUPWANK柚皮安装SSL证书指南

CAN光纤转换器解决泰和安TX3016A消防主机长距离联网问题

P1789 【Mc生存】插火把【入门】

ctf-pikachu-sql

STM32 - timer interrupt experiment

Z-Wave CTT使用方法以及测试演示
![[graduation project] network public opinion hotspot analysis system based on Emotional Analysis](/img/b6/c297f9b81446d8b2d0220c5f8774b5.png)
[graduation project] network public opinion hotspot analysis system based on Emotional Analysis

危化品企业双重预防机制数字化系统怎样建?

Nature Microbiology | 枯草芽孢杆菌生物膜促进甜瓜生长并抗病
![LeetCode 剑指Offer II 041滑动窗口的平均值[滑动窗口] HERODING的LeetCode之路](/img/29/ea4b91cc90ca0dd0eea7e6a3d33394.png)
LeetCode 剑指Offer II 041滑动窗口的平均值[滑动窗口] HERODING的LeetCode之路
随机推荐
给即将毕业要走往程序员道路的10条建议(精彩配图)
TCP拥塞控制详解 | 6. 主动队列管理
递归优化之缓存结果(js)
HALCON联合C#检测表面缺陷——ROI交互(二)(和图片同步缩放裁剪等功能)
对象序列化流与反序列化流
Stream—优雅的处理集合元素
uCOS-III学习笔记——时间片轮转
nn. BCEWithLogisticLoss() & nn. The difference between bceloss(), nn CrossEntropyLoss() & nn. Nllloss() differences
Idea installation, configuration, testing
特征值和特征向量的求法
华为设备射频资源管理命令
IDEA安装、配置、测试
WordPress personal blog theme wp-reason-v1.0
MySQL笔记: B站宋红康最新教程(持续更新中)
Sweep redis distributed crawler deployment
STM32——定时器中断实验
论文翻译解读:learning logic rules for reasoning on knowledge graphs【RNNLogic】
华为设备配置射频调优
字节三面终上岸,这份热腾腾的面经,请收好「含免费资料」
scrapy-redis分布式爬虫部署