传送门

题目

There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 i 2N) is ai, and the color of this ball is represented by a letter ci. ci = W represents the ball is white; ci = B represents the ball is black.

Takahashi the human wants to achieve the following objective:

  • For every pair of integers (i,j) such that 1 i < j N, the white ball with i written on it is to the left of the white ball with j written on it.
  • For every pair of integers (i,j) such that 1 i < j N, the black ball with i written on it is to the left of the black ball with j written on it.

In order to achieve this, he can perform the following operation:

  • Swap two adjacent balls.

Find the minimum number of operations required to achieve the objective.

Constraints

  • 1 N 2000
  • 1 ai N
  • ci = W or ci = B.
  • If i j, (ai,ci) (aj,cj)

Input

Input is given from Standard Input in the following format:

N
c1 a1
c2 a2
:
c2N a2N

Output

Print the minimum number of operations required to achieve the objective.

题目大意

给出一个黑棋N个白棋N个的排列,每一种颜色的球分别标上1 - N,每次可以交换相邻两个球,求白棋相对顺序正确并且黑棋相对顺序正确,所需要最少的步数

分析

设dpij为排好白球前i个和黑球前j个所需最小步数,用a和b数组分别记录黑白球的每一数值所在位置,用c1和c2分别记录将黑白球某一数值x移到此颜色的x-1数值之后所需步数(这里的步数既有同色数目又有异色数目)。所以我们不难想出dpij由dpi-1j和dpij-1转移而来,因此得到方程式dp[i][j]=min(dp[i-1][j]+c1[i][j],dp[i][j-1]+c2[i][j])。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int dp[2100][2100],a[2100],b[2100],c1[2100][2100],c2[2100][2100];
int main(){
      int n,m,i,j,k,x;
      char ch;
      cin>>n;
      for(i=1;i<=2*n;i++){
           cin>>ch>>x;
           if(ch=='B'){
               a[x]=i;
           }else {
               b[x]=i;
           }
      }
      for(i=1;i<=n;i++){
           for(j=1;j<i;j++)
              if(a[i]<a[j])c1[i][0]++;
           for(j=1;j<=n;j++){
               c1[i][j]=c1[i][j-1];
               if(b[j]>a[i])c1[i][j]++;
           }
      }
      //c用于记录要向后走几步
      for(i=1;i<=n;i++){
           for(j=1;j<i;j++)
              if(b[i]<b[j])c2[0][i]++;
           for(j=1;j<=n;j++){
               c2[j][i]=c2[j-1][i];
               if(a[j]>b[i])c2[j][i]++;
           }
      }
      int wh1=0,wh2=0;
      for(i=1;i<=n;i++){
           wh1+=c1[i][0];
           wh2+=c2[0][i];
           dp[i][0]=wh1;
           dp[0][i]=wh2;
      }
      for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
            dp[i][j]=min(dp[i-1][j]+c1[i][j],dp[i][j-1]+c2[i][j]);
      cout<<dp[n][n]<<endl;
      return 0;
}

ARC097E Sorted and Sorted的更多相关文章

  1. arc 097 E - Sorted and Sorted

    E - Sorted and Sorted Time limit : 2sec / Memory limit : 1024MB Score : 600 points Problem Statement ...

  2. python中sorted和.sorted 、reversed和reverse的注意点

    L=[1,2,3,4]l1=[123,123,23]if l1.sort() == L.reverse():   #这个判断式是恒等的,因为两个函数的返回值都是None(其实是无返回值)    pri ...

  3. python中sorted和sorted 、reversed和reverse的使用。

    #encoding = utf-8 list = [1,8,3,6] print(list.sort()) #None print(list) #[1,3,6,8] print(sorted(list ...

  4. AtCoder ARC097C Sorted and Sorted:dp

    传送门 题意 有 $ 2n $ 个球排成一行,其中恰好有 $ n $ 个白球和 $ n $ 个黑球.每个球上写着数字,其中白球上的数字的并集为 $ \lbrace 1 \dots n\rbrace $ ...

  5. python 中 sorted() 和 list.sort() 的用法

    今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reve ...

  6. python 排序sorted

    num = [3,2,4,6,5] anum = sorted(num) dnum = sorted(num,reverse=True) print '升序:',anum # 升序: [2, 3, 4 ...

  7. Redis in .NET Core 入门:(5) Sorted SET

    第1篇:https://www.cnblogs.com/cgzl/p/10294175.html 第2篇 String:https://www.cnblogs.com/cgzl/p/10297565. ...

  8. Redis实战 - 2.list、set和Sorted Set

    List Redis的List是通过Linked List(链表)来实现的String集合,所以插入数据的速度很快. 但是缺点就是在数据量比较大的时候,访问某个数据的时间可能会很长,但针对这种情况,可 ...

  9. [Swift]LeetCode768. 最多能完成排序的块 II | Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

随机推荐

  1. flex 坐标系

    全局坐标(舞台坐标)  本地坐标 内容坐标系  地图坐标(MapPoint) flash和flex针对不同的目的,提供了3种不同的坐标系 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容 ...

  2. [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  3. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  4. float元素的父元素自适应高度

    当在对象内的盒子使用了float后,导致对象本身不能被撑开自适应高度,这个是由于浮动产生原因. 如何解决父div对象自适应高度,方法有三种. 1.对父元素设置固定高度 2.使用clear清除浮动 3. ...

  5. Oracle的PL_SQL的结构

    --PL/SQL的结构 declare --声明变量和常量关键字 v_name nvarchar2(); v_age integer;--常规变量声明 v_product table_name.col ...

  6. jquery带下拉菜单和焦点图

    jQuery,下拉菜单,二级菜单,索引按钮,焦点图代码,jquery带下拉菜单和焦点图是一款顶部通栏带二级下拉菜单和banner导航菜单代码. JQuery特效代码来源:http://www.huiy ...

  7. 应验log4j.xml时不能找到log4j.dtd

    原因分析:log4j.xml中使用log4j的DTD验证其格式的有效性"<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd&quo ...

  8. MVC中URL传多个参数

    1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...

  9. fatal error C1071: unexpected end of file found in comment

    1.错误 #include<iostream> using namespace std; int main() { ..... return 0; } //如果把注释放到这里了,那么提交就 ...

  10. 【转】CSS制作图形速查表-存档

      http://www.w3cplus.com/css/css-simple-shapes-cheat-sheet http://www.cnblogs.com/powertoolsteam/p/c ...