Problem Statement

There are $N$ balls arranged from left to right.
The color of the $i$-th ball from the left is Color $C_i$, and an integer $X_i$ is written on it.

Takahashi wants to rearrange the balls so that the integers written on the balls are non-decreasing from left to right.
In other words, his objective is to reach a situation where, for every $1\leq i\leq N-1$, the number written on the $(i+1)$-th ball from the left is greater than or equal to the number written on the $i$-th ball from the left.

For this, Takahashi can repeat the following operation any number of times (possibly zero):

Choose an integer $i$ such that $1\leq i\leq N-1$.

If the colors of the $i$-th and $(i+1)$-th balls from the left are different, pay a cost of $1$.
(No cost is incurred if the colors are the same).

Swap the $i$-th and $(i+1)$-th balls from the left.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • $2 \leq N \leq 3\times 10^5$
  • $1\leq C_i\leq N$
  • $1\leq X_i\leq N$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$C_1$ $C_2$ $\ldots$ $C_N$
$X_1$ $X_2$ $\ldots$ $X_N$

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective, as an integer.


Sample Input 1

5
1 5 2 2 1
3 2 1 2 1

Sample Output 1

6

Let us represent a ball as $($Color$,$ Integer$)$.
The initial situation is $(1,3)$, $(5,2)$, $(2,1)$, $(2,2)$, $(1,1)$.
Here is a possible sequence of operations for Takahashi:

  • Swap the $1$-st ball (Color $1$) and $2$-nd ball (Color $5$). Now the balls are arranged in the order $(5,2)$, $(1,3)$, $(2,1)$, $(2,2)$, $(1,1)$.
  • Swap the $2$-nd ball (Color $1$) and $3$-rd ball (Color $2$). Now the balls are arranged in the order $(5,2)$, $(2,1)$, $(1,3)$, $(2,2)$, $(1,1)$.
  • Swap the $3$-rd ball (Color $1$) and $4$-th ball (Color $2$). Now the balls are in the order $(5,2)$, $(2,1)$, $(2,2)$, $(1,3)$, $(1,1)$.
  • Swap the $4$-th ball (Color $1$) and $5$-th ball (Color $1$). Now the balls are in the order $(5,2)$, $(2,1)$, $(2,2)$, $(1,1)$, $(1,3)$.
  • Swap the $3$-rd ball (Color $2$) and $4$-th ball (Color $1$). Now the balls are in the order$(5,2)$, $(2,1)$, $(1,1)$, $(2,2)$, $(1,3)$.
  • Swap the $1$-st ball (Color $5$) and $2$-nd ball (Color $2$). Now the balls are in the order $(2,1)$, $(5,2)$, $(1,1)$, $(2,2)$, $(1,3)$.
  • Swap the $2$-nd ball (Color $5$) and $3$-rd ball (Color $1$). Now the balls are in the order $(2,1)$, $(1,1)$, $(5,2)$, $(2,2)$, $(1,3)$.

After the last operation, the numbers written on the balls are $1,1,2,2,3$ from left to right, which achieves Takahashi's objective.

The $1$-st, $2$-nd, $3$-rd, $5$-th, $6$-th, and $7$-th operations incur a cost of $1$ each, for a total of $6$, which is the minimum.
Note that the $4$-th operation does not incur a cost since the balls are both in Color $1$.


Sample Input 2

3
1 1 1
3 2 1

Sample Output 2

0

All balls are in the same color, so no cost is incurred in swapping balls.


Sample Input 3

3
3 1 2
1 1 2

首先当且仅当 \(X_i<X_{i+1}\) 才会交换第 \(i\) 个和第 \(i+1\) 个。交换完后会减少一个逆序对。所以不考虑颜色,交换次数等于逆序对个数。

考虑颜色如果两个数颜色相同,那么不计价值。所以答案还要减去同颜色的逆序对个数即可。

#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int x[N],c,y,t[N],n,m;
long long dp[N][N],ans;
int main()
{
memset(dp,-0x7f,sizeof(dp));
dp[0][0]=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",x+i);
for(int i=1;i<=m;i++)
scanf("%d%d",&c,&y),t[c]+=y;
for(int i=1;i<=n;i++)
{
dp[i][0]=dp[i-1][0];
for(int j=1;j<=n;j++)
dp[i][j]=dp[i-1][j-1]+t[j]+x[i],dp[i][0]=max(dp[i][0],dp[i-1][j-1]),ans=max(ans,dp[i][j]);
}
printf("%lld",ans);
}

