zoukankan      html  css  js  c++  java
  • 汉诺塔(思维、DP思想)

    链接:https://ac.nowcoder.com/acm/contest/3007/C
    来源:牛客网

    题目描述

    现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔的游戏。
    我们知道玩汉诺塔游戏需要把若干木板按照上小下大的顺序堆叠在一起,但因为木板是矩形,所以有一个问题:
    第 i 块木板能放在第 j 块木板上方当且仅当 Xi<Xj 且 Yi<Yj,于是你很可能没法把所有的木板按照一定的次序叠放起来。
    你想把这些木板分为尽可能少的组,使得每组内的木板都能按照一定的次序叠放。
    你需要给出任意一种合理的分组方案。
    提醒:“任意”意味着你的答案不必和标准输出完全一致,只要正确即可。

    输入描述:

    第一行,一个正整数 N
    接下来 N 行,每行两个正整数表示 Xi 和 Yi
    对于所有的数据,1≤N≤100,000,1≤Xi,Yi≤N,Xi 互不相等且 Yi 互不相等

    输出描述:

    输出文件包含两行,第一行一个正整数,表示最少组数
    第二行 N 个正整数,依次表示你的方案中每块木板分在了哪一组
    组的编号必须是从 1 开始的连续整数

    输入

    3
    1 1
    2 3
    3 2

    输出

    2
    1 1 2

    最长上升子序列的 贪心+二分法:O(nlogn) 

    分析:要让一个序列具有最长上升子序列,其实就是保证子序列中的每个元素尽可能小,降低门槛,让后面的元素尽可能多进入该子序列

    实现:定义一个最长子序列数组Array,以及当前长度Len,从头到尾维护数组a

    a. a[i]>Array[i] (当前元素大于子序列结尾元素),则a[i]进入子序列:Array[++len] = a[i]

    b. a[i]<=Array[i],这时对Array进行维护,把Array中比a[i]大的第一个元素替换成a[i](这样可以降低后面元素进入子序列的门槛。

    c. 为了降低算法复杂度,因为Array是升序序列,所以用lower_bound查找Array中第一个大于等于a[i]的元素

    官方题解:

    将木板按照Xi从小到大排序,将这时的Yi数列记为Zi数列,则问题变成将Zi划分为尽可能少的若干组上升子序列
    根据Dilworth定理,最小组数等于Zi的最长下降子序列长度
    要求最长下降子序列的长度,我们有一种经典的二分优化dp的方法,在这里不再详述。 借助这种做法我们能给出一种构造方法,在求出最小组数的同时得出方案。
    将状态数组的每个位置变为栈,用入栈操作代替修改元素操作,即可在求出组数的同时,用这些栈来完成对数列的划分。

    这题跟以前一道dp题导弹拦截问题很像。

    求最长下降子序列长度+二分

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const double eps =1e-8;
    16 const int mod=1e9+7;
    17 const int maxn=1e5+10;
    18 using namespace std;
    19 
    20 struct node
    21 {
    22     int x,y;
    23     int id;
    24     bool operator <(const node &a) const//小到大 
    25     {
    26         return x<a.x;
    27     }
    28 }P[maxn];
    29 
    30 int fa[maxn],st[maxn];
    31 int cnt;
    32 
    33 int main()
    34 {
    35     #ifdef DEBUG
    36     freopen("sample.txt","r",stdin);
    37     #endif
    38     
    39     int n;
    40     scanf("%d",&n);
    41     for(int i=1;i<=n;i++)
    42     {
    43         scanf("%d %d",&P[i].x,&P[i].y);
    44         P[i].id=i;
    45     }
    46     sort(P+1,P+1+n);
    47     int cnt=0;
    48     for(int i=1;i<=n;i++)
    49     {
    50         int L=1,R=cnt;
    51         while(L<=R)//二分查找st中第一个比P[i].y小的位置 
    52         {
    53             int mid=(L+R)>>1;
    54             if(st[mid]<P[i].y)
    55                 R=mid-1;
    56             else if(st[mid]>P[i].y) L=mid+1;
    57         }
    58         st[L]=P[i].y;//替换该位置的值(贪心) 
    59         if(L>cnt) cnt=L;//比st中所有都小,添到尾部 
    60         fa[P[i].id]=L;//它替换了st中谁的值,他就跟谁在同一个集合
    61     }
    62     printf("%d
    ",cnt);
    63     for(int i=1;i<=n;i++)
    64         printf(i==n?"%d
    ":"%d ",fa[i]);
    65     
    66     return 0;
    67 }

    求最长上升子序列长度+upper_bound()

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const double eps =1e-8;
    16 const int mod=1e9+7;
    17 const int maxn=1e5+10;
    18 using namespace std;
    19 
    20 struct node
    21 {
    22     int x,y;
    23     int id;
    24     bool operator <(const node &a) const//大到小 
    25     {
    26         return x>a.x;
    27     }
    28 }P[maxn];
    29 
    30 int fa[maxn],st[maxn];
    31 int cnt;
    32 
    33 int main()
    34 {
    35     #ifdef DEBUG
    36     freopen("sample.txt","r",stdin);
    37     #endif
    38     
    39     int n;
    40     scanf("%d",&n);
    41     for(int i=1;i<=n;i++)
    42     {
    43         scanf("%d %d",&P[i].x,&P[i].y);
    44         P[i].id=i;
    45     }
    46     sort(P+1,P+1+n);
    47     int cnt=0;
    48     for(int i=1;i<=n;i++)
    49     {
    50         int pos;
    51         if(P[i].y>st[cnt]) pos=++cnt;//比st中所有都大,添到尾部
    52         else pos=upper_bound(st+1,st+1+cnt,P[i].y)-(st+1)+1;//下标别忘加1 
    53         st[pos]=P[i].y;//替换该位置的值(贪心) 
    54         fa[P[i].id]=pos;//它替换了st中谁的值,他就跟谁在同一个集合
    55     }
    56     printf("%d
    ",cnt);
    57     for(int i=1;i<=n;i++)
    58         printf(i==n?"%d
    ":"%d ",fa[i]);
    59     
    60     return 0;
    61 }

    -

  • 相关阅读:
    杭电 Problem
    杭电Problem 5053 the sum of cube 【数学公式】
    杭电 Problem 2089 不要62 【打表】
    杭电 Problem 4548 美素数【打表】
    杭电 Problem 2008 分拆素数和 【打表】
    杭电 Problem 1722 Cake 【gcd】
    杭电 Problem 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
    杭电Problem 1872 稳定排序
    杭电 Problem 1753 大明A+B
    东北林业大 564 汉诺塔
  • 原文地址:https://www.cnblogs.com/jiamian/p/12315601.html
Copyright © 2011-2022 走看看