当前位置:网站首页>P1020 [noip1999 popularization group] missile interception problem solution
P1020 [noip1999 popularization group] missile interception problem solution
2022-07-21 03:06:00 【wkh2021】
Ideas : Sequence DP + Line segment tree optimization DP
First, the first question , In fact, it is to find the longest Non rising sequence
We design d p i {dp}_{i} dpi For from 1 To i i i And its longest Non rising sequence is i i i For the end The sequence length of . Then we can update the answer from missiles that are higher or equal than the current missile . That is to say :
dp[i] = max{
dp[v]} + 1 (a[v] >= a[i] && v <= i)
The time complexity here is O ( n 2 ) O(n^2) O(n2)
For the second question , We can think of , If we first use an interception facility to shoot down all missiles that can be hit , Repeat this action for the remaining interception facilities , Until all missiles are intercepted . Suppose we get the minimum partition ( Subject requirements ) Of k k k Group missile . So for the i i i Any of the group missiles , In the i + 1 i+1 i+1 There must be a missile higher than this one , without , Then we can put i + 1 i+1 i+1 Combine to i i i Group .
So we find the longest ascending subsequence , Make sure that each missile in the sequence corresponds to a group of missiles , If there is more, it must be redundant , If less , Do not meet the above grouping . So when we are looking for the minimum partition , Just find one Longest ascending subsequence Length can .
We redesign d p i {dp}_{i} dpi For from 1 To i i i And its longest ascending subsequence is i i i For the end The sequence length of . ditto , We can get :
dp[i] = max{
dp[v]} + 1 (a[v] < a[i] && v <= i)
Time complexity is also O ( n 2 ) O(n^2) O(n2)
For optimization : ( Two questions are common )
We can find that one dimension is used to find d p 1.. n {dp}_{1..n} dp1..n The maximum value that satisfies the condition in . We can consider using segment tree to optimize . We use a i a_{i} ai ( It's height ) It's coordinates , Maintain in the segment tree f i f_{i} fi The maximum of . When we traverse regularly , Every time you update f i f_{i} fi We will update it in the segment tree , When looking for the maximum , We follow the size , In the corresponding interval ( For example, find the longest ascending subsequence , We should start from a height less than a i a_{i} ai Of f v f_{v} fv Transfer in , So we asked ( 1 , a [ i ] − 1 ) (1,a[i]-1) (1,a[i]−1) Interval information ) inquiry , And transfer .
The time complexity is O ( n log n ) O(n\log n) O(nlogn)
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
#define root 1,1,maxn
#define ls p<<1,l,m
#define rs p<<1|1,m+1,r
const int maxn=(int)5e4+10;
int Min[maxn<<2], Max[maxn<<2];
inline void update(int p) {
Min[p]= min(Min[p<<1], Min[p<<1|1]);
Max[p]= max(Max[p<<1], Max[p<<1|1]);
return;
}
inline void build(int p,int l, int r) {
if(l==r){
Min[p]=Max[p]=0;return; }
int m= (l+r)>>1;
build(ls); build(rs); update(p);
return;
}
inline void modify(int p, int l, int r, int now, int v) {
if(l==r){
Min[p]=Max[p]=v;return; }
int m= (l+r)>>1;
if(now <= m) modify(ls, now, v);
else modify(rs, now, v);
update(p); return;
}
inline int query(int p, int l, int r, int li, int ri) {
if(li<=l && r<=ri) return Max[p];
int m= (l+r)>>1;
if(li <= m) {
if(m < ri) return max( query(ls, li, ri), query(rs, li, ri) );
else return query(ls, li, ri);
}
else return query(rs, li, ri);
}
int f[maxn]={
0}, a[maxn]={
0};
int n=0, ans1=0, ans2=0, i=0;
int max_i=0;
int main(){
while(scanf("%d",&a[++n])!=EOF); n--;
for(i=1;i<=n;i++) f[i]=0;
build(root);
for(i=1;i<=n;i++) {
max_i=0;
max_i= query(root, a[i], maxn);
f[i]= max_i+1;
modify(root, a[i], f[i]);
ans1= max(f[i], ans1);
}
printf("%d\n",ans1);
for(i=1;i<=n;i++) f[i]=0;
build(root);
for(i=1;i<=n;i++) {
max_i=0;
max_i= query(root, 1, a[i]-1);
f[i]= max_i+1;
modify(root, a[i], f[i]);
ans2= max(f[i], ans2);
}
printf("%d\n",ans2);
return 0;
}
边栏推荐
- System learning CV pytorch advanced
- Partial voice feature recording
- Sparkcore operator and case, 220719,
- Excuse me, teachers, how to choose the startup mode in the flynk SQL Kafka connector
- Fragment APIs are obsolete. Are you still using them?
- [scientific literature measurement] statistics and visualization of word number and frequency of Chinese and English literature titles and abstracts
- Lombok详细介绍
- ES6中Symbol、迭代器和生成器基本语法
- The practical operation of multi category risk scoring data helps you stabilize your small, medium and micro businesses
- Introduction to Command Line
猜你喜欢
[flower carving experience] 20 Music Visualization: esp32_ Series attempts of C3 and ws2812b
【微信小程序】文本域输入带最大字数限制(1/100)
Technical dry goods | mindspire self-developed high-order optimizer source code analysis and practical application
Technical dry goods | cosinesimilarity based on mindspire
5-FU/DEX-g-PLA纳米微粒/BSA-AgNCs-PEI纳米粒/Cu(DDC)2蛋白纳米粒的制备
Usage and examples of Apache Doris binlog load
二叉树实现(根据层级数组生成二叉树)
Implementation of fruit and vegetable mall management system based on SSM
【科学文献计量】中英文文献标题及摘要可读性指标分析与可视化
Developers share the second regression analysis of "Book Eating bar: deep learning and mindspire practice"
随机推荐
webSocket学习与使用
Flag signal
Introduction to Command Line
毕业季--各应用场景案例使用技术
Learning and using websocket
SQL optimization (x): association update
JMeter stress test is set to send a request every second
第11章 网络物理隔离技术原理与应用
【科学文献计量】关键词的挖掘与可视化
Technical dry goods | solve 80% of the problems in the interview, and realize auc/roc based on mindspire
Apache Doris 通过ODBC连接SQL Server
STL list构造函数、大小
Constructeur de liste STL, taille
数据库事务(常被问的)
Circuit board debugging
What is integer lifting (instance)
[flower carving experience] 20 Music Visualization: esp32_ Series attempts of C3 and ws2812b
DOM之事件对象
Sparkcore operator and case, 220719,
Cmake basic grammar and practical project analysis