[ABC261F] Sorting Color Balls的更多相关文章

  1. AtCoder Beginner Contest 261 F // 树状数组

    题目链接:F - Sorting Color Balls (atcoder.jp) 题意: 有n个球,球有颜色和数字.对相邻的两球进行交换时,若颜色不同,需要花费1的代价.求将球排成数字不降的顺序,所 ...

  2. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  3. HDOJ(HDU) 2060 Snooker(英语很重要。。。)

    Problem Description background: Philip likes to play the QQ game of Snooker when he wants a relax, t ...

  4. canvas绘制弹跳小球

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 3P - Snooker

    background: Philip likes to play the QQ game of Snooker when he wants a relax, though he was just a ...

  6. 老男孩Day15作业:商城列表页面(静态)

    一. 一.作业需求: 1.完成商城列表静态页面的抒写 二.博客地址:https://www.cnblogs.com/catepython/p/9205636.html 三.运行环境 操作系统:Win1 ...

  7. Python-Day07-图形用户界面和游戏开发

    Python-100Day-学习打卡Author: Seven_0507Date: 2019-05-22123 文章目录Python图形用户界面和游戏开发1. tkinter模块2. Pygame进行 ...

  8. js 学习四 对象应用 吃货游戏

    游戏来源于 Mdn学习网站: 该例子用于对象的理解非常有效(建议看完上面网站的内容在开始练习) 弹球 body { margin: 0; overflow: hidden; font-family: ...

  9. awsl

    from enum import Enum, uniquefrom math import sqrtfrom random import randint import pygame @uniquecl ...

  10. HDU100题简要题解(2060~2069)

    这十题感觉是100题内相对较为麻烦的,有点搞我心态... HDU2060 Snooker 题目链接 Problem Description background: Philip likes to pl ...

随机推荐

  1. Mysql高阶自定义排序

    Mysql高阶自定义排序 嗨,大家好,我是远码,隔三岔五给大家分享一点工作的技术总结,花费的时间不多,几分钟就行,谢谢! Mysql对我们码农来说是在熟悉不过的日常了,就不在介绍它的基础用法了,今天我 ...

  2. 《SQL与数据库基础》09. 事务

    @ 目录 事务 简介 操作 方式一 方式二 四大特性(ACID) 并发事务问题 事务隔离级别 本文以 MySQL 为例 事务 简介 事务是一组操作的集合,它是一个不可分割的工作单位.事务会把所有的操作 ...

  3. 《CTFshow-Web入门》01. Web 1~10

    @ 目录 web1 题解 web2 题解 web3 题解 web4 题解 web5 题解 原理 web6 题解 原理 web7 题解 web8 题解 web9 题解 原理 web10 题解 ctf - ...

  4. iOS MDM HeadFirst

    引言 Apple MDM (Mobile Device Management) 字面理解就是一种管理移动设备的方式,覆盖 iOS 5 及更高版本的 iPhone/iPod touch/iPad.Mac ...

  5. 使用GPU训练Pytorch模型

    如何使用GPU训练Pytorch模型 这两天的深度学习实验真实让人头疼,传说中的"猫狗大战",对模型的训练用CPU的话9h起步,12h是常态,大学生哪耗得起,因此查找资料搭建了GP ...

  6. redis 源码分析:Jedis 哨兵模式连接原理

    1. 可以从单元测试开始入手 查看类JedisSentinelPool private static final String MASTER_NAME = "mymaster"; ...

  7. 43道Python经典案例题(有答案)

    1.有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? for x in range(0,5): for y in range(0,5): for z in range( ...

  8. JAVA图搜索算法之DFS-BFS

    图算法DFS与BFS BFS和DFS代表对图进行遍历,即搜索的算法,搜索算法中常用的只要有两种算法:深度优先遍历(Depth-First-Search : DFS)和广度优先遍历(Breadth-Fi ...

  9. 造轮子之集成GraphQL

    先简单对比以下GraphQL和WebAPI:GraphQL和Web API(如RESTful API)是用于构建和提供Web服务的不同技术. 数据获取方式: Web API:通常使用RESTful A ...

  10. 17.2 实现无管道正向CMD

    WSASocket 无管道正向CMD,使用WSASocket函数创建一个TCP套接字,并绑定到一个本地地址和端口上.然后使用CreateProcess函数创建一个新的CMD进程,并将标准输入.输出和错 ...