[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 ...
随机推荐
- 当 GPT-4 拥有了 Diff 视图,那真的是如虎添翼!
目录 1. 当你要求 GPT-4 帮你写点代码时 2. 你需要的背景知识都在这里 2.1 关于 GoPool 和 DevChat 2.2 关于 GoPool 的工作原理 2.3 我想要让 taskQu ...
- 领域驱动设计(DDD):从基础代码探讨高内聚低耦合的演进
大家好,我是付威,一名已在编码第一线奋斗了十余年的程序员.在2019年我初次接触到领域驱动设计(Domain-Driven Design,简称DDD)的概念.在我的探索中,我发现许多有关DDD的教程过 ...
- c# .NET 高级编程 高并发必备技巧 - 锁
锁 最为常见的应用就是 高并发的情况下,库存的控制.本次只做简单的单机锁介绍. 直接看代码: 每请求一次库存-1. 假如库存1000,在1000个人请求之后,库存将变为0. public int Re ...
- 前后端分离中台框架 Admin.Core 学习-介绍与配置说明
中台框架后端项目 Admin.Core 的介绍与配置说明 中台admin是前后端分离权限管理系统,Admin.Core为后端项目,基于.NET 7.0开发. 支持多租户.数据权限.动态 Api.任务调 ...
- 5、Mybatis之获取参数值
5.1.创建新module 5.1.1.右击SSM文件夹,创建新module 5.1.2.选择maven 5.1.3.配置module名称和路径 5.1.4.module初始状态 5.1.5.复制打包 ...
- WPF-封装自定义雷达图控件
源码地址:https://gitee.com/LiuShuiRuoBing/code_blog 雷达图用于表示不同内容的占比关系,在项目中有广泛的应用,但是目前未曾有封装良好的雷达图控件,鉴于最近项目 ...
- LeetCode46全排列(回溯入门)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 题目描述 难度:中等 给定一个不含重复数字的数组 nu ...
- Go语言中JSON的反序列化规则
Unmarshal 解析 func Unmarshal(data []byte, v any) error Unmarshal 解析 JSON 编码的数据,并将结果存储在 v 指向的值中.如果 v 为 ...
- 「codechef - STRQUER」Strange Queries
link. 首先对原序列排序,考虑静态序列做法为:设 \(f(n,k\in\{0,1\})\) 为对于前 \(n\) 个数,第 \(n\) 个数否 / 是已经决策完毕的最优方案,转移即 \[\begi ...
- Teamcenter RAC 开发之《PlaceHolder》
背景 做个swing表单,有时候想实现一些网页input标签的placeHolder提示,可能本人写vue or html写多,对某些细节有强迫症,所以找小下资料 实现方法(Swingx) 看源码