zoukankan      html  css  js  c++  java
  • Editing a Book UVA

    You have
    n
    equal-length paragraphs numbered 1 to
    n
    . Now you want to arrange them in the order
    of 1
    ;
    2
    ;:::;n
    . With the help of a clipboard, you can easily do this: Ctrl-X (cut) and Ctrl-V (paste)
    several times. You cannot cut twice before pasting, but you can cut several contiguous paragraphs at
    the same time - they'll be pasted in order.
    For example, in order to make
    f
    2, 4, 1, 5, 3, 6
    g
    , you can cut 1 and paste before 2, then cut 3 and
    paste before 4. As another example, one copy and paste is enough for
    f
    3, 4, 5, 1, 2
    g
    . There are two
    ways to do so: cut
    f
    3, 4, 5
    g
    and paste after
    f
    1, 2
    g
    , or cut
    f
    1, 2
    g
    and paste before
    f
    3, 4, 5
    g
    .
    Input
    The input consists of at most 20 test cases. Each case begins with a line containing a single integer
    n
    (1
    <n<
    10), thenumber of paragraphs. The next line contains a permutation of 1
    ;
    2
    ;
    3
    ;:::;n
    . The
    last case is followed by a single zero, which should not be processed.
    Output
    For each test case, print the case number and the minimal number of cut/paste operations.
    Sample Input
    6
    2 4 1 5 3 6
    5
    3 4 5 1 2
    0
    Sample Output
    Case 1: 2
    Case 2: 1
    /**
    题目:Editing a Book UVA - 11212
    链接:https://vjudge.net/problem/UVA-11212
    题意:lrjP208.
    思路:启发函数:当前已经操作的步数d,限制的步数maxd,后继不相同的个数x。
    由于每一步最多使x减少3,所以如果3*d+x>maxd*3;那么肯定不行。
    
    如果通过位置不同的个数作为启发,是不行的,无法判断。
    
    如果是每个数的后继不同的数有多少个。那么可以推算出每一步操作后,最多减少3个不同的后继。
    
    最终结果所有后继都满足。即a[i] = a[i-1]+1;
    
    具体看lrj算法竞赛入门经典P209;
    
    
    */
    
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int mod=1e9+7;
    const int maxn=1e2+5;
    const double eps = 1e-12;
    int a[10], n;
    int getDif()///获得后继不合法数.
    {
        int cnt = 0;
        for(int i = 2; i <= n; i++){
            if(a[i]!=a[i-1]+1){
                cnt++;
            }
        }
        return cnt;
    }
    int temp[10];
    void Move(int *temp,int *a,int L,int R,int LL,int RR)
    {
        int now = L;
        for(int i = LL; i <= RR; i++){
            a[now++] = temp[i];
        }
        for(int i = L; i <= R; i++){
            a[now++] = temp[i];
        }
    }
    bool dfs(int d,int maxd)
    {
        if(3*d+getDif()>3*maxd) return false;
        if(getDif()==0) return true;
        int odd[10];
        memcpy(odd,a,sizeof(int)*(n+1));
        ///如果[L,R]是连续的,那么不需要剪切。
        for(int i = 1; i <= n; i++){
            for(int j = i; j <= n; j++){
                for(int k = j+1; k <= n; k++){///每次交换[i,j],[j+1,k];每次交换相邻的两段区间。
                    Move(odd,a,i,j,j+1,k);
                    if(dfs(d+1,maxd)) return true;
                    memcpy(a,odd,sizeof(int)*(n+1));///保证每一次操作后,把a复原。
                }
            }
        }
        return false;
    }
    int main()
    {
        int cas = 1;
        while(scanf("%d",&n)==1)
        {
            if(n==0) break;
            for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
            for(int maxd = 0; ; maxd++){
                if(dfs(0, maxd)){
                    printf("Case %d: %d
    ",cas++,maxd); break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    android 图片特效处理之模糊效果
    android 图片特效处理之怀旧效果
    android图像处理系列之六-- 给图片添加边框(下)-图片叠加
    android图像处理系列之五-- 给图片添加边框(中)
    android图像处理系列之四-- 给图片添加边框(上)
    android图像处理系列之三-- 图片色调饱和度、色相、亮度处理
    Android学习笔记进阶18 之画图并保存图片到本地
    Android学习笔记进阶19 之给图片加边框
    HDU3572_Task Schedule(网络流最大流)
    再淡spring jdbc 连接池断开重连设置
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6899606.html
Copyright © 2011-2022 走看看