zoukankan      html  css  js  c++  java
  • HDU 5773:The All-purpose Zero(贪心+LIS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5773

    The All-purpose Zero

    Problem Description
     
    ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.
     
    Input
     
    The first line contains an interger T,denoting the number of the test cases.(T <= 10)
    For each case,the first line contains an interger n,which is the length of the array s.
    The next line contains n intergers separated by a single space, denote each number in S.
     
    Output
     
    For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.
     
    Sample Input
     
    2
    7
    2 0 2 1 2 0 5
    6
    1 2 3 3 0 0
     
    Sample Output
     
    Case #1: 5
    Case #2: 5
     
    Hint
     
    In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
     
    题意:在一个序列里面求最长严格上升子序列,其中0可以任意变换为其他数。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 #define N 100005
     7 
     8 int dp[N];
     9 int num[N];
    10 /*
    11 0可以转化成任意整数,包括负数,
    12 显然求LIS时尽量把0都放进去必定是正确的。
    13 因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,
    14 统计结果的时候再算上0的数量。为了保证严格递增,
    15 我们可以将每个权值S[i]减去i前面0的个数,再做LIS,
    16 就能保证结果是严格递增的。
    17 */
    18 int main()
    19 {
    20     int t;
    21     scanf("%d", &t);
    22     for(int cas = 1; cas <= t; cas++) {
    23         int n;
    24         scanf("%d", &n);
    25         int cnt = 0;
    26         int zero = 0;
    27         for(int i = 1; i <= n; i++){
    28             int a;
    29             scanf("%d", &a);
    30             if(a == 0) zero++;
    31             else {
    32                 num[++cnt] = a - zero;
    33             }
    34         }
    35         if(cnt == 0) {
    36             printf("Case #%d: %d
    ", cas, n);
    37             continue;
    38         }
    39         memset(dp, 0, sizeof(dp));
    40         int tot = 1;
    41         dp[1] = num[1];
    42         for(int i = 2; i <= cnt; i++) {
    43             if(num[i] > dp[tot]) dp[++tot] = num[i];
    44             else {
    45                 int pos = lower_bound(dp + 1, dp + tot, num[i]) - dp;
    46                 dp[pos] = num[i];
    47             }
    48         }
    49         printf("Case #%d: %d
    ", cas, tot + zero);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    linux LVM详解
    Mysql SQL优化系列之——执行计划连接方式浅释
    Vue SSR常见问题、异常处理以及优化方案
    vue组件生命周期详解
    axios全局设置url公共请求头
    WebView中JS调用Android Method 遇到的坑整理
    node.js项目多环境配置
    用vue构建多页面应用
    前端系列-移动端开发踩过的一些坑
    Async:简洁优雅的异步之道
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5717285.html
Copyright © 2011-2022 走看看