Topcoder Srm627 DIV 2
A,B:很水,注意边界,话说HACK都是这些原因。
C:
R[I][J]:表示反转I-J能改变冒泡排序的次数;
DP方程:dp[i][k]=max(dp[j][k],dp[j][k-1]+dp[j][i]) (0<=j<i)
最后枚举,具体看代码
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string.h> using namespace std;
class BubbleSortWithReversals{
public:
int bs(vector<int> a)
{
int n=a.size(),ans=0; while (n--){
for (int i=0;i<a.size()-1;i++)
if (a[i]>a[i+1])
{
swap(a[i],a[i+1]);
ans++;
}
}
return ans;
} int getMinSwaps(vector<int> A,int K){
int n=A.size();
int dp[55][55]={0};
int r[55][55]={0}; for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++){
r[i][j]=bs(A);
reverse(A.begin()+i,A.begin()+j+1);
r[i][j]-=bs(A);
reverse(A.begin()+i,A.begin()+j+1);
} for (int i=0;i<n;i++){
for (int k=1;k<=K;k++)
for (int j=0;j<i;j++)
{
dp[i][k]=max(dp[i][k],dp[j][k]);
dp[i][k]=max(dp[i][k],dp[j][k-1]+r[j][i]);
}
} int ans=0;
for (int i=0;i<=K;i++)
ans=max(ans,dp[n-1][i]);
return bs(A)-ans;
}
};
Topcoder Srm627 DIV 2的更多相关文章
- TopCoder[SRM513 DIV 1]:Reflections(1000)
Problem Statement Manao is playing a new game called Reflections. The goal of the game is trans ...
- Topcoder SRM584 DIV 2 500
#include <set> #include <iostream> #include <string> #include <vector> using ...
- Topcoder SRM583 DIV 2 250
#include <string> #include <iostream> using namespace std; class SwappingDigits { public ...
- 【补解体报告】topcoder 634 DIV 2
A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...
- Topcoder SRM548 Div 1
1. KingdomAndTrees 给出n个数a[1..n],求一个数组b[1..n]满足b严格递增,且b[1]>=1. 定义代价为W = max{abs(a[i]-b[i])},求代价最小值 ...
- TopCoder[SRM587 DIV 1]:TriangleXor(550)
Problem Statement You are given an int W. There is a rectangle in the XY-plane with corners at ...
- TopCoder[SRM587 DIV 1]:ThreeColorability(900)
Problem Statement There is a H times W rectangle divided into unit cells. The rows of cells are ...
- TopCoder[SRM513 DIV 1]:PerfectMemory(500)
Problem Statement You might have played the game called Memoria. In this game, there is a board ...
- [topcoder]BinaryCards
现在觉得有空时可以刷一下topcoder的DIV 2的Lvl 3的题目.感觉和刷LeetCode和WikiOi都是不一样的. http://community.topcoder.com/stat?c= ...
随机推荐
- 4)Java容器类相关知识
1>Array 和 Arrays: Arrays:用来操作array的工具类,其中包含一组static函数: equals():比较两个array 是否相等. array拥有相同元 ...
- 在Silverlight宿主html页面添加按钮无法显示
在建silverlight应用程序时宿主html中嵌入的silverlight时出现的问题: 预想效果: 实际效果: silverlight填满整个page的所以无法显示html中其他的控件 解决办法 ...
- 15.python的for循环与迭代器、生成器
在前面学习讲完while循环之后,现在终于要将for循环这个坑填上了.之所以拖到现在是因为for循环对前面讲过的序列.字典.集合都是有效的,讲完前面的内容再来讲for循环会更加容易上手. 首先,for ...
- [div+css布局]命名规则
//首页可能碰到的 页头:header登录条:loginBar标志:logo侧栏:sideBar广告:banner导航:nav子导航:subNav菜单:menu子菜单:subMenu搜索:search ...
- linux C 管道
单一进程使用管道基本上毫无意义.管道一般用来子进程和父进程之间的通信,或者兄弟进程间的通信. 创建管道的主要函数是pipe #include<unistd.h> ]) pipe函数创建一个 ...
- Ubuntu下部署java JDK和eclipse IDE
安装Java编程开发环境: Ubuntu默认安装openjava,可以通过java -version查看是否安装.但我使用Ubuntu9.10升级到10.04LTS时,openjava没有了.另外,如 ...
- DB2测试存储过程的原子性
存储过程在运行过程中需要对其做异常处理.原子性等测试 下面是一个原子性测试案例 ===================================== 代码区域 ================= ...
- ORACLE-12C-RAC INSTALL
OS: Oracle Linux Server release 5.7 DB: 12.1.0.1.0 挂载镜像:mkdir /media/diskmount /dev/cdrom /media/dis ...
- Spring框架中的IOC和DI的区别
上次面试被问到IOC和DI的区别时,没怎么在意,昨天又被问到,感觉有点可惜.今晚总算抽点时间,查看了spring官方文档.发现,IoC更像是一种思想,DI是一种行为.为了降低程序的耦合度,利用spri ...
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序 先看一下我们完成的效果图 首先下载xmppframework这个框架,下载 点ZIP下载 接下来,用Xcode新建一个工程 将以下 ...