BZOJ1166 : [Baltic2008]Magical Stones

考虑二分答案,转化为求有多少$\leq lim$的数满足条件。

从两侧往中间进行数位DP,设$f[l][r][j][x][y][z][pre][suf]$表示当前准备填的两个位置是$l$和$r$,已经有$j$对相邻的数不同,$l-1$填的是$x$,$r+1$填的是$y$,正序串和逆序串的大小关系为$z$,正序串和$lim$的大小关系为$pre$,逆序串和$lim$的大小关系为$suf$的方案数。

注意到$r=n-l+1$,因此可以省去$r$这一维,然后枚举接下来填什么数进行转移即可。

时间复杂度$O(2^6n^2k)$。

#include<cstdio>
typedef long long ll;
const ll inf=1LL<<61;
const int N=65;
int n,m,i,j,S,l,r,x,y,z,p,s,nj,nz,np,ns,A,B,a[N];ll L,R,mid,K,ans=-1,f[N][N][1<<5];
inline void up(ll&a,ll b){a+=b;if(a>inf)a=inf;}
inline ll check(ll mid){
  for(i=n;i;i--)a[i]=mid&1,mid>>=1;
  for(i=1;i<=n;i++)for(j=0;j<=m;j++)for(S=0;S<32;S++)f[i][j][S]=0;
  ll ret=0;
  for(f[0][0][0]=l=1,r=n;l<=r;l++,r--)for(j=0;j<=m;j++)for(S=0;S<32;S++)if(f[l-1][j][S]){
    x=S&1,y=S>>1&1,z=S>>2&1,p=S>>3&1,s=S>>4&1;
    if(l<r)for(A=0;A<2;A++)for(B=0;B<2;B++){
      nj=j;
      if(l>1)nj+=(A^x)+(B^y);
      if(nj>m)continue;
      if(z)nz=1;
      else{
        if(A>B)continue;
        nz=A<B;
      }
      if(p)np=1;
      else{
        if(A>a[l])continue;
        np=A<a[l];
      }
      if(B==a[r])ns=s;else ns=B>a[r];
      up(f[l][nj][A|(B<<1)|(nz<<2)|(np<<3)|(ns<<4)],f[l-1][j][S]);
    }
    if(l==r)for(A=0;A<2;A++){
      if(l>1&&j+(A^x)+(A^y)>m)continue;
      if(!p&&(A>a[l]||A==a[l]&&s))continue;
      up(ret,f[l-1][j][S]);
    }
  }
  if(n%2==0)for(j=0;j<=m;j++)for(S=0;S<32;S++)if(f[l-1][j][S]){
    x=S&1,y=S>>1&1;
    if(j+(x^y)>m)continue;
    p=S>>3&1,s=S>>4&1;
    if(!p&&s)continue;
    up(ret,f[l-1][j][S]);
  }
  return ret;
}
int main(){
  scanf("%d%d%lld",&n,&m,&K);
  L=0;R=(1LL<<n)-1;
  while(L<=R)if(check(mid=(L+R)>>1)>=K)R=(ans=mid)-1;else L=mid+1;
  if(ans<0)puts("NO SUCH STONE");else for(i=n-1;~i;i--)putchar(ans>>i&1?‘X‘:‘I‘);
  return 0;
}

  

时间: 2024-10-10 09:10:56

BZOJ1166 : [Baltic2008]Magical Stones的相关文章

hdu4941 Magical Forest

Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest will m

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

hdu 5154 Harry and Magical Computer 拓扑排序

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal wi

【BZOJ】【4052】【CERC2013】Magical GCD

DP/GCD 然而蒟蒻并不会做…… Orz @lct1999神犇 首先我们肯定是要枚举下端点的……嗯就枚举右端点吧…… 那么对于不同的GCD,对应的左端点最多有log(a[i])个:因为每次gcd缩小,至少变成gcd/2(2是最小的质因数),所以是log个左端点…… 所以我们就有了log段!每段的gcd是相同的.当我们加入一个新的右端点时,除了该节点本身外,不会出现新的左端点,原有的左端点可能会不变,或是两(多)段合并成一段,用滚动数组记一下,暴力搞就可以了……$O(n*log^2n)$ Orz

HDOJ 5154 Harry and Magical Computer floyd判环

floyd判环 Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 404 Problem Description In reward of being yearly outstanding magic student, H

hdu5154--Harry and Magical Computer(拓扑排序)

Harry and Magical Computer Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2015-01-06) Description In reward of being yearly outstanding magic student, Harry gets a mag

hdu 4941 Magical Forest(Map)

http://acm.hdu.edu.cn/showproblem.php?pid=4941 因为地图的行和列很大,操作次数也很多,直接循环模拟肯定不行.但可以用map映射一下,当交换行和列的时候,直接交换它们的映射值,直接O(1)进行交换. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

hdu 5973 Game of Taking Stones

Game of Taking Stones Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 456    Accepted Submission(s): 174 Problem Description Two people face two piles of stones and make a game. They take turns