Almost Sorted Array---hdu5532(j简单dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5532

题意:问一个含有n个数的序列,删除一个数后是否有序(递增或递减都可以)

我们只要求一下最长上升子序列或者最长下降子序列是否大于等于n-1即可;

时间复杂度n*log(n);

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define N 100005
#define INF 0x3f3f3f3f
typedef long long LL;

int n, a[N], dp[N];

int solve1()
{
    for(int i=0; i<=n; i++)
        dp[i] = INF;
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
        int pos = upper_bound(dp, dp+n, a[i]) - dp;
        dp[pos] = min(dp[pos], a[i]);
        ans = max(ans, pos);
    }
    return ans+1;
}
int solve2()
{
    for(int i=0; i<=n; i++)
        dp[i] = INF;
    int ans = 0;
    for(int i=n; i>=1; i--)
    {
        int pos = upper_bound(dp, dp+n, a[i]) - dp;
        dp[pos] = min(dp[pos], a[i]);
        ans = max(ans, pos);
    }
    return ans+1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);
        int ans1 = solve1();
        int ans2 = solve2();
        if(ans1 >= n-1 || ans2 >= n-1)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}

时间: 2024-12-30 05:33:12

Almost Sorted Array---hdu5532(j简单dp)的相关文章

HDU5280 Senior&#39;s Array(简单DP)

题目链接:传送门 题意: 给定一个长度为n的序列,和一个修改的值p,必须从原序列中选一个位置修改成p, 求修改后的区间和的最大值. 分析: 枚举位置+最大区间和.复杂度O(n^2); 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int ma

简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in t

POJ #3267 The Cow Lexicon 型如&quot; E[j] = opt{D+w(i,j)} &quot;的简单DP 区间DP

Description 问题的描述以及输入输出的样例可以看这里:链接 思路 虽然 DISCUSS 中总有人说水题,但是我觉得这道题的质量可以 (或许我比较弱ORZ ,在做过的 DP 题里算 medium 难度. 题目的意思是给你一个主串和一堆子串,需要你将子串和主串完全匹配上,在匹配过程中可以删除主串中匹配不上的字符,最后统计出被删除的最少字符数目. 比如主串是 carmsr ,子串有 car .mr 两种.可以只用 car 去匹配,那么匹配不上的字符有 m.s.r 三个,所以需要删除三个字符:

HDU5653 Bomber Man wants to bomb an Array 简单DP

题意:bc 77 div1 1003(中文题面) 分析:先不考虑将结果乘以 1e6. 设 dp[i] 为从前 i 个格子的状态可以获得的最大破坏指数. 那么我们可以枚举每个炸弹,该炸弹向左延伸的距离和向又延伸的距离. 设第 i 个炸弹破坏区间为 [l, r], 则 dp[r] = dp[l - 1] + log2(r - l + 1).答案就是 dp[n - 1].不要忘记最后要向下取整. 注:我对官方题解稍作解释,dp[i]代表前i个格子可以获得的最大破坏指数 但是更新的时候,需要注意,假设有

HDU5532 Almost Sorted Array

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 9982    Accepted Submission(s): 2329 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Leetcode题目:Merge Sorted Array

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

Leetcode: Merge Sorted Array

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B a

[LeetCode] Merge Sorted Array [22]

题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar