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. [ABC131E] Friendships

    2023-01-30 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 找规律,构造 解题思路 先构造一个菊花图为最大边的图,再依次连边减小k. ...

  2. Oracle数据库字符集概述及修改方式

    1.字符集概述 Oracle语言环境的描述包括三部分:language.territory.characterset(语言.地域.字符集) language:主要指定服务器消息的语言,提示信息显示中文 ...

  3. 《CTFshow-Web入门》06. Web 51~60

    @ 目录 web51 题解 web52 题解 原理 web53 题解 原理 web54 题解 原理 web55 题解 原理 web56 题解 原理 web57 题解 原理 web58 题解 原理 we ...

  4. android webview调用js(vue)问题记录

    这几天和别人对接移动端,安卓平台,我们这边输出vue界面,安卓方反馈轮询的时候调用不到,具体原因也定位不到,只能确定前端这边没几句代码,应该没有问题,因此决定自己下载个android studio写个 ...

  5. 《Python魔法大冒险》006 变量的迷雾

    小鱼和魔法师走了很久,终于来到了一个神秘的森林前.这片森林与众不同,它被一层厚厚的迷雾所包围,仿佛隐藏着无尽的秘密. 小鱼好奇地看着这片森林:"这是什么地方?" 魔法师:这是魔法森 ...

  6. 领域驱动设计(DDD):DDD落地问题和一些解决方法

    欢迎继续关注本系列文章,下面我们继续讲解下DDD在实战落地时候,会具体碰到哪些问题,以及解决的方式有哪些. DDD 是一种思想,主要知道我们方向,具体如何做,需要我们根据业务场景具体问题具体分析. 充 ...

  7. 使用GPU训练Pytorch模型

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

  8. Python网络编程——操作系统基础、网络通信原理、.网络通信实现、DNS域名解析、 网络通信流程

    文章目录 一.操作系统基础 二.网络通信原理 2.1 互联网的本质就是一系列的网络协议 2.2 osi七层协议 2.3 tcp/ip五层模型讲解 2.3.1 物理层 2.3.2 数据链路层 2.3.3 ...

  9. 【第一章 web入门】afr_3——模板注入与proc文件夹

    [第一章 web入门]afr_3--模板注入与proc文件夹 题目来源n1book,buu上的环境 看题 url中提供了name参数,类似在路径中进行了文件名查询然后展示: 随便输入一个数字: 说明肯 ...

  10. 【Python微信机器人】第一篇:在windows11上编译python

    前言 我打算写一个系列,内容是将python注入到其他进程实现inline hook和主动调用.本篇文章是这个系列的第一篇,后面用到的案例是注入python到PC微信实现基本的收发消息.文章着重于py ...