Single Round Match 517 Round 1 - Division I, Level Two AdjacentSwaps
来源:互联网 发布:linux装进u盘 编辑:程序博客网 时间:2024/06/05 06:45
好题!
要采用区间DP的方法去求解。用f[l][r]表示把[l,r]这段区间排序的方法数。题目的交换性质也表明了如果要把这段区间排序,也只要交换这段区间内部的元素就可以了。那么我们只需要枚举一段区间最后交换的相邻元素。不妨设为k和k+1。如果最后后一次能这样换,原数列第l到第k-1个元素加上k+1个元素必然是排序后前k-l+1小的元素的排列。前面k-l+1个元素要交换k-l次,后面的r-k个元素要交换r-k-1次,把这么多交换穿插起来,共r-l-1次,有C[r-l-1][k-l]种组合,转移方程就是:
f[l][r]+=f[l][k]*f[k+1][r]*C[r-l-1][k-l];f[i][i]=1
答案是f[0][n-1]
#include <bits/stdc++.h>#define maxn 64using namespace std;long long C[maxn][maxn],f[maxn][maxn];bool vis[maxn];vector<int>b;const int MOD=1000000007;class AdjacentSwaps{public:int theCount(vector <int> p){int n=p.size();for(int i=0;i<maxn;i++){C[i][0]=C[i][i]=1;for(int j=1;j<i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;}for(int i=0;i<n;i++)f[i][i]=1;for(int len=2;len<=n;len++){for(int l=0;l+len-1<n;l++){int r=l+len-1;f[l][r]=0;b.clear();for(int i=l;i<=r;i++)b.push_back(p[i]);sort(b.begin(),b.end());for(int k=l;k<r;k++){memset(vis,0,sizeof(vis));for(int i=0;i<k-l;i++)vis[b[i]]=1;vis[b[k-l+1]]=1;bool ok=1;for(int i=l;i<=k;i++){if(!vis[p[i]]){ok=0;break;}}if(ok){long long cur=0;cur=f[l][k]*f[k+1][r]%MOD;cur=cur*C[r-l-1][k-l]%MOD;f[l][r]=(f[l][r]+cur)%MOD;}}}}return f[0][n-1];}};
0 0
- Single Round Match 517 Round 1 - Division I, Level Two AdjacentSwaps
- Topcoder Single Round Match 453.5 Round 1 - Division I, Level Two TheProduct
- topcoder Single Round Match 457 Round 1 - Division I, Level Two baTheHexagonsDivOne
- Member Single Round Match 461 Round 1 - Division I, Level Two BuildingCities
- Single Round Match 464 Round 1 - Division I, Level Two ColorfulDecoration
- Member Single Round Match 465 Round 1 - Division I, Level Two GreenWarfareze
- Member Single Round Match 474 Round 1 - Division I, Level Two TreesCount
- Single Round Match 477 Round 1 - Division I, Level Two PythTriplets
- Single Round Match 480 Round 1 - Division I, Level Two NetworkSecurity
- Single Round Match 479 Round 1 - Division I, Level Two TheAirTripDivOne
- Single Round Match 506 Round 1 - Division I, Level Two SlimeXGrandSlimeAuto
- Single Round Match 524 Round 1 - Division I, Level Two LongestSequence
- Single Round Match 573 Round 1 - Division I, Level Two SkiResorts
- Single Round Match 508 Round 1 - Division I, Level Two YetAnotherORProblem
- Single Round Match 462 Round 1 - Division I, Level Three WarTransportation
- Single Round Match 459 Round 1 - Division II, Level Three gaiParkAmusement
- Single Round Match 671 Round 1 - Division II, Level Three(状压DP)(略难)
- 2010 TCO Algorithm Online Round 5 - Division I, Level Two LongJourney
- iOS 手势并存
- HDOJ 题目1509 Be the Winner(anti-nim)
- iOS开发---网址收藏
- HTML前端ajax请求
- Xcode的Architectures、Valid Architectures和Build Active Architecture Only属性
- Single Round Match 517 Round 1 - Division I, Level Two AdjacentSwaps
- iOS 类别和继承的区别之synthesize not allowed in a category\'s implementation
- iOS UDID(转)
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)
- 支付宝SDK和ZBar SDK冲突问题
- Xcode6 模拟器不显示键盘解决方案
- Jenkins权限配置
- 使用UITextView的dataDetectorTypes实现超链接需要注意的事项!
- TOP域名:发展分析