[ABC261F] Sorting Color Balls
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的更多相关文章
- AtCoder Beginner Contest 261 F // 树状数组
题目链接:F - Sorting Color Balls (atcoder.jp) 题意: 有n个球,球有颜色和数字.对相邻的两球进行交换时,若颜色不同,需要花费1的代价.求将球排成数字不降的顺序,所 ...
- Android Animation学习(三) ApiDemos解析:XML动画文件的使用
Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...
- HDOJ(HDU) 2060 Snooker(英语很重要。。。)
Problem Description background: Philip likes to play the QQ game of Snooker when he wants a relax, t ...
- canvas绘制弹跳小球
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 3P - Snooker
background: Philip likes to play the QQ game of Snooker when he wants a relax, though he was just a ...
- 老男孩Day15作业:商城列表页面(静态)
一. 一.作业需求: 1.完成商城列表静态页面的抒写 二.博客地址:https://www.cnblogs.com/catepython/p/9205636.html 三.运行环境 操作系统:Win1 ...
- Python-Day07-图形用户界面和游戏开发
Python-100Day-学习打卡Author: Seven_0507Date: 2019-05-22123 文章目录Python图形用户界面和游戏开发1. tkinter模块2. Pygame进行 ...
- js 学习四 对象应用 吃货游戏
游戏来源于 Mdn学习网站: 该例子用于对象的理解非常有效(建议看完上面网站的内容在开始练习) 弹球 body { margin: 0; overflow: hidden; font-family: ...
- awsl
from enum import Enum, uniquefrom math import sqrtfrom random import randint import pygame @uniquecl ...
- HDU100题简要题解(2060~2069)
这十题感觉是100题内相对较为麻烦的,有点搞我心态... HDU2060 Snooker 题目链接 Problem Description background: Philip likes to pl ...
随机推荐
- Linux第四章(80X86保护模式及其编程)
80X86保护模式及其编程 80X86基础知识 保护模式内存管理 各种保护措施 中断和异常处理 任务管理 保护模式编程的初始化 一个简单的多任务内核 4.1 80X86系统寄存器和系统指令 为了协助处 ...
- 【问题解决】容器部署MySQL的数据在docker commit导出的镜像中丢失
问题起因 最近公司有个甲方项目参加竞赛,要求在(基于kubeflow/arena)平台上部置应用,可以将MySQL打包在应用一起,也可以分开部署,没有提供volume相关的支持.大意是可以把初始好的数 ...
- 行行AI人才直播第16期:【无界AI首席研究员】刘秋衫《AI创新设计:AIGC赋能设计行业的新思维》
在这一轮生成式AI浪潮中,设计行业是受波及最为广泛的一个行业.这是设计师们始料未及的事情,至少在此之前,人们认为以设计.艺术为首的创意产业是最难被AI改变的产业之一.而生成式AI的出现,与其说是一次冲 ...
- 在 Spring 6 中使用虚拟线程
一.简介 在这个简短的教程中,我们将了解如何在 Spring Boot 应用程序中利用虚拟线程的强大功能. 虚拟线程是Java 19 的预览功能,这意味着它们将在未来 12 个月内包含在官方 JDK ...
- Springboot简单功能示例-5 使用JWT进行授权认证
springboot-sample 介绍 springboot简单示例 跳转到发行版 查看发行版说明 软件架构(当前发行版使用) springboot hutool-all 非常好的常用java工具库 ...
- IDEA2019 Debug傻瓜式上手教程
Step Into (F7):步入,如果当前行有方法,可以进入方法内部,一般用于进入自定义方法内,不会进入官方类库的方法. Force Step Into (Alt + Shift + F7) ...
- Springboot项目使用Undertow替换内置Tomcat服务器,实现RESTFUL接口web应用
Maven实例:pom.xml文件中添加更换依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- 我看懂了oracle中pivot行转列的用法
我看懂了PIVOT的用法 用法Select * From 表名,PIVOT( SUM('要合并的列1'),MAX('要合并的列2'),....FOR 将值转换成列的列名 IN(列名1,列名2,列名3 ...
- sql分组后排序计算
用法:RANK() OVER(PARTITION BY 分组字段 ORDER BY 排序字段 ) 例子:要得到n4列 ---创建测试数据create table tb(n1 varchar2(40) ...
- 使用 Alacritty 替代 Gnome-Terminal 解决 Ubuntu 中终端的行间距问题
我之前发了一篇随笔新版 Ubuntu 中 gnome-terminal 可恶的行间距问题逼我退回了 Ubuntu 20.04,描述了新版 Ubuntu 中终端的行间距问题.评论中给出了一个解决办法,那 ...