Sorting is done by moving one card at a time from its current position to a new position in the hand, at the start, end, or in between two adjacent cards. What is the smallest number of moves required to sort a given hand of cards?
标签:between mes 技术 can 分享 while ide src esc
7
9d As 2s Qd 2c Jd 8h
2
去掉的大小王的扑克牌,要求对给出扑克排序,你每次可以拿出一张牌,插到任意位置,重复此操作直到序列有序。有序定义为:相同花色必须在一起,相同花色必须升序或者降序,不同的花色之间没有要求。
/// author:Kissheart /// #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<vector> #include<stdlib.h> #include<math.h> #include<queue> #include<deque> #include<ctype.h> #include<map> #include<set> #include<stack> #include<string> #define INF 0x3f3f3f3f #define FAST_IO ios::sync_with_stdio(false) const double PI = acos(-1.0); const double eps = 1e-6; const int MAX=1e5+10; const int mod=1e9+7; typedef long long ll; using namespace std; #define gcd(a,b) __gcd(a,b) inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;} inline ll inv1(ll b){return qpow(b,mod-2);} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;} inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;return x*f;} //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int dp[55][55],n; int order[4]={0,1,2,3}; char a[2]; map<char,int>mp; vector<int>v[5],s,t; int main() { mp[‘s‘]=0;mp[‘h‘]=1;mp[‘d‘]=2;mp[‘c‘]=3;mp[‘2‘]=0;mp[‘3‘]=1;mp[‘4‘]=2;mp[‘5‘]=3;mp[‘6‘]=4; mp[‘7‘]=5;mp[‘8‘]=6;mp[‘9‘]=7;mp[‘T‘]=8;mp[‘J‘]=9;mp[‘Q‘]=10;mp[‘K‘]=11,mp[‘A‘]=12; s.push_back(0); t.push_back(0); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",a); s.push_back(mp[a[0]]+mp[a[1]]*13); v[mp[a[1]]].push_back(mp[a[0]]+mp[a[1]]*13); } sort(v[0].begin(),v[0].end()); sort(v[1].begin(),v[1].end()); sort(v[2].begin(),v[2].end()); sort(v[3].begin(),v[3].end()); int ans=INF; do { for(int i=0;i<16;i++) { for(int j=0;j<4;j++) { if(!v[order[j]].size()) continue; if((i>>j)&1) { for(int k=0;k<v[order[j]].size();k++) t.push_back(v[order[j]][k]); } else { for(int k=v[order[j]].size()-1;k>=0;k--) t.push_back(v[order[j]][k]); } } memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(s[i]==t[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } ans=min(ans,n-dp[n][n]); t.clear();t.push_back(0); } }while(next_permutation(order,order+4)); printf("%d\n",ans); return 0; }
标签:between mes 技术 can 分享 while ide src esc
原文地址:https://www.cnblogs.com/Kissheart/p/9749649.html