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= ...
随机推荐
- phpStorm使用技巧及快捷键
下面是PhpStorm的注册码.Key,其license由用户名和License值组成. User name: EMBRACE License key: ===== LICENSE BEGIN === ...
- AngularJs记录学习04
<html> <head> <title>Angular JS Views</title> <script src="js/Angula ...
- Python学习教程(learning Python)--2.2.2 Python全局和局部变量
Python的变量也有全局和局部变量之分. 1. 局部变量 用在子函数里的变量称之为局部变量,其生命周期为该函数执行周期,即函数执行完后变量即不存在.由于局部变量和某个函数直接相关,故不同子函数里可以 ...
- Map和HashMap
通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...
- 简答的理解C语言中的各种类型函数
1.变参函数 变长参数的函数即参数个数可变.参数类型不定 的函数.最常见的例子是printf函数.scanf函数和高级语言的Format函数.在C/C++中,为了通知编译器函数的参数个数和类型可变(即 ...
- node.js 在 Express4.0 框架使用 Connect-Busboy 实现文件上传
node.js下四种post提交数据的方式 今天说分享的是其中一种,就是上传文件. Express 4.0 以后,将功能原子化,高内聚,低耦合,独立出了很多中间件 今天主要分享文件上传 对于conne ...
- poj 1338 Ugly Numbers
原题链接:http://poj.org/problem?id=1338 优先队列的应用,如下: #include<cstdio> #include<cstdlib> #incl ...
- OpenStack: OVS安装
> OVS安装:1. Install the Open vSwitch plug-in and its dependencies:# apt-get install \neutron-plugi ...
- iOS学习之Object-C语言内存管理
一.内存管理的方式 1.iOS应用程序出现Crash(闪退),90%的原因是因为内存问题. 2.内存问题: 1)野指针异常:访问没有所有权的内存,如果想要安全的访问,必须 ...
- EasyUI datagrid frozencolumn的bug???
今天碰到了个很蛋疼的问题.我用到了easyui 的 treegrid,内容只显示一列,我把它设置成了冻结列. 在谷歌调试下,因为内容比较多,所以,会有竖向的滚动条.但是,到了ie和火狐,滚动条神奇般没 ...