zoukankan      html  css  js  c++  java
  • 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接:

      http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807

    题目大意:

      给你一个长度为N(N<=105)的数列,数列中的0可以被其他数字替换,最终形成一个1~N的排列,求这个排列的最长上升子序列长度为N-1的方案数。

    题目思路:

      【模拟】

      这道题需要分类讨论。

      首先可以肯定,一个长度为n的序列最长上升子序列长度为n-1(最长下降子序列长度为2),那么这个序列的样子是1~n从小到大排列后其中一个数字挪到其余数字中间(错位)

      一个长度为L的0区间,考虑第i个数字,如果放在第i位上,则f[i]=f[i-1]

      如果放在第i-1位上,则第i位有i-1种放法(前面i-1个数都可以放在第i位,此时错位的为第i位上的数)

      如果放在其他位置,则这个数就是错位的数,那这个数可以放的位置有i-2个(其他的数必须按从小到大排列才能保证错位数为1个)

      

      现在考虑给定的数字,如果某个数字a[i]与i的偏移量超过1,那么意味着它前面必然有至少2个比它大的(a[i]-i>1),或它后面有至少2个比它小的(a[i]-i<-1),那么当前的a[i]必然是唯一的最终答案里偏移的数,或者无解。

      如果给定的数字没有偏移,那么那个偏移的数字一定在只含0的区间中的一个,那么ans=Σ F[l] (F[l]即为长度为l的0区间偏移量为1的方案数)

      如果区间[L,R]里给定的数字都往前偏移了1,那么代表有一个小于L的数在R的后面(如果区间[L,R]里给定的数字都往后偏移了1,代表一个大于R的数在L前面),并且根据前面判断得出这个数字还未被填(偏移量超过1),左端小于L的能填的数=L左端到第一个a[i]=i之间的0数(l[L]),能够填的位置就是R右端到第一个a[i]=i之间的0数(r[R]),所以ans=l[L]*r[R]。(另一种情况同理)

      还有一种情况是某两个数字交换位置,这个我一开始考虑漏了。两位数字交换只有在这两个数字相邻的时候才可行且只有唯一解,并且要求其他数字必须要从小到大排列不能再有错位。

      1 //
      2 //by coolxxx
      3 //#include<bits/stdc++.h>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<iomanip>
      8 #include<map>
      9 #include<stack>
     10 #include<queue>
     11 #include<set>
     12 #include<bitset>
     13 #include<memory.h>
     14 #include<time.h>
     15 #include<stdio.h>
     16 #include<stdlib.h>
     17 #include<string.h>
     18 //#include<stdbool.h>
     19 #include<math.h>
     20 #define min(a,b) ((a)<(b)?(a):(b))
     21 #define max(a,b) ((a)>(b)?(a):(b))
     22 #define abs(a) ((a)>0?(a):(-(a)))
     23 #define lowbit(a) (a&(-a))
     24 #define sqr(a) ((a)*(a))
     25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
     26 #define mem(a,b) memset(a,b,sizeof(a))
     27 #define eps (1e-10)
     28 #define J 10000
     29 #define mod 1000000007
     30 #define MAX 0x7f7f7f7f
     31 #define PI 3.14159265358979323
     32 #pragma comment(linker,"/STACK:1024000000,1024000000")
     33 #define N 100004
     34 using namespace std;
     35 typedef long long LL;
     36 double anss;
     37 LL aans;
     38 int cas,cass;
     39 int n,m,lll,ans;
     40 int a[N];
     41 LL f[N];
     42 void init()
     43 {
     44     int i;
     45     f[0]=f[1]=0;
     46     for(i=2;i<N;i++)f[i]=f[i-1]+i-1+i-2;
     47 }
     48 void work1(int x)
     49 {
     50     int i,t=1;
     51     for(i=1;i<=n;i++)
     52     {
     53         if(i==x)continue;
     54         if(t==a[x])t++;
     55         if(t==a[i] || !a[i])t++;
     56         else break;
     57     }
     58     if(i<=n)puts("0");
     59     else puts("1");
     60 }
     61 void work2(int l,int r)
     62 {
     63     int i,x,ll=0,rr=0;
     64     for(i=l;i<=r;i++)
     65     {
     66         if(!a[i])continue;
     67         if(a[i]-i!=a[l]-l)break;
     68     }
     69     if(i<=r){puts("0");return;}
     70     for(i=l-1;i;i--)
     71     {
     72         if(!a[i])ll++;
     73         else if(a[i]-i!=a[l]-l)break;
     74     }
     75     for(i=r+1;i<=n;i++)
     76     {
     77         if(!a[i])rr++;
     78         else if(a[i]-i!=a[l]-l)break;
     79     }
     80     if(a[l]-l==1)ll++;
     81     else rr++;
     82     printf("%lld
    ",1LL*ll*rr);
     83 }
     84 void work3(int x,int y,int l,int r)
     85 {
     86     int i;
     87     if(x!=y || l!=r || x!=l-1){puts("0");return;}
     88     puts("1");
     89 }
     90 void work4()
     91 {
     92     int i,sz=0;
     93     for(i=1;i<=n;i++)
     94     {
     95         if(!a[i])sz++;
     96         else aans+=f[sz],sz=0;
     97     }
     98     if(sz)aans+=f[sz];
     99     printf("%lld
    ",aans);
    100 }
    101 int main()
    102 {
    103     #ifndef ONLINE_JUDGE
    104 //    freopen("1.txt","r",stdin);
    105 //    freopen("2.txt","w",stdout);
    106     #endif
    107     int i,j,k;
    108     int x,y,l,r;
    109     init();
    110 //    for(scanf("%d",&cass);cass;cass--)
    111 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    112 //    while(~scanf("%s",s))
    113     while(~scanf("%d",&n))
    114     {
    115         aans=0;
    116         x=l=n+1,y=r=0;
    117         for(i=1;i<=n;i++)scanf("%d",a+i);
    118         for(i=1;i<=n;i++)
    119         {
    120             if(!a[i])continue;
    121             if(abs(a[i]-i)>1){work1(i);break;}
    122             if(a[i]-i==1)x=min(x,i),y=max(y,i);
    123             if(a[i]-i==-1)l=min(l,i),r=max(y,i);
    124         }
    125         if(i<=n)continue;
    126         if(x<=n && y>0 && !r)work2(x,y);
    127         else if(l<=n && r>0 && !y)work2(l,r);
    128         else if(y && r)work3(x,y,l,r);
    129         else work4();
    130     }
    131     return 0;
    132 }
    133 /*
    134 //
    135 
    136 //
    137 */
    View Code
  • 相关阅读:
    Linux基础命令
    python中eval的用法
    man命令手册打开以后的使用方法
    Sequel Pro(连接mysql数据库)的下载和使用
    pycharm里连接mysql
    python里的路径拼接
    第一个接口自动化框架atp
    使用yaml(yml)写用例
    计算机及Linux简介
    xpath和css selector
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5855030.html
Copyright © 2011-2022 走看看