Card Hand Sorting binary enumeration violence

  In fact, due to this problem, then the order of arrangement of each suit only four colors, but also just 4! Species, then the internal each suit in the end is ascending or descending, in fact, can also direct violence, there is a total of 4! * 2 ^ 4 cases, and then directly to sort it, but how do we calculate the position you want to move it? ? ? We consider this, we are due to ensure internal order, then it must be a last ascending or descending order, then insert a card is actually quite change the relative position of the interior, consider disorderly, we certainly find the longest increment subsequence, then they must not move with each other, while others will definitely have to move, because the order before and after they met, and they are sure to move directly to binary enumeration.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
   int id;
   int val;
   int col;
   int flag;
}a[105];
int dp[69];
int col[4]={0,1,2,3};
char s[105];
int n;
bool cmp(node x,node y){
   if (x.col==y.col){
      return x.flag<y.flag;
   }
   return col[x.col]<col[y.col];
}
int LCS(){
    memset(dp,0,sizeof(dp));
    int ans=1;
    for (int i=1;i<=n;i++){
        dp[i]=1;
        for (int j=1;j<i;j++){
            if (a[i].id>a[j].id){
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
        ans=max(ans,dp[i]);
    }
    return ans;
}
int main(){
  while(~scanf("%d",&n)){
  int ans=0x3f3f3f3f;
  for (int i=1;i<=n;i++){
     scanf("%s",s);
     if (s[0]=='T')
        a[i].val=10;
     else if (s[0]=='J')
        a[i].val=11;
     else if (s[0]=='Q')
        a[i].val=12;
     else if (s[0]=='K')
        a[i].val=13;
     else if (s[0]=='A')
        a[i].val=14;
     else
        a[i].val=s[0]-'0';
     if (s[1]=='s')a[i].col=0;
     else if (s[1]=='h')a[i].col=1;
     else if (s[1]=='d')a[i].col=2;
     else a[i].col=3;
     a[i].id=i;
  }
  //cout<<"sssssss";
  do{
    for (int i=1;i<16;i++){
        for (int j=1;j<=n;j++){
             a[j].flag=a[j].val*(((i>>col[a[j].col])&1)?1:-1);
        }
        sort(a+1,a+1+n,cmp);
        int minn=n-LCS();
        ans=min(ans,minn);
    }

  }while(next_permutation(col,col+4));
  printf("%d\n",ans);
  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/bluefly-hrbust/p/11628677.html